셸 스크립트에서 완전히 새로 워서 정수를 반올림하는 방법에 대한 질문이 있습니다. 여기 셸 스크립트에서 정수를 반올림하십시오.
는 ㅋㅋ하여 스크립트 코드의 라인 (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
@hendry 답장을 보내 주셔서 감사합니다. 스크립트에서 내 잔액이 10 진수 값인 경우 (예 : 잔액이 20보다 작 으면 경고 메시지를 보냅니다.) 내 질문에 더 많은 코드를 붙여 넣습니다. –
나는이 코드가 "문제를 반올림"하는 것을 혼란스럽게한다고 생각한다. 아마도 http://stackoverflow.com/a/2395601/4534가 문제를 해결합니다. – hendry