Printf question: getting padded zero in decimal plus floating point together.

Hi Experts,
Quick question:

I am trying to get the output with decimal and floating point but not working:

echo "20.03" | awk '{printf "%03d.2f\n" , $0 }'
020.2f

How to get the output as :

020.03

Thank you.

$
$ echo "20.03" | awk '{printf "%06.2f\n" , $0 }'
020.03
$
$
1 Like

You can also control numeric output format using gawk OFMT built-in variable:

$ echo "20.03" | awk 'BEGIN{OFMT="%06.2f"}{print $0+0}'
020.03

OR

$ awk 'BEGIN{OFMT="%06.2f";print 20.03}'
020.03
2 Likes

For this simple example you don't need awk

$ printf "%06.2f\n" 20.03
020.03
2 Likes

Thanks all I got it now .

echo "20.03" | awk '{printf "%06.2f\n" , $0 }'
awk 'BEGIN{OFMT="%06.2f";print 20.03}'
printf "%06.2f\n" 20.03

All worked... Thanks.

---------- Post updated at 03:03 PM ---------- Previous update was at 02:59 PM ----------

>> For this simple example you don't need awk

  • correct , Actually the program I was needing help for this was in awk.
    Thanks ...
1 Like