How to round to 2 decimals in bash like MS Excel does?
8
votes
5
answers
1839
views
I spent hours in searching how to round "floating numbers" in BASH but couldn't find any correct! solution :( If I put these numbers to Excel, then I will receive the correct results after rounding to 2 decimals:
3.314 -> 3.31
3.315 -> 3.32
8.124 -> 8.12
8.125 -> 8.13
How to have the exact results in BASH? I tried with
printf
and awk
but don't have the same result
prompt> printf '%.*f\n' 2 8.125
8.12
prompt> echo '8.125' | awk '{printf("%.2f\n", $1)}'
8.12
Asked by user3719454
(202 rep)
Oct 24, 2023, 11:22 AM
Last activity: Oct 28, 2023, 11:46 AM
Last activity: Oct 28, 2023, 11:46 AM