I would like to get two decimals after the dot in this expression: 7/2
#temperature equal 7
tempeture= `cat temperature`
rate= expr $temperature/2
echo "$rate"
I'm getting 3 and I want 3.50. thks
You could also use printf
's type specifier:
$ temperature=7
$ echo "$temperature/2" | bc -l
3.50000000000000000000
$ printf "%.2f\n" $(echo "$temperature/2" | bc -l)
3.50
One way to round to two decimails would be to use the bc
command and setting the scale
variable equal to 2:
echo "scale=2; ($temperature/2)" | bc
You could also use printf
like this:
printf "%.2f" $(($temperature/2))
This question has already been asked and answered.
See: https://askubuntu.com/questions/179898/how-to-round-decimals-using-bc-in-bash
A bash round function:
round()
{
echo $(printf %.$2f $(echo "scale=$2;(((10^$2)*$1)+0.5)/(10^$2)" | bc))
};