CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/golang-github-com-shopspring--decimal

Arbitrary-precision fixed-point decimal numbers for Go, avoiding floating-point precision issues with support for arithmetic, rounding, serialization, and database integration.

Overview
Eval results
Files

constructors.mddocs/api/

Constructors

Functions for creating Decimal values from various input types. The most reliable way to create a Decimal is from a string via NewFromString or RequireFromString.

Capabilities

From String

// NewFromString returns a new Decimal from a string representation.
// Trailing zeroes are not trimmed.
// Returns error for invalid string.
func NewFromString(value string) (Decimal, error)

// RequireFromString returns a new Decimal from a string or panics if parsing fails.
func RequireFromString(value string) Decimal

Supported formats: integers, decimals, scientific notation (e.g., "1.5e-3").

Usage:

d, err := decimal.NewFromString("-123.45")    // d = -123.45
d2, err := decimal.NewFromString(".0001")     // d2 = 0.0001
d3, err := decimal.NewFromString("1.47000")  // d3 = 1.47000 (trailing zeroes preserved)
d4, err := decimal.NewFromString("1.5e-3")   // d4 = 0.0015

// Panic on error:
d5 := decimal.RequireFromString("99.99")

From Formatted String

// NewFromFormattedString returns a new Decimal from a formatted string, stripping
// all characters matching replRegexp before parsing.
func NewFromFormattedString(value string, replRegexp *regexp.Regexp) (Decimal, error)

Usage:

import "regexp"

r := regexp.MustCompile(`[$,]`)
d1, err := decimal.NewFromFormattedString("$5,125.99", r) // d1 = 5125.99

r2 := regexp.MustCompile(`[_]`)
d2, err := decimal.NewFromFormattedString("1_000_000", r2) // d2 = 1000000

r3 := regexp.MustCompile(`[USD\s]`)
d3, err := decimal.NewFromFormattedString("5000 USD", r3)  // d3 = 5000

From Integer

// New returns a new Decimal: value * 10^exp.
func New(value int64, exp int32) Decimal

// NewFromInt converts an int64 to Decimal.
func NewFromInt(value int64) Decimal

// NewFromInt32 converts an int32 to Decimal.
func NewFromInt32(value int32) Decimal

// NewFromUint64 converts a uint64 to Decimal.
func NewFromUint64(value uint64) Decimal

Usage:

decimal.New(12345, -3)     // 12.345  (12345 * 10^-3)
decimal.New(-12345, -3)    // -12.345
decimal.New(1, 6)          // 1000000 (1 * 10^6)
decimal.NewFromInt(123)    // 123
decimal.NewFromInt(-10)    // -10
decimal.NewFromInt32(42)   // 42
decimal.NewFromUint64(123) // 123

From Float

// NewFromFloat converts a float64 to Decimal with reliable roundtrip precision
// (typically 15 significant digits).
// NOTE: panics on NaN, +Inf, or -Inf.
func NewFromFloat(value float64) Decimal

// NewFromFloat32 converts a float32 to Decimal with reliable roundtrip precision
// (typically 6-8 significant digits).
// NOTE: panics on NaN, +Inf, or -Inf.
func NewFromFloat32(value float32) Decimal

// NewFromFloatWithExponent converts a float64 to Decimal with specified exponent precision.
// The result is rounded to exp decimal places.
func NewFromFloatWithExponent(value float64, exp int32) Decimal

Usage:

decimal.NewFromFloat(123.456)                         // 123.456
decimal.NewFromFloat32(float32(123.456))              // ~123.456 (less precise)
decimal.NewFromFloatWithExponent(123.456, -2)         // 123.46 (rounded to 2 places)
decimal.NewFromFloatWithExponent(123.456789, -4)      // 123.4568 (rounded to 4 places)

Note: Prefer NewFromString for exact values; float conversion may introduce representation imprecision.

From big.Int

// NewFromBigInt returns a new Decimal from a *big.Int: value * 10^exp.
func NewFromBigInt(value *big.Int, exp int32) Decimal

Usage:

import "math/big"

bigVal := big.NewInt(123456789)
d := decimal.NewFromBigInt(bigVal, -3) // 123456.789

From big.Rat

// NewFromBigRat returns a new Decimal from a *big.Rat rounded to given precision.
// Numerator and denominator are divided and rounded to the specified number of
// decimal places.
func NewFromBigRat(value *big.Rat, precision int32) Decimal

Usage:

import "math/big"

decimal.NewFromBigRat(big.NewRat(0, 1), 0)    // "0"
decimal.NewFromBigRat(big.NewRat(4, 5), 1)    // "0.8"
decimal.NewFromBigRat(big.NewRat(1000, 3), 3) // "333.333"
decimal.NewFromBigRat(big.NewRat(2, 7), 4)    // "0.2857"

Install with Tessl CLI

npx tessl i tessl/golang-github-com-shopspring--decimal

docs

api

arithmetic.md

comparison.md

configuration.md

constructors.md

conversion.md

null-decimal.md

rounding.md

serialization.md

index.md

README.md

tile.json