Evaluation an expression and rounding up to three decimals
7,683
Solution 1
Python seems have your preferred behaviour:
$ echo 'print(round(' "5+50*3/20 + (19*2)/7" ', 3))' | python3
17.929
Solution 2
Just write this:
read exp
printf %.3f $(echo "$exp" | bc -l)
Solution 3
You can simplify the expression from your comment by using "here string" end removing both echo
s:
printf "%.3f\n" "$(bc -l <<< "$var")"
or even
printf "%.3f\n" "$(bc -l <<< "5+50*3/20 + (19*2)/7")"
Related videos on Youtube
Comments
-
WannaBeCoder almost 2 years
I have an expression
"5+50*3/20 + (19*2)/7"
I need to round it up to 3 decimal places. The answer to this is17.92857142857143
. When I use the script below it is giving me17.928
. The answer should be17.929
.read exp echo "scale=3; $exp" |bc -l
And one more question is how to use
printf
to do the same task-
Admin over 9 yearsYou mean "round up" to three decimals. Depending on the rounding conventions applied,
17.928
may or may not be correct. -
Admin over 9 yearsYes round it up.
-
Admin over 9 years
echo $(printf "%.3f\n" $(echo " scale=4; $exp" |bc))
I used this and got the ans. But will see any one comes up with alternate method. I am learning bash so using it.
-
-
Rahul over 9 yearsAre the quotes around $(...) and $var necessary? It seems to work without the quotes, so I was wondering if they are there for some other cases.
-
jimmij over 9 years@Rahul Yes, it works here with or without quotes, but I recommend to always quote variables, command substitutions, arithmetic expansion, etc. unless you really know what you are doing. Read about details here: unix.stackexchange.com/questions/171346/…
-
countermode over 7 yearsPlease elaborate your answer for the benefit of other users.
-
Amit24x7 over 7 years
$(command)
is 'command substitution', it runs the command, captures its output and inserts it into the command line that contains the $(…)bc -l
is calculating the expression and giving the result upto 20 decimal placesprintf %.3f
is taking floating number where .3 tells it to round the number to 3 decimal places. -
Francky_V over 3 yearsSeems contrived to me to call a whole programming language to perform a single-line operation. Especially since there are solutions to this using purely bash. Why add such a dependancy, essentially for formatting? What if the os doesn't have python3? What if the interpreter would be call by python (as is the case on some distribution)? Older distribution? Does that work on Windows?
-
Francky_V over 3 yearsAs for the other - answer using python... Calling on such a dependancy for as one-liner that can definitely be done in multiple ways in bash is beyond me....
-
muru over 3 yearsNobody's posted a pure bash answer yet, though. I don't see it is all that different from calling
bc
. If anything, Python is more likely to be available thanbc
these days, and likely to be already loading into memory for some other program too, so it's probably faster to launch thanbc
. -
Francky_V over 3 yearsYou're correct it is not pure bash. Still, IMO the point stands, I don't think it's a good practice. The path to the python interpreter may very well change from OS to os even on unix-base systems, not to mention windows. If that's going to be solution, then pretty much anything you can do in bash, you can do with nicer syntax in python, so why bother with bash at all, save for #!/bin/bash python [path-to-myscript.py]...