How do I correctly round IEEE 754 floating point numbers on the command line?
I want to specify the precision of the output number – the count of fractional digits.
Rounding 6.66
to precision 1
should give 6.7
, for example. More in the table below:
Value Precision Rounded
6.66 0 7
6.66 1 6.7
6.66 2 6.66
6.66 3 6.660
6.666 3 6.666
6.6666 3 6.667
It should be usable in an interactive shell, but ideally robust enough for using it in production shell scripts.
Best Answer
Rounding floating point numbers
What does "rounding a floating point number" mean?
That's easy, obviously... Where's my math book from school...
No, we already know nothing related to floating point numbers is easy:
For a start, there are multiple rounding modes:
How to handle the corner cases? How to find out which are the corner cases?
OK, looks like we better use an implementation of the IEEE 754 standard, and let our system take care of that.
To round a floating point number in the shell, based on standard floating point arithmetic, we need three steps:
Turns out that the shell command
printf
can do all of this. It can be used to print numbers according to a format specification as described inman 3 printf
. The numbers are rounded implicitly in the standard way if it is required for the output format:The command
Round
x
top
digits precision with input as command line arguments:Or in a shell pipeline, with input of
x
on standard input, andp
as argument:Examples:
Bad traps
Beware the locale! It specifies the separator between the integral and fraction part - the
.
, as you may expect.But see yourself what happens in a German locale, for example:
Yes, that's right
6,667
- six comma six six seven. That would mess up your script for sure.(But only for the two customers in Germany. Except for the developer's machines currently debugging for these customers.)
More robust
To make it more robust, use:
or
This also uses
/usr/bin/printf
instead of the shell builtin ofbash
orzsh
to work around minor inconsistencies in implementation of theprintf
variants, and prevent a very dirty effect when, in a German locale,LC_ALL
is set, but not exported. Then, the builtin uses,
, and/usr/bin/printf
uses.
...See also
%g
for rounding to a specified number of significant digits.