Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 02-28-2007, 07:12 AM   #1
Registered: Jun 2006
Location: Galway
Distribution: Suse 10.1
Posts: 114

Rep: Reputation: 15
Multiplying data column in awk - can rounding be suppressed?


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:

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?

Old 02-28-2007, 07:39 AM   #2
Registered: Jun 2006
Location: Galway
Distribution: Suse 10.1
Posts: 114

Original Poster
Rep: Reputation: 15
formatted printing takes care of it:

awk '{printf "%8.3f  %8.3f  %5.2f\n", $1*0.001, $2*0.001, $3}' datafile
Old 02-28-2007, 07:56 AM   #3
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep: Reputation: Disabled
use printf instead of print :
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 08:04 AM.
1 members found this post helpful.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
sed / awk command to print line number as column? johnpaulodonnell Linux - Newbie 2 01-22-2007 07:07 AM
Parsing rows and column data from a file using perl dav_y2k Programming 1 10-08-2006 11:57 AM
awk column printing schneidz Programming 7 09-29-2005 06:14 AM
'Last Post' Column showing incorrect data Shade LQ Suggestions & Feedback 1 04-20-2005 01:31 AM
Finding the name and data type of a column in SQL Travis86 Programming 11 08-15-2004 04:20 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:46 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration