I am using bc and scale for evaluating a expression however I want it to round up instead of round down. What is the simplest way to do this?
$ read exp
5+50*3/20 + (19*2)/7
$ echo "scale=3; $exp" | bc -l
17.928
However my desired answer is 17.929
I prefer the answer to be an addendum to my answer than something different ground up. Thanks
Here are some of the things I've tried:
$ echo "scale=4; ($exp+0.0005)" | bc -l
17.9290
$ echo "scale=3; ($exp+0.0005)" | bc -l
17.9285
However I want 17.929
as answer with no zero at the end.
solved it by using scaling factor
of printf
as 3
, scaling factor
of bc
as 4
and adding 0.0005
to the expression:
printf "%.3f\n" $( echo "scale=4; $exp+0.0005" | bc -l )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With