https://github.com/strongo/decimal
Decimal(64) implementation in GoLang based on int64 to keep money amounts
https://github.com/strongo/decimal
decimal go golang
Last synced: 5 months ago
JSON representation
Decimal(64) implementation in GoLang based on int64 to keep money amounts
- Host: GitHub
- URL: https://github.com/strongo/decimal
- Owner: strongo
- License: mit
- Created: 2016-12-17T08:34:51.000Z (over 9 years ago)
- Default Branch: main
- Last Pushed: 2024-05-09T10:04:04.000Z (about 2 years ago)
- Last Synced: 2024-10-16T09:28:07.271Z (over 1 year ago)
- Topics: decimal, go, golang
- Language: Go
- Size: 24.4 KB
- Stars: 10
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Package `github.com/strongo/decimal`
[](https://github.com/strongo/decimal/actions/workflows/ci.yml)
[](https://godoc.org/github.com/strongo/decimal)
[](https://goreportcard.com/report/github.com/strongo/decimal)
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"`.
```go
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/.