1 Star 0 Fork 0

danlansky/go-library

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
price.go 1.09 KB
一键复制 编辑 原始数据 按行查看 历史
zhangminghua 提交于 11个月前 . feat:基础工具包
package utils
import (
"github.com/shopspring/decimal"
"strconv"
)
//分转元
func Fen2YuanString(price int64) string {
d := decimal.New(1, 2) //分除以100得到元
result := decimal.NewFromInt(price).DivRound(d, 2).String()
return result
}
//分转元
func Fen2YuanInt64(price int64) int64 {
d := decimal.New(1, 2) //分除以100得到元
result := decimal.NewFromInt(price).DivRound(d, 2).IntPart()
return result
}
//元转分,乘以100后,保留整数部分
func StringYuan2FenInt64(price string) int64 {
float64Price, _ := strconv.ParseFloat(price, 64)
int64Price := Float64Yuan2FenInt64(float64Price)
return int64Price
}
//元转分,乘以100后,保留整数部分
func Float64Yuan2FenInt64(price float64) int64 {
d := decimal.New(1, 2) //分转元乘以100
df := decimal.NewFromFloat(price).Mul(d).IntPart()
//d1 := decimal.New(1, 0) //乘完之后,保留2为小数,需要这么一个中间参数
//如下是满足,当乘以100后,仍然有小数位,取四舍五入法后,再取整数部分
//dff := decimal.NewFromFloat(price).Mul(d).DivRound(d1, 0).IntPart()
return df
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/danlansky/go-library.git
git@gitee.com:danlansky/go-library.git
danlansky
go-library
go-library
v1.0.7

搜索帮助