1 Star 0 Fork 1

go-genie/sqlx

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
currency.go 3.50 KB
一键复制 编辑 原始数据 按行查看 历史
文兄 提交于 2025-05-26 17:36 +08:00 . init
package datatypes
import (
"database/sql/driver"
"fmt"
"github.com/shopspring/decimal"
)
// openapi:strfmt decimal
type Currency decimal.Decimal
func NewCurrency(val any) (Currency, error) {
return parseCurrency(val)
}
func parseCurrency(src any) (Currency, error) {
switch src := src.(type) {
case float64:
return Currency(decimal.NewFromFloat(src)), nil
case float32:
return Currency(decimal.NewFromFloat32(src)), nil
case int64:
return Currency(decimal.NewFromInt(src)), nil
case int32:
return Currency(decimal.NewFromInt(int64(src))), nil
case int:
return Currency(decimal.NewFromInt(int64(src))), nil
case uint64:
return Currency(decimal.NewFromInt(int64(src))), nil
case uint32:
return Currency(decimal.NewFromInt(int64(src))), nil
case uint:
return Currency(decimal.NewFromInt(int64(src))), nil
case string:
d, err := decimal.NewFromString(src)
if err != nil {
return Currency{}, err
}
return Currency(d), nil
case []uint8:
d, err := decimal.NewFromString(string(src))
if err != nil {
return Currency{}, err
}
return Currency(d), nil
case nil:
return Currency(decimal.NewFromInt(0)), nil
default:
return Currency{}, fmt.Errorf("cannot sql.Scan() strfmt.Currency from: %#v", src)
}
}
func (c Currency) String() string {
return decimal.Decimal(c).String()
}
func (c *Currency) UnmarshalText(text []byte) error {
d, err := decimal.NewFromString(string(text))
if err != nil {
return err
}
*c = Currency(d)
return nil
}
func (c Currency) MarshalText() (text []byte, err error) {
return []byte(decimal.Decimal(c).String()), nil
}
func (c *Currency) Scan(src any) error {
d, err := parseCurrency(src)
if err != nil {
return err
}
*c = d
return nil
}
func (c Currency) Value() (driver.Value, error) {
return decimal.Decimal(c).String(), nil
}
func (Currency) DataType(driverName string) string {
return "decimal(24,6)"
}
// Abs returns the absolute value of the decimal.
func (d Currency) Abs() Currency {
return Currency(decimal.Decimal(d).Abs())
}
// Add returns d + d2.
func (d Currency) Add(d2 Currency) Currency {
return Currency(decimal.Decimal(d).Add(decimal.Decimal(d2)))
}
// Sub returns d - d2.
func (d Currency) Sub(d2 Currency) Currency {
return Currency(decimal.Decimal(d).Sub(decimal.Decimal(d2)))
}
// Neg returns -d.
func (d Currency) Neg() Currency {
return Currency(decimal.Decimal(d).Neg())
}
// Mul returns d * d2.
func (d Currency) Mul(d2 Currency) Currency {
return Currency(decimal.Decimal(d).Mul(decimal.Decimal(d2)))
}
// Div returns d / d2. If it doesn't divide exactly, the result will have
// DivisionPrecision digits after the decimal point.
func (d Currency) Div(d2 Currency) Currency {
return Currency(decimal.Decimal(d).Div(decimal.Decimal(d2)))
}
// Mod returns d % d2.
func (d Currency) Mod(d2 Currency) Currency {
return Currency(decimal.Decimal(d).Mod(decimal.Decimal(d2)))
}
// Round rounds the decimal to places decimal places.
// If places < 0, it will round the integer part to the nearest 10^(-places).
//
// Example:
//
// NewFromFloat(5.45).Round(1).String() // output: "5.5"
// NewFromFloat(545).Round(-1).String() // output: "550"
func (d Currency) Round(places int32) Currency {
return Currency(decimal.Decimal(d).Round(places))
}
// Floor returns the nearest integer value less than or equal to d.
func (d Currency) Floor() Currency {
return Currency(decimal.Decimal(d).Floor())
}
// Ceil returns the nearest integer value greater than or equal to d.
func (d Currency) Ceil() Currency {
return Currency(decimal.Decimal(d).Ceil())
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/go-genie/sqlx.git
git@gitee.com:go-genie/sqlx.git
go-genie
sqlx
sqlx
v1.1.3

搜索帮助