vote-admin-api/utils/compute.go
2024-08-30 11:19:25 +08:00

65 lines
1.1 KiB
Go

package utils
import (
"time"
)
// 一些计算
// ComputeIndividualIncomeTax 计算个人所得税
func ComputeIndividualIncomeTax(income float64) float64 {
if income <= 800 {
return 0
}
if income <= 4000 {
income = income - 800
}
// 实际纳税金额
if income > 4000 {
income = income * 0.8
}
// 税率、速算扣除数
var taxRate, quickDeduction float64
if income <= 20000 {
taxRate = 0.2
quickDeduction = 0
} else if income <= 50000 {
taxRate = 0.3
quickDeduction = 2000
} else {
taxRate = 0.4
quickDeduction = 7000
}
incomeTax := income*taxRate - quickDeduction
return incomeTax
}
// CalculateAge 计算年龄
func CalculateAge(birthdate string) (int, error) {
// 解析出生日期字符串
layout := "2006-01-02"
birthTime, err := time.Parse(layout, birthdate)
if err != nil {
return 0, err
}
// 获取当前时间
now := time.Now()
// 计算年龄
years := now.Year() - birthTime.Year()
// 如果今年的生日还没到,年龄减一
if now.Month() < birthTime.Month() || (now.Month() == birthTime.Month() && now.Day() < birthTime.Day()) {
years--
}
return years, nil
}