decimal

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 19, 2025 License: MIT Imports: 4 Imported by: 0

README

Decimal arithmetic

Go Reference Go Report Go Coverage

Arithmetic that works on a type alias of json.Number, inspired by SQLite.

Since Number is a string, literals are valid numbers, and you can do:

decimal.Sum("0.1", "0.1", "0.1", "-0.3") // == "0"

All operations produce exact results, with arbitrary precision (which is why we can't have decimal.Div).

Instead, we have decimal.Split (and decimal.Allocate) to distribute an amount amongst parties without loosing units to rounding.

decimal.Split("0.99", "0.01", 2) // == ["0.5", "0.49"]
decimal.Allocate("100", "0.05", 2, 3, 2) // == ["28.6", "48.85", "28.55"]

We also have operations to round amounts to multiples of a unit under various rounding modes.

The library won't win any performance prizes, but you may find the API more ergonomic than alternatives.

Documentation

Overview

Package decimal implements arbitrary-precision decimal arithmetic.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cmp

func Cmp(x, y Number) int

Cmp compares x and y, like cmp.Compare.

func IsInt added in v0.0.3

func IsInt(x Number) bool

IsInt reports whether x is an integer.

func IsValid added in v0.0.3

func IsValid(x Number) bool

IsValid reports whether x is a valid JSON number literal.

Types

type Fmt

type Fmt Number

Fmt is a formatter for a decimal number.

func (Fmt) Format added in v0.0.3

func (x Fmt) Format(f fmt.State, v rune)

Format implements fmt.Formatter. It accepts the formats for decimal floating-point numbers: 'e', 'E', 'f', 'F', 'g', 'G'. The 'v' format is handled like 'g'.

type Number

type Number = json.Number

A Number is an arbitrary precision decimal number, stored as JSON text.

func Abs added in v0.0.3

func Abs(x Number) Number

Abs returns |x| (the absolute value of x).

func Add

func Add(x, y Number) Number

Add returns the sum x + y.

func Allocate added in v0.0.3

func Allocate(amount, unit Number, ratios ...uint) []Number

Allocate allocates an integer amount of units according to a list of ratios. Leftover units are distributed round-robin, from left to right.

func Ceil added in v0.0.3

func Ceil(x, unit Number) Number

Ceil returns the least multiple of unit greater than or equal to x.

func Float64

func Float64(f float64) Number

Float64 converts f into a decimal number.

func Floor added in v0.0.3

func Floor(x, unit Number) Number

Floor returns the greatest multiple of unit less than or equal to x.

func Int64

func Int64(i int64) Number

Int64 converts i into a decimal number.

func Mul

func Mul(x, y Number) Number

Mul returns the product x * y.

func Neg

func Neg(x Number) Number

Neg returns -x (x with its sign negated).

func Pow added in v0.1.0

func Pow(x Number, n uint) Number

Pow returns xⁿ (the nth power of x).

func Prod added in v0.1.0

func Prod(n ...Number) Number

Prod returns the product of all n.

func Round added in v0.0.3

func Round(x, unit Number) Number

Round rounds x to the nearest multiple of unit, with ties away from zero.

func RoundToEven added in v0.0.3

func RoundToEven(x, unit Number) Number

RoundToEven rounds x to the nearest multiple of unit, with ties to an even multiple of unit.

func Split added in v0.0.3

func Split(amount, unit Number, n uint) []Number

Split splits an integer amount of units over n parties. Leftover units are distributed round-robin, from left to right.

func Sub

func Sub(x, y Number) Number

Sub returns the difference x - y.

func Sum

func Sum(n ...Number) Number

Sum returns the sum of all n.

func Trunc added in v0.0.3

func Trunc(x, unit Number) Number

Trunc rounds x toward zero, to a multiple of unit.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL