代码拉取完成,页面将自动刷新
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())
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。