coins

package
v3.1.32 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 21, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MoneyScale = 8
	MoneyBase  = int64(100000000)
)

Variables

View Source
var ZeroMoney = Money{}

Functions

func GetDefaultJSONDecimals

func GetDefaultJSONDecimals() int

GetDefaultJSONDecimals 获取默认 JSON 输出小数位数

func SetDefaultJSONDecimals

func SetDefaultJSONDecimals(decimals int)

SetDefaultJSONDecimals 设置默认 JSON 输出小数位数 decimals: 0 表示输出所有非零小数位,1-8 表示保留指定位数

Types

type Money

type Money struct {
	// contains filtered or unexported fields
}

func FromInt64

func FromInt64(v int64) Money

func MustParse

func MustParse[T moneyInput](v T) Money

func New added in v3.1.22

func New[T moneyInput](v T) Money

New constructs Money from whole or decimal coin units. Use FromInt64 when the value is already scaled by MoneyBase.

func Parse

func Parse[T moneyInput](v T) (Money, error)

func Zero

func Zero() Money

func (Money) Abs

func (m Money) Abs() Money

func (Money) Add

func (m Money) Add(x Money) Money

func (Money) Cmp

func (m Money) Cmp(x Money) int

func (Money) DivInt

func (m Money) DivInt(n int64) Money

func (Money) Float64

func (m Money) Float64() float64

func (Money) FormatDecimals

func (m Money) FormatDecimals(decimals int) string

FormatDecimals 按指定小数位格式化 decimals: 0 表示输出所有非零小数位,1-8 表示保留指定位数

func (Money) Ge added in v3.1.22

func (m Money) Ge(x Money) bool

Ge 基于 Cmp 实现的 >=

func (Money) GormDBDataType

func (Money) GormDBDataType(db *gorm.DB, field *schema.Field) string

func (Money) GormDataType

func (Money) GormDataType() string

func (Money) Greater

func (m Money) Greater(x Money) bool

Greater 大于

func (Money) Int64

func (m Money) Int64() int64

func (Money) IsNegative

func (m Money) IsNegative() bool

func (Money) IsZero

func (m Money) IsZero() bool

func (Money) Le added in v3.1.22

func (m Money) Le(x Money) bool

Le 基于 Cmp 实现的 <=

func (Money) Less

func (m Money) Less(x Money) bool

Less 小于

func (Money) MarshalJSON

func (m Money) MarshalJSON() ([]byte, error)

func (Money) MarshalJSONWithDecimals

func (m Money) MarshalJSONWithDecimals(decimals int) ([]byte, error)

func (Money) MulInt

func (m Money) MulInt(n int64) Money

func (Money) MulRate

func (m Money) MulRate(rate int64) Money

rate = 1e8 表示小数 例如 0.002 -> 200000

func (Money) MulRatio

func (m Money) MulRatio(numerator, denominator int64) Money

floor(m * numerator / denominator)

func (Money) SafeAdd

func (m Money) SafeAdd(x Money) (Money, error)

func (*Money) Scan

func (m *Money) Scan(value any) error

func (Money) String

func (m Money) String() string

func (Money) StringFixed

func (m Money) StringFixed(decimals int) string

func (Money) Sub

func (m Money) Sub(x Money) Money

func (*Money) UnmarshalJSON

func (m *Money) UnmarshalJSON(data []byte) error

func (Money) Value

func (m Money) Value() (driver.Value, error)

type MoneyJSON

type MoneyJSON struct {
	Money
	Decimals int
}

MoneyJSON 用于临时指定小数位的包装器

func (MoneyJSON) MarshalJSON

func (mj MoneyJSON) MarshalJSON() ([]byte, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL