2

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

Psowe
  • 23
  • 4

3 Answers3

1

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
riteshtch
  • 8,629
  • 4
  • 25
  • 38
1

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))
Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
0

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))
};
Community
  • 1
  • 1
Nemanja Trifunovic
  • 3,017
  • 1
  • 17
  • 20