Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

round up the bc scale instead of round down with scale or printf [duplicate]

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.

like image 270
Mona Jalal Avatar asked May 24 '16 03:05

Mona Jalal


1 Answers

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 ) 
like image 75
Mona Jalal Avatar answered Sep 23 '22 05:09

Mona Jalal