3

I want to print 0.74 for 0.748.

If I use

awk '{printf "%0.2f\n",$1}' filename

for 0.748 it prints 0.75. But I want 0.74 and remove remaining part. For 77.348 it should be 77.34.

Any ideas?

There is no

gawk -M

option to use ROUNDMODE="Z" ??

Even I couldn't find ceiling and floor functions as well.

1 Answers1

1

Using awk

  • with print

    awk -F. '{print $1"."substr($2,1,2)}' filename
    

or

  • with printf

    awk -F. '{printf "%0.2f\n",$1"."substr($2,1,2)}' filename
    

Example

$ awk -F. '{print $1"."substr($2,1,2)}' foo
0.74
77.65

$ awk -F. '{printf "%0.2f\n",$1"."substr($2,1,2)}' foo
0.74
77.65

$ cat foo
0.748
77.657
A.B.
  • 90,397