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 echos:

 printf "%.3f\n" "$(bc -l <<< "$var")"

or even

 printf "%.3f\n" "$(bc -l <<< "5+50*3/20 + (19*2)/7")"
Share:
7,683

Related videos on Youtube

WannaBeCoder
Author by

WannaBeCoder

#SOreadytohelp

Updated on September 18, 2022

Comments

  • WannaBeCoder
    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 is 17.92857142857143. When I use the script below it is giving me 17.928. The answer should be 17.929.

    read exp
    echo "scale=3; $exp" |bc -l
    

    And one more question is how to use printf to do the same task

    • Admin
      Admin over 9 years
      You mean "round up" to three decimals. Depending on the rounding conventions applied, 17.928 may or may not be correct.
    • Admin
      Admin over 9 years
      Yes round it up.
    • Admin
      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
    Rahul over 9 years
    Are 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
    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
    countermode over 7 years
    Please elaborate your answer for the benefit of other users.
  • Amit24x7
    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 places printf %.3f is taking floating number where .3 tells it to round the number to 3 decimal places.
  • Francky_V
    Francky_V over 3 years
    Seems 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
    Francky_V over 3 years
    As 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
    muru over 3 years
    Nobody'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 than bc these days, and likely to be already loading into memory for some other program too, so it's probably faster to launch than bc.
  • Francky_V
    Francky_V over 3 years
    You'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]...