awk division without rounding

Hello to all,
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

Decimal numbers represented by a binary system are ALWAYS rounded / subject to rounding. For example, your first number, given as 0.123456789012 , is internally approximated / stored as 0.123456789011999995553381381796 , which itself is a rounded value already. You may end up less than or greater than your target value, or, in rare cases, exactly on target (0.5, or 0.25, for example).

So, your request is a bit diffucult to fulfill. You can try awk 's OFMT variable set to a fixed output field length, but, surprise, additional undesired decimal places occur:

awk '{val = $1 / 100; print val}' OFMT="%.20f" file
0.00123456789012000006
0.00123456789012344999
0.00123456789012345671

If that is unsatisfying, you can resort to formatted print and a tricky taylored field length computation:

awk '{val = $1 / 100; printf "%.*f\n", length($1), val}' file
0.00123456789012
0.00123456789012345
0.0012345678901234567

This works as there are two additional places needed for the division by 100, but length ($1) already counts two characters: the leading 0 and dot. If you have more significant leading digits, additional measures / steps need to be taken.

2 Likes

Thanks, that's what I was looking for, but how do I put the value into a variable instead of printing it with printf?

Hi.

This might possibly be done for the range of numbers noted by parsing the numbers, inserting 2 zeros, and so avoiding any arithmetic whatsoever -- just a thought ... cheers, drl

the paradigm for the ksh/bash-like shells is:

myVar="$(executeMyCommandHere)"

I need to make the assignment in awk code.
I found another post where they recommend using

variable = sprintf (x, y)

Thanks anyway for the reply :slight_smile: