项目地址:https://github.com/shopspring/decimal

Variables

  1. // 除法精度是指当除法不精确时,结果的小数位数
  2. var DivisionPrecision = 16
  3. d1 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(3))
  4. d1.String() // output: "0.6666666666666667"
  5. d2 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(30000))
  6. d2.String() // output: "0.0000666666666667"
  7. d3 := decimal.NewFromFloat(20000).Div(decimal.NewFromFloat(3))
  8. d3.String() // output: "6666.6666666666666667"
  9. // 自定义精度
  10. decimal.DivisionPrecision = 3
  11. d4 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(3))
  12. d4.String() // output: "0.667"
  1. //如果希望将小数作为数字而不是字符串编组,MarshalJSONWithoutQuotes应该设置为true。
  2. //警告:对于多位数的小数,这是危险的,因为许多JSON解组器(例如:Javascript的)会将JSON数字解组
  3. //为IEEE 754双精度浮点数,这意味着您可能会悄悄地失去精度。
  4. var MarshalJSONWithoutQuotes = false
  1. // 零常数,使计算更快。0不能直接与==或!=比较,请使用小数。等于或小数
  2. var Zero = New(0, 1)

type Decimal

Decimal表示定点小数。它是不可变的。number = value * 10 ^ exp

  1. type Decimal struct {
  2. // contains filtered or unexported fields
  3. }

初始化

func New(value int64, exp int32) Decimal 返回定点小数,value = value 10 ^ exp
func NewFromBigInt(value
big.Int, exp int32) Decimal 返回定点小数,value = value * 10 ^ exp
func NewFromFloat(value float64) Decimal 将float64转为decimal,通常保留15位小数

  1. fmt.Println(NewFromFloat(123.123123123123).String())
  2. fmt.Println(NewFromFloat(.123123123123123).String())
  3. fmt.Println(NewFromFloat(-1e13).String())
  4. 123.123123123123
  5. 0.123123123123123
  6. -10000000000000

func NewFromFloat32(value float32) Decimal 将float32转为decimal,通常保留6-8位小数

  1. fmt.Println(NewFromFloat32(123.123123123123).String())
  2. fmt.Println(NewFromFloat32(.123123123123123).String())
  3. fmt.Println(NewFromFloat32(-1e13).String())

func NewFromFloatWithExponent(value float64, exp int32) Decimal 保留指定小数位,四舍五入
func NewFromInt(value int64) Decimal
func NewFromInt32(value int32) Decimal
func NewFromString(value string) (Decimal, error) 要求精度高的,一般将其存为string,再用string处理
func RequireFromString(value string) Decimal 将string转为decimal,转换不了会panic

运算

func (d Decimal) Add(d2 Decimal) Decimal
func (d Decimal) Sub(d2 Decimal) Decimal
func Sum(first Decimal, rest …Decimal) Decimal 总和
func (d Decimal) Mod(d2 Decimal) Decimal 取模
func (d Decimal) Mul(d2 Decimal) Decimal 乘法
func (d Decimal) Neg() Decimal 取负数
func (d Decimal) Pow(d2 Decimal) Decimal pow
func (d Decimal) QuoRem(d2 Decimal, precision int32) (Decimal, Decimal) 返回商和余数
func (d Decimal) Abs() Decimal 绝对值
func (d Decimal) Atan() Decimal 反正切
func (d Decimal) BigFloat() big.Float 返回BigFloat。请注意,将decimal转换为BigFloat可能会导致精度下降
func (d Decimal) BigInt()
big.Int 返回decimal的整数部分
func (d Decimal) Cmp(d2 Decimal) int 系数返回小数点的系数。乘以10的指数
func (d Decimal) Cos() Decimal 回弧度参数x的余弦
func (d Decimal) Div(d2 Decimal) Decimal 除法
func (d Decimal) DivRound(d2 Decimal, precision int32) Decimal 除法,指定精度
func (d Decimal) Equal(d2 Decimal) bool 比较相等
func (d Decimal) Exponent() int32 返回指数或小数位数

  1. func main() {
  2. d1 := decimal.New(2, 5)
  3. fmt.Println(d1.Exponent()) // 5
  4. d1, _ := decimal.NewFromString("1.12341111111111111111111111111")
  5. fmt.Println(d1.Exponent()) // -29
  6. d1, _ := decimal.NewFromFloat("1.12341111111111111111111111111")
  7. fmt.Println(d1.Exponent()) // -16
  8. }

func (d Decimal) Float64() (f float64, exact bool) 返回float64,并返回是否丢失精度
func (d Decimal) Floor() Decimal 地板
func (d Decimal) Ceil() Decimal 天花板
func (d Decimal) GreaterThan(d2 Decimal) bool 是否大于
func (d Decimal) GreaterThanOrEqual(d2 Decimal) bool 是否大于等于
func (d Decimal) LessThan(d2 Decimal) bool 小于
func (d Decimal) LessThanOrEqual(d2 Decimal) bool 小于等于
func (d Decimal) IntPart() int64 返回整数部分
func (d Decimal) IsNegative() bool 是否小于0

  1. true if d < 0
  2. false if d == 0
  3. false if d > 0

func (d Decimal) IsPositive() bool 是否小于0
func (d Decimal) IsZero() bool 是否等于0
func (d Decimal) Rat() *big.Rat 返回decimal的big.Rat
func (d Decimal) Round(places int32) Decimal 把小数点取四舍五入。如果位数< 0,它将整数部分四舍五入到最近的10^(-位数)

  1. NewFromFloat(5.45).Round(1).String() // output: "5.5"
  2. NewFromFloat(545).Round(-1).String() // output: "550"

func (d Decimal) RoundBank(places int32) Decimal RoundBank将小数点四舍五入。如果要舍入的最后一位数字与最接近的两个整数的距离相等,则取舍入的值为偶数 如果位数< 0,它将整数部分四舍五入到最近的10^(-位数)

  1. NewFromFloat(5.45).Round(1).String() // output: "5.4"
  2. NewFromFloat(545).Round(-1).String() // output: "540"
  3. NewFromFloat(5.46).Round(1).String() // output: "5.5"
  4. NewFromFloat(546).Round(-1).String() // output: "550"
  5. NewFromFloat(5.55).Round(1).String() // output: "5.6"
  6. NewFromFloat(555).Round(-1).String() // output: "560"

func (d Decimal) Sin() Decimal 正弦值
func (d Decimal) String() string 返回带有定点的小数点的字符串表示形式
func (d Decimal) StringFixed(places int32) string 返回一个四舍五入的定点字符串,小数点后有数位

  1. NewFromFloat(0).StringFixed(2) // output: "0.00"
  2. NewFromFloat(0).StringFixed(0) // output: "0"
  3. NewFromFloat(5.45).StringFixed(0) // output: "5"
  4. NewFromFloat(5.45).StringFixed(1) // output: "5.5"
  5. NewFromFloat(5.45).StringFixed(2) // output: "5.45"
  6. NewFromFloat(5.45).StringFixed(3) // output: "5.450"
  7. NewFromFloat(545).StringFixed(-1) // output: "550"

func (d Decimal) StringFixedBank(places int32) string 返回银行家四舍五入的定点字符串,小数点后有数位

  1. NewFromFloat(0).StringFixedBank(2) // output: "0.00"
  2. NewFromFloat(0).StringFixedBank(0) // output: "0"
  3. NewFromFloat(5.45).StringFixedBank(0) // output: "5"
  4. NewFromFloat(5.45).StringFixedBank(1) // output: "5.4"
  5. NewFromFloat(5.45).StringFixedBank(2) // output: "5.45"
  6. NewFromFloat(5.45).StringFixedBank(3) // output: "5.450"
  7. NewFromFloat(545).StringFixedBank(-1) // output: "540"

func (d Decimal) Tan() Decimal 正切
func (d Decimal) Truncate(precision int32) Decimal 截断,不做四舍五入操作

实现的接口

func (d Decimal) MarshalBinary() (data []byte, err error) MarshalBinary实现了编码。BinaryMarshaler接口
func (d Decimal) MarshalJSON() ([]byte, error) MarshalJSON实现了json.Marshaler
func (d Decimal) MarshalText() (text []byte, err error) 实现了encoding.TextMarshaler interface
func (d Decimal) UnmarshalBinary(data []byte) error
func (d
Decimal) UnmarshalJSON(decimalBytes []byte) error
func (d Decimal) UnmarshalText(text []byte) error
func (d Decimal) Value() (driver.Value, error) implements the driver.Valuer interface
func (d
Decimal) Scan(value interface{}) error implements the sql.Scanner interface

type NullDecimal

NullDecimal表示一个可为空的小数,它与从数据库扫描空值的兼容性一致

  1. type NullDecimal struct {
  2. Decimal Decimal
  3. Valid bool
  4. }

func (d NullDecimal) MarshalJSON() ([]byte, error)
func (d NullDecimal) Scan(value interface{}) error
func (d
NullDecimal) UnmarshalJSON(decimalBytes []byte) error
func (d NullDecimal) Value() (driver.Value, error)