coin

package
v2.0.61 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const MoneyScale = 8

========================================== 常量与基数 ==========================================

Variables

View Source
var (
	MoneyBase = new(big.Int).Exp(big.NewInt(10), big.NewInt(MoneyScale), nil) // 10^8

)

Functions

func GetDefaultOutputDecimals

func GetDefaultOutputDecimals() int

GetDefaultOutputDecimals 获取默认输出小数位数

func SetDefaultOutputDecimals

func SetDefaultOutputDecimals(decimals int)

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

Types

type Money

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

========================================== Money 结构体 内部使用 big.Int 存储最小单位(固定 8 位小数) ==========================================

func FromInt

func FromInt(v *big.Int) Money

func FromInt64

func FromInt64(v int64) Money

func FromToken

func FromToken(token *big.Int, decimals uint8) Money

TokenAmount → Money decimals: ERC20 decimals

func MustParse

func MustParse(s string) Money

func One

func One() Money

func Parse

func Parse(s string) (Money, error)

func ParseMoney

func ParseMoney(s string) (Money, error)

ParseMoney 从字符串解析,例如 "123.45678901"

func Zero

func Zero() Money

========================================== 构造函数 ==========================================

func (Money) Abs

func (m Money) Abs() Money

Abs 绝对值

Abs returns the absolute value of x.

func (Money) Add

func (m Money) Add(x Money) Money

========================================== 基础运算 ==========================================

func (Money) Cmp

func (m Money) Cmp(x Money) int

Cmp 比较 Cmp compares x and y and returns:

-1 if x < y;

0 if x == y;

+1 if x > y.

func (Money) Div

func (m Money) Div(x Money) Money

func (Money) DivInt

func (m Money) DivInt(n int64) Money

func (Money) DivIntExact

func (m Money) DivIntExact(n int64) (Money, error)

DivIntExact 精确除法(必须整除)

如果除数不是 n 的倍数,则返回错误。

func (Money) FormatDecimals

func (m Money) FormatDecimals(decimals int) string

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

func (Money) GormDBDataType

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

========================================== GORM / SQL ==========================================

func (Money) Greater

func (m Money) Greater(x Money) bool

Greater 大于

func (Money) GreaterThanOrEqual

func (m Money) GreaterThanOrEqual(o Money) bool

GreaterThanOrEqual 大于等于

func (Money) Int

func (m Money) Int() *big.Int

func (Money) IsNegative

func (m Money) IsNegative() bool

IsNegative 是否为负数

func (Money) IsZero

func (m Money) IsZero() bool

IsZero 是否为0

func (Money) Less

func (m Money) Less(x Money) bool

Less 小于

func (Money) LessThanOrEqual

func (m Money) LessThanOrEqual(o Money) bool

LessThanOrEqual 小于等于

func (Money) MarshalJSON

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

========================================== JSON ==========================================

func (Money) MarshalJSONWithDecimals

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

MarshalJSONWithDecimals 按指定小数位序列化

func (Money) Mul

func (m Money) Mul(x Money) Money

Mul 乘法

func (Money) MulInt

func (m Money) MulInt(n int64) Money

MulInt 整数乘法

func (Money) MulRate

func (m Money) MulRate(rate *big.Int) Money

MulRate 基于 MoneyBase 的比例乘法 rate 是 *1e8 表示的小数(如 0.002 = 200000)

func (Money) MulRatio

func (m Money) MulRatio(numerator, denominator *big.Int) Money

MulRatio 向下截断的比例乘法 等价于:m * numerator / denominator

func (Money) MulRatioFloor

func (m Money) MulRatioFloor(
	numerator,
	denominator *big.Int,
) Money

MulRatioFloor 返回 floor(m * numerator / denominator)

func (*Money) Scan

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

func (Money) String

func (m Money) String() string

========================================== 输出(支持小数位配置) ==========================================

func (Money) StringFull

func (m Money) StringFull() string

StringFull 输出完整 8 位小数

func (Money) Sub

func (m Money) Sub(x Money) Money

func (Money) ToToken

func (m Money) ToToken(decimals uint8) (*big.Int, error)

Money → TokenAmount

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