Sort 130 Discussions, By:
Please Login in order to post a comment
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"
scale = 4
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.
scale = 3
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.
Pretty straightforward way to do it, feel free to correct if it's wrong or can be improved:
echo $num | bc -l | xargs printf "%.3f"
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..
value=` echo "$word " | bc -l `
echo $value | python -c "print round(float(raw_input()),3)"