-
Notifications
You must be signed in to change notification settings - Fork 148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NewFromFloat shifts the value of the amount in certain currencies. #124
Comments
I am having the same issue:
|
Floats are the issue: @kotaroyamazaki https://go.dev/play/p/oNzxYVoZK4K @kylebragger https://go.dev/play/p/j9lwBeNUllb The idea of this package is to use the smallest unit of the currency, so cent in case of dollars and use that as int. So 136.98 becomes 13698. This is know as Fowler's Money pattern which is what this package implements. It is designed to overcome floating point issues like the one you described. |
I was starting with a string in the form of a float, so my solution was to strip non-numeric characters with regex, parse the remaining string as an int, and pass that int to the
|
We encountered the same issue, and decided to switch to https://github.com/shopspring/decimal. amount := 73708.43
// money
moneyAmount := money.NewFromFloat(amount, "EUR")
fmt.Println("money cents", moneyAmount.Amount()) // decimal 7370843
// decimal
decimalAccount := decimal.NewFromFloat(amount)
decimalSubunits := decimal.New(100, 0)
decimalAmountCents := decimalAccount.Mul(decimalSubunits)
fmt.Println("decimal cents", decimalAmountCents.IntPart()) // money 7370842 @SkipHendriks @Rhymond Hey, just a thought, if floats are causing trouble, why was the NewFromFloat method included in the library to begin with? Maybe it's worth mentioning in the README so other people don't stumble over this again? |
e,g)
Display amount of SDG
input 136.98
expected: $136.98
actual: $136.97
seeing is believing ↓
https://go.dev/play/p/l4imxLkT70u
The text was updated successfully, but these errors were encountered: