我想在这个表达式中的点后得到两位小数:7/2
#temperature equal 7
tempeture= `cat temperature`
rate= expr $temperature/2
echo "$rate"
我得到 3,我想要 3.50。谢谢
你也可以使用printf
's 类型说明符:
$ temperature=7
$ echo "$temperature/2" | bc -l
3.50000000000000000000
$ printf "%.2f\n" $(echo "$temperature/2" | bc -l)
3.50
四舍五入到两个分邮件的一种方法是使用bc
命令并将scale
变量设置为 2:
echo "scale=2; ($temperature/2)" | bc
你也可以printf
这样使用:
printf "%.2f" $(($temperature/2))
这个问题已经被问及并得到了回答。
请参阅: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))
};