mmath

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Zero = Decimal{/* contains filtered or unexported fields */}
	One  = Decimal{/* contains filtered or unexported fields */}
)

常量定义

Functions

func RandInt

func RandInt(min, max int) int

RandInt 返回闭区间 [min, max] 的随机整数;若 min > max 则会交换两者 RandInt(2,5) 可能返回 2,3,4,5

func RandIntN

func RandIntN(n int) int

RandIntN 返回 [0, n) 的随机整数;当 n <= 0 时返回 0 RandIntN(5) 可能返回 0,1,2,3,4(不会返回 5)。

Types

type Decimal

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

Decimal 封装了 shopspring/decimal 库的 Decimal 类型,提供更易用的接口

func ChgPct

func ChgPct(a, b Decimal) Decimal

((a-b)/b)*100,除数为零时返回 Zero

func ChgPctN

func ChgPctN(a, b Decimal, places int32) Decimal

((a-b)/b)*100 并保留指定小数位数,除数为零时返回 Zero

func Mean

func Mean(nums ...Decimal) Decimal

Mean 计算算术平均数,传入 0 个参数时返回 Zero

func NewFromFloat

func NewFromFloat(value float64) Decimal

NewFromFloat 从 float64 创建一个 Decimal 对象

func NewFromInt

func NewFromInt(value int64) Decimal

NewFromInt 从 int64 创建一个 Decimal 对象

func NewFromString

func NewFromString(value string) (Decimal, error)

NewFromString 从字符串创建一个 Decimal 对象

func Pct

func Pct(a, b Decimal) Decimal

(a/b)*100,除数为零时返回 Zero

func PctN

func PctN(a, b Decimal, places int32) Decimal

(a/b)*100 并保留指定小数位数,除数为零时返回 Zero

func Sum

func Sum(nums ...Decimal) Decimal

Sum 多数求和,传入 0 个参数时返回 Zero

func (Decimal) Abs

func (d Decimal) Abs() Decimal

Abs 返回绝对值

func (Decimal) Add

func (d Decimal) Add(other Decimal) Decimal

Add 加法运算

func (Decimal) Cmp

func (d Decimal) Cmp(other Decimal) int

Cmp 比较两个 Decimal 对象 返回值: -1 (小于), 0 (等于), 1 (大于)

func (Decimal) Div

func (d Decimal) Div(other Decimal) Decimal

Div 除法运算,除数为零时返回零值

func (Decimal) DivSafe

func (d Decimal) DivSafe(other Decimal) (Decimal, error)

DivSafe 除法,除数为零时返回错误

func (Decimal) Equal

func (d Decimal) Equal(other Decimal) bool

Equal 判断是否相等

func (Decimal) Float64

func (d Decimal) Float64() float64

Float64 将 Decimal 对象转换为 float64,自动保留 float64 支持的最大有效位数

func (Decimal) GreaterThan

func (d Decimal) GreaterThan(other Decimal) bool

GreaterThan 判断是否大于

func (Decimal) IntPart

func (d Decimal) IntPart() int64

IntPart 获取 Decimal 对象的整数部分

func (Decimal) IsNegative

func (d Decimal) IsNegative() bool

IsNegative 判断是否为负值

func (Decimal) IsPositive

func (d Decimal) IsPositive() bool

IsPositive 判断是否为正值

func (Decimal) IsZero

func (d Decimal) IsZero() bool

IsZero 判断是否为零值

func (Decimal) LessThan

func (d Decimal) LessThan(other Decimal) bool

LessThan 判断是否小于

func (Decimal) Mul

func (d Decimal) Mul(other Decimal) Decimal

Mul 乘法运算

func (Decimal) Neg

func (d Decimal) Neg() Decimal

Neg 返回取负值 正数变负数,负数变正数,0 的相反数仍是 0

func (Decimal) Precision

func (d Decimal) Precision() int

Precision 返回小数点后的有效位数(小数位数) 例如 12.345 -> 3, 100 -> 0

func (Decimal) Round

func (d Decimal) Round(places int32) Decimal

Round 四舍五入到指定小数位数

func (Decimal) String

func (d Decimal) String() string

String 返回 Decimal 对象的字符串表示

func (Decimal) Sub

func (d Decimal) Sub(other Decimal) Decimal

Sub 减法运算

func (Decimal) Truncate

func (d Decimal) Truncate(places int32) Decimal

Truncate 截断到指定小数位数(不四舍五入)

func (Decimal) Value

func (d Decimal) Value() decimal.Decimal

Value 返回底层 shopspring/decimal.Decimal(便于需要原生 API 的场景)

Jump to

Keyboard shortcuts

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