Skip to main content

Decimal

The decimal package provides a re-exported shopspring/decimal interface with additional convenience constructors and constants for arbitrary-precision decimal arithmetic.

Type

import "github.com/coldsmirk/vef-framework-go/decimal"

// decimal.Decimal is an alias for shopspring/decimal.Decimal
var price decimal.Decimal

Constants

Pre-defined decimal values for common arithmetic:

decimal.Zero   // 0
decimal.One // 1
decimal.Two // 2
decimal.Three // 3
decimal.Four // 4
decimal.Five // 5
decimal.Six // 6
decimal.Seven // 7
decimal.Eight // 8
decimal.Nine // 9
decimal.Ten // 10

Constructors

All standard shopspring/decimal constructors are re-exported:

decimal.New(value, exp)                   // Raw constructor
decimal.NewFromInt(42) // From int64
decimal.NewFromInt32(42) // From int32
decimal.NewFromUint64(42) // From uint64
decimal.NewFromFloat(3.14) // From float64
decimal.NewFromFloat32(3.14) // From float32
decimal.NewFromFloatWithExponent(3.14, -2) // With specific exponent
decimal.NewFromBigInt(bigInt, exp) // From *big.Int
decimal.NewFromBigRat(bigRat, precision) // From *big.Rat
decimal.NewFromString("123.45") // From string (returns error)
decimal.NewFromFormattedString("1,234.56", ",") // From formatted string
decimal.RequireFromString("123.45") // From string (panics on error)

NewFromAny — Universal Converter

Convert any Go value to a Decimal:

d, err := decimal.NewFromAny(value)

Supported input types:

TypeBehavior
Decimal, *DecimalDirect pass-through
int, int8int64Integer conversion
uint, uint8uint64Unsigned integer conversion
float32, float64Float conversion
string, []byteString parsing
booltrue → 1, false → 0
fmt.StringerUses .String() method

Panic variant for when you know the conversion will succeed:

d := decimal.MustFromAny(value) // Panics on error

Utility Functions

decimal.Max(a, b, c...)       // Maximum of multiple decimals
decimal.Min(a, b, c...) // Minimum of multiple decimals
decimal.Sum(a, b, c...) // Sum of multiple decimals
decimal.Avg(a, b, c...) // Average of multiple decimals
decimal.RescalePair(a, b) // Rescale two decimals to same exponent

Usage in Models

type Product struct {
orm.FullAuditedModel
Name string `json:"name" bun:"name"`
Price decimal.Decimal `json:"price" bun:"price,type:decimal(10,2)"`
}

The decimal.Decimal type is fully supported by:

  • Bun ORM (database scanning/value)
  • JSON marshaling/unmarshaling
  • The copier package (value ↔ pointer conversion)
  • The mapx package (decode hooks)