1 Star 1 Fork 0

zhuyuns / basic

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
yuan.go 2.15 KB
一键复制 编辑 原始数据 按行查看 历史
wing 提交于 2021-12-18 15:19 . 修改基础包依赖
package utils
import (
"bufio"
"github.com/leekchan/accounting"
"github.com/shopspring/decimal"
"io/ioutil"
"math/big"
"os"
)
func FenToYuanFormat(price int64) string {
if price <= 0 {
return "0.00"
}
ac := accounting.Accounting{Symbol: "", Precision: 2}
return ac.FormatMoney(FenToYuanStr(price))
}
func FenToYuanStr(amount int64) string {
amountObj := decimal.NewFromBigInt(big.NewInt(amount), 0)
quantity := decimal.NewFromFloat(100)
amountYuan := amountObj.Div(quantity).StringFixed(2)
return amountYuan
}
func FenToYuanFloat(amount int64) float64 {
amountObj := decimal.NewFromBigInt(big.NewInt(amount), 0)
quantity := decimal.NewFromFloat(100)
amountYuan, _ := amountObj.Div(quantity).Float64()
return amountYuan
}
func YuanToFenStr(amount string) (int64, error) {
amountObj, err := decimal.NewFromString(amount)
if err != nil {
return 0, err
}
quantity := decimal.NewFromFloat(100)
amountFen := amountObj.Mul(quantity).IntPart()
return amountFen, nil
}
func YuanToFenFloat(amount float64) int64 {
amountObj := decimal.NewFromFloat(amount)
quantity := decimal.NewFromFloat(100)
amountFen := amountObj.Mul(quantity).IntPart()
return amountFen
}
func FloatMul100Str(str string) string {
amountObj, _ := decimal.NewFromString(str)
quantity := decimal.NewFromFloat(100)
return amountObj.Mul(quantity).StringFixed(2)
}
func FloatMul100(amount float64) float64 {
amountObj := decimal.NewFromFloat(amount)
quantity := decimal.NewFromFloat(100)
floatMul100, _ := amountObj.Mul(quantity).Float64()
return floatMul100
}
func FloatDiv100(amount float64) float64 {
amountObj := decimal.NewFromFloat(amount)
quantity := decimal.NewFromFloat(100)
floatDiv100, _ := amountObj.Div(quantity).Float64()
return floatDiv100
}
func FloatDiv100Str(amount float64) string {
amountObj := decimal.NewFromFloat(amount)
quantity := decimal.NewFromFloat(100)
return amountObj.Div(quantity).StringScaled(2)
}
//文件读取
func ReadFile(fileName string) ([]byte, error) {
f, err := os.Open(fileName)
if err != nil {
return nil, err
}
defer f.Close()
result, err := ioutil.ReadAll(bufio.NewReader(f))
if err != nil {
return nil, err
}
return result, nil
}
Go
1
https://gitee.com/zhuyuns/basic.git
git@gitee.com:zhuyuns/basic.git
zhuyuns
basic
basic
v1.0.3

搜索帮助