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

fen and yuan

This commit is contained in:
2022-03-29 23:17:37 +08:00
parent 1c4f84d765
commit a1f37b9051
2 changed files with 56 additions and 1 deletions

View File

@ -4,6 +4,7 @@ import (
"strconv"
"github.com/shopspring/decimal"
"golang.org/x/exp/constraints"
)
// @title 分转换为元
@ -19,7 +20,6 @@ func CentToDollar(cent int32) string {
// 元转换为分
func DollarToCent(dollar string) int64 {
p, _ := strconv.ParseFloat(dollar, 64)
d := decimal.New(1, 2)
@ -27,3 +27,16 @@ func DollarToCent(dollar string) int64 {
return df
}
func FenToYuan[T constraints.Integer](v T) string {
d := decimal.New(1, 2)
return decimal.NewFromInt(int64(v)).DivRound(d, 2).StringFixedBank(2)
}
func YuanToFen(yuan string) int64 {
p, _ := decimal.NewFromString(yuan)
d := decimal.New(1, 2)
return p.Mul(d).IntPart()
}