をshに私はこの表現では、ドットの後に小数点以下2桁を取得したいと思います:7/2ラウンドは
#temperature equal 7
tempeture= `cat temperature`
rate= expr $temperature/2
echo "$rate"
私は3を取得していると私は3.50をしたいです。 thks
をshに私はこの表現では、ドットの後に小数点以下2桁を取得したいと思います:7/2ラウンドは
#temperature equal 7
tempeture= `cat temperature`
rate= expr $temperature/2
echo "$rate"
私は3を取得していると私は3.50をしたいです。 thks
一つの方法は、使用することですbc
コマンドを使用し、scale
変数を2に設定すると、
echo "scale=2; ($temperature/2)" | bc
また、このようprintf
を使用することができます。
printf "%.2f" $(($temperature/2))
bc呼び出しに-lパラメータを追加する必要があります。それ以外の場合は、整数でのみ動作します。 – PerroVerd
@PerroVerd - それなしで正常に動作しているようですか? – Cyclonecode
この質問は既に回答済みです。
参照: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))
};
あなたはまた、printf
の型指定を使用することができます。2 decimailsに丸める
$ temperature=7
$ echo "$temperature/2" | bc -l
3.50000000000000000000
$ printf "%.2f\n" $(echo "$temperature/2" | bc -l)
3.50
あなたはもう少し説明できますか? –