Reputation: 65
I'm working on a file that contains a series of decimal values with different precision. I need to divide these numbers by 100 and I don't need to round.
awk '{val = $1 / 100; print val}' input_file.txt
Input file:
0.123456789012
0.123456789012345
0.12345678901234567
output:
0.00123457
0.00123457
0.00123457
I would like not to round the result and get the following output:
0.00123456789012
0.00123456789012345
0.0012345678901234567
Thanks in advance
Upvotes: 1
Views: 751
Reputation: 67497
interesting problem since you want different precision for different numbers. Here is one workaround
$ awk '{d=length($1); printf "%."d"f\n", $1/100}' file
0.00123456789012
0.00123456789012345
0.0012345678901234567
this should cover numbers greater than 1 as well.
$ awk '{d=length($1); if($1>1) d+=1-length(int($1)); printf "%."d"f\n", $1/100}' file
may need to tweak for negative numbers...
Upvotes: 3
Reputation: 133508
Could you please try following.
awk '{sub(/\./,".00")} 1' Input_file
With OP's original command:
awk '{val = $1 / 100; sub(/\./,".00",val);print val}' input_file.txt
Upvotes: 1