1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 08:32:40 +08:00

金额转换

This commit is contained in:
2022-03-27 10:19:33 +08:00
parent 176cd0575d
commit aff81b33cd
2 changed files with 76 additions and 0 deletions

29
currency/amountConvert.go Normal file
View File

@ -0,0 +1,29 @@
package currency
import (
"strconv"
"github.com/shopspring/decimal"
)
// @title 分转换为元
// @param cent 金额分
// @return 字符串表示的元
func CentToDollar(cent int32) string {
d := decimal.New(1, 2)
result := decimal.NewFromInt32(cent).DivRound(d, 2).StringFixedBank(2)
return result
}
// 元转换为分
func DollarToCent(dollar string) int64 {
p, _ := strconv.ParseFloat(dollar, 64)
d := decimal.New(1, 2)
df := decimal.NewFromFloat(p).Mul(d).IntPart()
return df
}