我目前正在学习Linux编程,我们被要求制作付费系统。
答案 0 :(得分:3)
通常建议以美分进行财务计算,以避免出现浮点错误。
您的脚本是一个很好的开始。我冒昧地抛光了一下:
#!/bin/bash
declare -i hrs=1
declare -i rate
declare -i grossCents netCents socSecAmtCents
socSecRate=5
currency="£"
function displayAmt {
printf "%s%d.%02d\n" "$currency" $(($1 / 100)) $(($1 % 100))
}
while :; do
read -p "Enter number of hours worked: " hrs
(( hrs == 0 )) && break
read -p "Please enter the pay rate per hour: " rate
((
grossCents = hrs * rate * 100,
socSecAmtCents = grossCents * socSecRate / 100,
netCents = grossCents - socSecAmtCents
))
printf "Payable Amount before Social Security: %10s\n" "$(displayAmt $grossCents)"
printf "Total Social Security Deducted: %10s\n" "$(displayAmt $socSecAmtCents)"
printf "Payable Amount after Social Security: %10s\n" "$(displayAmt $netCents)"
echo
done
$ bash pay.sh
Enter number of hours worked: 123
Please enter the pay rate per hour: 19
Payable Amount before Social Security: £2337.00
Total Social Security Deducted: £116.85
Payable Amount after Social Security: £2220.15
Enter number of hours worked: q