2

我想在这个表达式中的点后得到两位小数:7/2

#temperature equal 7
tempeture= `cat temperature`  
rate= expr $temperature/2  
echo "$rate"

我得到 3,我想要 3.50。谢谢

4

3 回答 3

1

你也可以使用printf's 类型说明符:

$ temperature=7
$ echo "$temperature/2" | bc -l
3.50000000000000000000
$ printf "%.2f\n" $(echo "$temperature/2" | bc -l)
3.50
于 2016-05-17T09:59:08.603 回答
1

四舍五入到两个分邮件的一种方法是使用bc命令并将scale变量设置为 2:

echo "scale=2; ($temperature/2)" | bc

你也可以printf这样使用:

printf "%.2f" $(($temperature/2))
于 2016-05-17T09:59:28.997 回答
0

这个问题已经被问及并得到了回答。
请参阅:https ://askubuntu.com/questions/179898/how-to-round-decimals-using-bc-in-bash

一个 bash 轮函数:

round()
{
echo $(printf %.$2f $(echo "scale=$2;(((10^$2)*$1)+0.5)/(10^$2)" | bc))
};
于 2016-05-17T09:55:33.350 回答