我是shell腳本中的全部新功能,因此有一個關於如何舍入整數的問題。將shell腳本中的整數向上舍入
下面是一個腳本通過凱凱的代碼行(smstools3開發商)
balance=$(substr "$result" "$balance_prefix" "$balance_suffix")
我的平衡是111.12
,我想圓起來。
我試圖
balance1=$(substr "$result" "$balance_prefix" "$balance_suffix")
balance=$("%0.f\n" "$balance1")
或
balance1=$(substr "$result" "$balance_prefix" "$balance_suffix")
balance=$(ceil($balance1))
從一些答案都指向谷歌之後,我甚至不知道如果語法當然correct.And兩個例子返回空白。
任何提示或建議?謝謝。
編輯:
# Check that required words exists:
if [[ "$result" == *${balance_prefix}* ]] && \
[[ "$result" == *${balance_suffix}* ]]
then
# Get the balance and check it:
balance=$(substr "$result" "$balance_prefix" "$balance_suffix")
balance_low=0
if [ $(expr "$balance" + 1 2> /dev/null) ]; then
[ $balance -le $alert_balance ] && balance_low=1
else
echo "Error while parsing an integer: $balance"
fi
else
echo "Error while parsing the answer (balance): $result"
fi
來源:http://smstools3.kekekasvi.com/topic.php?id=320
你想把它整理成什麼?你能否添加一些更多的輸入/輸出示例以使其更清晰? – hendry 2012-02-01 07:02:47
@hendry感謝您的回覆。因爲在腳本中,如果我的餘額是十進制值,它不會進行任何比較(例如,如果餘額低於20,則發送警報消息)我在我的問題中粘貼更多代碼。 – 2012-02-01 07:05:21
我認爲這裏的代碼讓人感到分心到「四捨五入問題」。也許http://stackoverflow.com/a/2395601/4534解決了這個問題。 – hendry 2012-02-01 07:08:28