strongo / decimal

Decimal(64) implementation in GoLang based on int64 to keep money amounts

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Package github.com/strongo/decimal

Go CI GoDoc Go Report

Decimal 64 bit numbers implementation to represent money values in GoLang. Based on int64. Supports JSON (un)marshalling.

At the moment provides just a single type Decimal64p2 with fixed precision of 2 digits after point. In simple words it stores value as 64 bits integer amount of cents.

The code has 100% unit tests coverage.

E.g. 1.43 will be stored as int64(143) but when rendered as string will be represented as "1.43".

package example

import "github.com/strongo/decimal"

func Example() {
	var amount decimal.Decimal64p2; print(amount)  // 0
	
	amount = decimal.NewDecimal64p2(0, 43); print(amount)  // 0.43
	amount = decimal.NewDecimal64p2(1, 43); print(amount)  // 1.43
	amount = decimal.NewDecimal64p2FromFloat64(23.100001); print(amount)  // 23.10
	amount, _ = decimal.ParseDecimal64p2("2.34"); print(amount)  // 2.34
	amount, _ = decimal.ParseDecimal64p2("-3.42"); print(amount)  // -3.42
}

This package originally was developed for DebtsTracker.io - a mobile app & chat bots to split bills & track your debts.

Reasoning

  • Fast
  • Compact
  • No precision issues with storing values like 0.10
  • By storing with precision to cents there is no ambiguity with rounding. E.g. if you split $10 between 3 persons the amounts will be $3.33, $3.33 & $3.34.

MIT License

Free to use without restrictions. If cloned please keep links to https://github.com/strongo/decimal and to https://debtstracker.io/.

About

Decimal(64) implementation in GoLang based on int64 to keep money amounts

License:MIT License


Languages

Language:Go 100.0%