Package money implements immutable monetary amounts for Go.
To install the money package into your Go workspace, you can use the go get command:
go get github.com/govalues/money
To use the money package in your Go project, you can import it as follows:
import (
"github.com/govalues/decimal"
"github.com/govalues/money"
)
To create a new amount, you can use one of the provided constructors,
such as NewAmount
, MustNewAmount
, ParseAmount
or MustParseAmount
.
d := decimal.MustNew(12345, 2) // d = 123.45
a := money.MustNewAmount(money.USD, d) // a = USD 123.45
b := money.MustParseAmount("USD", "123.45") // b = USD 123.45
Once you have an amount, you can perform arithmetic operations such as addition, subtraction, multiplication, division, as well as rounding operations such as ceiling, floor, truncation, and rounding.
sum, _ := a.Add(b)
difference, _ := a.Sub(b)
product, _ := a.Mul(d)
quotient, _ := a.Quo(d)
ratio, _ := a.Rat(b)
ceil := a.Ceil(2)
floor := a.Floor(2)
trunc := a.Trunc(2)
round := a.Round(2)
For more details on these and other methods, see the package documentation at pkg.go.dev.
For the benchmark results please check description of decimal package.
The money package is hosted on GitHub. To contribute to the project, follow these steps:
- Fork the repository and clone it to your local machine.
- Make the desired changes to the code.
- Write tests for the changes you made.
- Ensure that all tests pass by running
go test
. - Commit the changes and push them to your fork.
- Submit a pull request with a clear description of the changes you made.
- Wait for the maintainers to review and merge your changes.
Note: Before making any significant changes to the code, it is recommended to open an issue to discuss the proposed changes with the maintainers. This will help to ensure that the changes align with the project's goals and roadmap.