- Practice
- Linux Shell
- Bash
- Arithmetic Operations
- Discussions

# Arithmetic Operations

# Arithmetic Operations

varunyellina + 0 comments When you use

``echo "scale=3; `$sum/$`x | bc"``

your expected result would be with

`3`

decimal points. You should note that there is there is no**rounding off**done by**bc**.

Now in bash you can use the

**printf**function to do the rounding off that you require. Use`scale = 4`

and then use**printf**to round off to 3 decimal points using`"%.3f"`

Dillanm + 0 comments Having the same issues as everyone else here;

`scale = 3`

gives 17.928 NOT 17.929. Although I know that the answer should be 17.929, quite an odd problem.EDIT:

You need to use the

`printf`

method for it to work, the reason is that`-bc`

literally chops off the end of the number instead of actually rounding.

sreya92 + 0 comments Pretty straightforward way to do it, feel free to correct if it's wrong or can be improved:

read num echo $num | bc -l | xargs printf "%.3f"

pieta_absurdum + 0 comments http://stackoverflow.com/questions/26465496/rounding-up-float-point-numbers-bash

I found this on stackoverflow...Even people there are using printf or even piping it to python script???I thought i would learn something here..If we need to learn how to round up than make an example or put a good link on the site...and not the links that are here for examples..

Giridhar_L + 0 comments This works

read word value=` echo "$word " | bc -l ` echo $value | python -c "print round(float(raw_input()),3)"

Sort 130 Discussions, By:

Please Login in order to post a comment