Skip to content

ncruces/decimal

Repository files navigation

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.

About

Decimal arithmetic

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages