Arbitrary-precision fixed-point decimal numbers for Go, avoiding floating-point precision issues with support for arithmetic, rounding, serialization, and database integration.
Methods to convert Decimal to strings, native Go numeric types, and math/big types. Also includes methods to access internal components of a Decimal.
// String returns the string representation of the decimal with fixed point.
// Example: New(-12345, -3).String() => "-12.345"
func (d Decimal) String() string
// StringFixed returns a rounded fixed-point string with places digits after
// the decimal point (half-up rounding).
// Negative places rounds the integer part.
// Example: NewFromFloat(5.45).StringFixed(1) => "5.5"
func (d Decimal) StringFixed(places int32) string
// StringFixedBank returns a banker's-rounded fixed-point string with places
// digits after the decimal point.
// Example: NewFromFloat(5.45).StringFixedBank(1) => "5.4"
func (d Decimal) StringFixedBank(places int32) string
// StringFixedCash returns a cash-rounded fixed-point string.
// interval must be 5, 10, 25, 50, or 100.
func (d Decimal) StringFixedCash(interval uint8) string
// StringScaled scales the decimal then calls String().
// Deprecated: buggy and unintuitive. Use StringFixed instead.
func (d Decimal) StringScaled(exp int32) stringUsage:
d := decimal.NewFromFloat(1234.5678)
d.String() // "1234.5678"
d.StringFixed(2) // "1234.57"
d.StringFixed(0) // "1235"
d.StringFixed(6) // "1234.567800"
d2 := decimal.New(-12345, -3)
d2.String() // "-12.345"// Float64 returns the nearest float64 value for d and whether the conversion
// is exact.
// WARNING: conversion may be inexact for high-precision decimals.
func (d Decimal) Float64() (f float64, exact bool)
// InexactFloat64 returns the nearest float64 value for d.
// No indication of precision loss.
func (d Decimal) InexactFloat64() float64Usage:
d := decimal.NewFromFloat(1.23456789)
f, exact := d.Float64() // f = 1.23456789, exact = true or false
d2 := decimal.NewFromString("1.1234567890123456789")
f2, exact2 := d2.Float64() // exact2 = false (exceeds float64 precision)
f3 := d.InexactFloat64() // 1.23456789 (no exactness indication)// IntPart returns the integer component of the decimal as int64.
// IntPart of 1234.5678 is 1234.
// IntPart of -1234.5678 is -1234.
// WARNING: returns 0 if the integer part cannot fit in int64.
func (d Decimal) IntPart() int64Usage:
decimal.NewFromFloat(1234.5678).IntPart() // 1234
decimal.NewFromFloat(-1234.5678).IntPart() // -1234
decimal.NewFromFloat(0.99).IntPart() // 0// BigInt returns the integer component of the decimal as a *big.Int.
// Truncates fractional part.
func (d Decimal) BigInt() *big.IntUsage:
d := decimal.NewFromFloat(1234.5678)
bi := d.BigInt() // *big.Int with value 1234// BigFloat returns the decimal as a *big.Float.
// WARNING: casting decimal to BigFloat might cause a loss of precision.
func (d Decimal) BigFloat() *big.FloatUsage:
d := decimal.NewFromFloat(3.14159265)
bf := d.BigFloat() // *big.Float representation// Rat returns a rational number representation of the decimal as *big.Rat.
func (d Decimal) Rat() *big.RatUsage:
d := decimal.NewFromFloat(0.5)
rat := d.Rat() // big.Rat{1, 2}// Coefficient returns the coefficient (significand) of the decimal.
// The full value is: Coefficient() * 10^Exponent().
// Returns a new *big.Int.
func (d Decimal) Coefficient() *big.Int
// CoefficientInt64 returns the coefficient as int64.
// Result is undefined if the coefficient cannot be represented as int64.
func (d Decimal) CoefficientInt64() int64
// Exponent returns the exponent component of the decimal as int32.
// The full value is: Coefficient() * 10^Exponent().
func (d Decimal) Exponent() int32
// NumDigits returns the number of digits in the decimal value
// (not counting the sign or decimal point).
func (d Decimal) NumDigits() int
// Copy returns a copy of the decimal with the same value and exponent
// but a new (independent) *big.Int pointer.
func (d Decimal) Copy() DecimalUsage:
// New(-12345, -3) represents -12.345
d := decimal.New(-12345, -3)
d.Coefficient() // big.Int(-12345)
d.CoefficientInt64() // -12345
d.Exponent() // -3
d.String() // "-12.345"
d2 := decimal.NewFromFloat(123456789.123)
d2.NumDigits() // number of significant digits
// Copy for independent mutation safety
original := decimal.NewFromFloat(3.14)
copy := original.Copy()
// copy has same value but different internal pointerInstall with Tessl CLI
npx tessl i tessl/golang-github-com-shopspring--decimal