Sample Header Ad - 728x90

How to round floating point numbers in shell?

23 votes
3 answers
93251 views
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.
Asked by Volker Siegel (17703 rep)
Nov 10, 2014, 05:49 AM
Last activity: May 5, 2023, 10:24 AM