$ echo "9/2" | bc
4
By default bc truncate the result which is bit annonying...
Using bc -l option gives you a far too precise result :
$ echo "9/2" | bc -l
4.50000000000000000000
Fortunately, bc comes with the "scale" option to set the scale to whatever presision you wish.
To do it non interactively, you need to specify the scale in the echo before the operation.
For example, to set a precision of 2 digits :
$ echo -e "scale=2 \n 9/2" | bc
4.50
So the script code would look like :
val=$(echo -e "scale=2 \n 9/2" | bc)
No comments:
Post a Comment