1 Star 0 Fork 0

技术狼/go-amount-decimal

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
amountdecimal_type_json_number.go 1.52 KB
一键复制 编辑 原始数据 按行查看 历史
技术狼 提交于 2023-12-29 10:42 . no message
package amountdecimal
import (
"encoding/json"
"errors"
"fmt"
)
func NewJsonNumber(amount json.Number) *AmountDecimal {
return NewString(amount.String())
}
// @title: addition json.Number type
// @auth: jishulang.com
// @date: 2022/7/30 20:59
func (c *AmountDecimal) AddJsonNumber(amounts ...json.Number) *AmountDecimal {
return c.amountsJsonNumber(add, amounts...)
}
// @title: subtraction json.Number type
// @auth: jishulang.com
// @date: 2022/7/30 20:59
func (c *AmountDecimal) SubJsonNumber(amounts ...json.Number) *AmountDecimal {
return c.amountsJsonNumber(sub, amounts...)
}
// @title: multiplication json.Number type
// @auth: jishulang.com
// @date: 2022/7/30 20:59
func (c *AmountDecimal) MulJsonNumber(amounts ...json.Number) *AmountDecimal {
return c.amountsJsonNumber(mul, amounts...)
}
// @title: division json.Number type
// @auth: jishulang.com
// @date: 2022/7/30 20:59
func (c *AmountDecimal) DivJsonNumber(amounts ...json.Number) *AmountDecimal {
return c.amountsJsonNumber(div, amounts...)
}
func (c *AmountDecimal) amountsJsonNumber(f uint8, amounts ...json.Number) *AmountDecimal {
if c.err != nil {
return c
}
if len(amounts) == 0 {
c.err = errors.New(errCodeMap[amounts_empty])
return c
}
var amounts2 []string
amountStr := ""
for _, amount := range amounts {
if f == div && amount == json.Number(0) {
c.err = errors.New(errCodeMap[amount_divisor_zero])
return c
}
amountStr = fmt.Sprintf("%v", amount)
amounts2 = append(amounts2, amountStr)
}
return c.amountsString(f, amounts2...)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/jishulangcom/go-amount-decimal.git
git@gitee.com:jishulangcom/go-amount-decimal.git
jishulangcom
go-amount-decimal
go-amount-decimal
v0.0.1

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385