LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Multiplying data column in awk - can rounding be suppressed? (https://www.linuxquestions.org/questions/linux-newbie-8/multiplying-data-column-in-awk-can-rounding-be-suppressed-533237/)

johnpaulodonnell 02-28-2007 07:12 AM

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.

johnpaulodonnell 02-28-2007 07:39 AM

formatted printing takes care of it:

Code:

awk '{printf "%8.3f  %8.3f  %5.2f\n", $1*0.001, $2*0.001, $3}' datafile

berbae 02-28-2007 07:56 AM

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.


All times are GMT -5. The time now is 01:34 AM.