linux - 壳牌支付脚本

标签 linux

我目前正在学习 Linux 编程,我们被要求制作一个付费系统。

最佳答案

通常建议以美分进行财务计算,以避免出现浮点错误。

您的脚本是一个很好的开始。我冒昧地对它进行了一些润色:

#!/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

关于linux - 壳牌支付脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22542465/

相关文章:

linux - 检查文件名中的点并将其删除(在 Linux 上)

linux - Aptana studio 3在Windows和Linux之间共享工作区

linux - xargs sh -c 跳过第一个参数

linux - 更改文件时间(触摸)

linux - 如何测试 TCP 写入超时的处理?

linux - Shell Scripting ssh 执行命令

linux - 通过在 bash 中附加来有条件地合并文件

linux - 什么是 Linux 中的长格式?

c++ - 在 C++ 中录制时从设备获取音量

c - 当我需要在 linux 下解析一个简单的配置文件时使用什么 c 库?