Latest LQ Deal: Linux Power User Bundle
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org Multiplying data column in awk - can rounding be suppressed?
 User Name Remember Me? Password
 Linux - Newbie This Linux forum is for members that are new to Linux. Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

 02-28-2007, 08:12 AM #1 johnpaulodonnell Member   Registered: Jun 2006 Location: Galway Distribution: Suse 10.1 Posts: 114 Rep: Multiplying data column in awk - can rounding be suppressed? Hi. I have a large XYZ data file. Columns X and Y contain x-positions and y-positions - both are in metres. Column Z holds data values. I have to pass this data file in to a code - but firstly the x,y positions have to be converted to kilometres. My problem is that awk rounds the values upon multiplication by 0.001 - leading to a loss of data accuracy: Code: `awk -v OFS="\t" '{print \$1*0.001, \$2*0.001, \$3}' datafile` So for example x position 123456 metres is converted to 123.5 metres or 123.46 metres say.....Is there any switch to suppress this rounding in awk? Thanks.
 02-28-2007, 08:39 AM #2 johnpaulodonnell Member   Registered: Jun 2006 Location: Galway Distribution: Suse 10.1 Posts: 114 Original Poster Rep: formatted printing takes care of it: Code: `awk '{printf "%8.3f %8.3f %5.2f\n", \$1*0.001, \$2*0.001, \$3}' datafile`
02-28-2007, 08:56 AM   #3
berbae
Member

Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep:
use printf instead of print :
Quote:
 awk -v OFS="\t" '{printf "%.3f %.3f %.3f", \$1*0.001, \$2*0.001, \$3}' datafile
where %.3f gives a floating number with 3 digits after the decimal point.

Last edited by berbae; 02-28-2007 at 09:04 AM.

1 members found this post helpful.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post johnpaulodonnell Linux - Newbie 2 01-22-2007 08:07 AM dav_y2k Programming 1 10-08-2006 12:57 PM schneidz Programming 7 09-29-2005 07:14 AM Shade LQ Suggestions & Feedback 1 04-20-2005 02:31 AM Travis86 Programming 11 08-15-2004 05:20 PM

LinuxQuestions.org

All times are GMT -5. The time now is 11:50 AM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -