Using any awk:
$ awk '{printf("%06.2f\n", $1)}' <<< 99.111111
099.11
The padding length takes into account the whole formatted number, so with the fractional part taking up 3 characters (.
and the 2
precision digits), you need a padding length of 6 if you want the integral part to be padded to a length of 3. Note that the padding also takes into account the sign if any so -1
for instance would be formatted as -01.00
¹
¹ or possibly -01,00
or -01٫00
depending on the user's locale and the awk
implementation². In the case of GNU awk
, it also depends on whether there's a POSIXLY_CORRECT
variable in the environment or not.