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

rounding.mddocs/api/

Rounding

Multiple rounding strategies for financial and general-purpose use. All methods return new Decimal values.

Negative places values round the integer part (e.g., places=-1 rounds to nearest 10).

Capabilities

Round (Half-Up)

Rounds to places decimal places. If the digit to be rounded is exactly 5, rounds away from zero (half-up).

// Round rounds to places decimal places using half-up rounding.
// If places < 0, rounds the integer part to the nearest 10^(-places).
func (d Decimal) Round(places int32) Decimal

Usage:

decimal.NewFromFloat(5.45).Round(1).String()   // "5.5"
decimal.NewFromFloat(5.44).Round(1).String()   // "5.4"
decimal.NewFromFloat(5.445).Round(2).String()  // "5.45"
decimal.NewFromFloat(545).Round(-1).String()   // "550"
decimal.NewFromFloat(544).Round(-1).String()   // "540"

RoundBank (Banker's / Half-to-Even)

Rounds to places decimal places using banker's rounding (round half to even). When the digit to round is exactly 5, rounds to the nearest even number.

// RoundBank rounds to places decimal places using banker's rounding (half-to-even).
// If places < 0, rounds the integer part to the nearest 10^(-places).
func (d Decimal) RoundBank(places int32) Decimal

Usage:

decimal.NewFromFloat(5.45).RoundBank(1).String()  // "5.4" (rounds to even: 4)
decimal.NewFromFloat(5.55).RoundBank(1).String()  // "5.6" (rounds to even: 6)
decimal.NewFromFloat(5.46).RoundBank(1).String()  // "5.5"
decimal.NewFromFloat(545).RoundBank(-1).String()  // "540" (rounds to even: 4)
decimal.NewFromFloat(555).RoundBank(-1).String()  // "560" (rounds to even: 6)

RoundUp (Away from Zero)

Rounds away from zero in all cases (ceiling for positive, floor for negative).

// RoundUp rounds the decimal away from zero.
// If places < 0, rounds the integer part to the nearest 10^(-places).
func (d Decimal) RoundUp(places int32) Decimal

Usage:

decimal.NewFromFloat(545).RoundUp(-2).String()    // "600"
decimal.NewFromFloat(500).RoundUp(-2).String()    // "500"
decimal.NewFromFloat(1.1001).RoundUp(2).String()  // "1.11"
decimal.NewFromFloat(-1.454).RoundUp(1).String()  // "-1.5"

RoundDown (Towards Zero / Truncation)

Rounds towards zero in all cases.

// RoundDown rounds the decimal towards zero.
// If places < 0, rounds the integer part to the nearest 10^(-places).
func (d Decimal) RoundDown(places int32) Decimal

Usage:

decimal.NewFromFloat(545).RoundDown(-2).String()    // "500"
decimal.NewFromFloat(-500).RoundDown(-2).String()   // "-500"
decimal.NewFromFloat(1.1001).RoundDown(2).String()  // "1.1"
decimal.NewFromFloat(-1.454).RoundDown(1).String()  // "-1.4"

RoundCeil (Towards +Infinity)

Rounds towards positive infinity (ceiling function for decimal places).

// RoundCeil rounds the decimal towards +infinity.
// If places < 0, rounds the integer part to the nearest 10^(-places).
func (d Decimal) RoundCeil(places int32) Decimal

Usage:

decimal.NewFromFloat(545).RoundCeil(-2).String()    // "600"
decimal.NewFromFloat(500).RoundCeil(-2).String()    // "500"
decimal.NewFromFloat(1.1001).RoundCeil(2).String()  // "1.11"
decimal.NewFromFloat(-1.454).RoundCeil(1).String()  // "-1.4"

RoundFloor (Towards -Infinity)

Rounds towards negative infinity (floor function for decimal places).

// RoundFloor rounds the decimal towards -infinity.
// If places < 0, rounds the integer part to the nearest 10^(-places).
func (d Decimal) RoundFloor(places int32) Decimal

Usage:

decimal.NewFromFloat(545).RoundFloor(-2).String()    // "500"
decimal.NewFromFloat(-500).RoundFloor(-2).String()   // "-500"
decimal.NewFromFloat(1.1001).RoundFloor(2).String()  // "1.1"
decimal.NewFromFloat(-1.454).RoundFloor(1).String()  // "-1.5"

RoundCash (Cash / Penny Rounding)

Rounds to the nearest multiple of the minimum currency unit. Used for cash transactions.

// RoundCash rounds decimal to a specific currency interval.
// Valid intervals: 5, 10, 25, 50, 100.
// Panics for any other interval value.
//
// Rounding behavior:
//   5:   5 cent rounding  3.43 => 3.45
//   10:  10 cent rounding 3.45 => 3.50  (5 rounds up)
//   25:  25 cent rounding 3.41 => 3.50
//   50:  50 cent rounding 3.75 => 4.00
//   100: 100 cent rounding 3.50 => 4.00
func (d Decimal) RoundCash(interval uint8) Decimal

Usage:

decimal.NewFromFloat(3.43).RoundCash(5).String()    // "3.45"
decimal.NewFromFloat(3.47).RoundCash(5).String()    // "3.45"
decimal.NewFromFloat(3.45).RoundCash(10).String()   // "3.50"
decimal.NewFromFloat(3.41).RoundCash(25).String()   // "3.50"
decimal.NewFromFloat(3.75).RoundCash(50).String()   // "4.00"
decimal.NewFromFloat(3.50).RoundCash(100).String()  // "4.00"

Truncate

Removes digits beyond precision decimal places without rounding.

// Truncate truncates off digits from the number without rounding.
// precision is the last digit that will NOT be truncated. Must be >= 0.
func (d Decimal) Truncate(precision int32) Decimal

Usage:

d, _ := decimal.NewFromString("123.456")
d.Truncate(2).String()  // "123.45"
d.Truncate(1).String()  // "123.4"
d.Truncate(0).String()  // "123"

Floor and Ceil

Round to the nearest integer.

// Floor returns the nearest integer value less than or equal to d.
func (d Decimal) Floor() Decimal

// Ceil returns the nearest integer value greater than or equal to d.
func (d Decimal) Ceil() Decimal

Usage:

decimal.NewFromFloat(1.9).Floor().String()   // "1"
decimal.NewFromFloat(-1.9).Floor().String()  // "-2"
decimal.NewFromFloat(1.1).Ceil().String()    // "2"
decimal.NewFromFloat(-1.1).Ceil().String()   // "-1"

String Representation with Rounding

Methods to format decimal as a string with a fixed number of decimal places:

// StringFixed returns a rounded fixed-point string with places digits after the decimal point.
// Uses half-up rounding.
// If places < 0, see StringFixed docs.
func (d Decimal) StringFixed(places int32) string

// StringFixedBank returns a banker's-rounded fixed-point string with places digits
// after the decimal point.
func (d Decimal) StringFixedBank(places int32) string

// StringFixedCash returns a cash-rounded fixed-point string.
// interval must be one of: 5, 10, 25, 50, 100.
func (d Decimal) StringFixedCash(interval uint8) string

Usage:

decimal.NewFromFloat(0).StringFixed(2)        // "0.00"
decimal.NewFromFloat(5.45).StringFixed(0)     // "5"
decimal.NewFromFloat(5.45).StringFixed(1)     // "5.5"
decimal.NewFromFloat(5.45).StringFixed(2)     // "5.45"
decimal.NewFromFloat(5.45).StringFixed(3)     // "5.450"
decimal.NewFromFloat(545).StringFixed(-1)     // "550"

decimal.NewFromFloat(5.45).StringFixedBank(1) // "5.4"
decimal.NewFromFloat(5.55).StringFixedBank(1) // "5.6"

decimal.NewFromFloat(3.43).StringFixedCash(5) // "3.45"

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