Comments (5)
Hopefully, this convo will help for posterity if someone needs specific rounding for a use-case and how they can achieve this with NewFromFloat
. Going to close this. Thanks!
from go-money.
Hi @TimothyIp
To be honest there was not much of a discussion about this new feature. The current formula:
int64(99.9999 * 10^(fraction))
The rounding is done by casting float to integer and this is how it works in Go.
Maybe @npinochet can give you a bit more details
from go-money.
The rounding is done by casting float to integer and this is how it works in Go.
That's correct, I made the function thinking more about simplicity rather than usability.
Maybe we can have the discussion here, I think rounding is aceptable in this case, the formula can be adjusted for it:
factor = 10^fraction
int64((99.9999 + 0.5 / factor) * factor)
from go-money.
Just want to give thanks to the both of you for taking the time out of your day for answering this question of mine.
I can see the winner being simplicity here. I just went down a rabbit hole of all the different ways you can round cents (round down, round up, banker's rounding, ...etc).
I could see it just being as simple as having the consumer provide the float into NewFromFloat
already rounded to their specific use case. (Which will solve my use case)
As for changing the internals of the function, that part i'm unsure of. I'm with keeping the behaviour in line with Go, since this rounding topic can get quite opinionated.
An additional function could be added that would do the explicit rounding for a given float, something like NewFromFloatRounded
.
from go-money.
Thank you for opening this discussion :)
I agree that there are many ways to handle rounding in regards to currencies, I believe it's better to keep it simple.
There may be cases where floats like 99.999999 may appear, but that could mean that somewhere you did some float arithmetics before converting them to Money, which beats the use of this library.
If rounding is really necesary, one could always pre round the float before passing it to the function (NewFromFloat(99.999999 + 0.005)
)
from go-money.
Related Issues (20)
- Reminder: migrate master branch to main
- Split of small negative values doesn't work properly HOT 1
- Proposal: Add function NewFromFloat HOT 6
- Missing Currencies: West African Frank (XOF) and Malagasy Ariary (MGA) HOT 1
- KPW and HUF have incorrect ISO-4217 minor units HOT 2
- Add money comparisson HOT 1
- Wrong fraction for HUF HOT 1
- NewFromFloat trouble HOT 3
- Panic on money.Allocate() HOT 3
- NewFromFloat shifts the value of the amount in certain currencies. HOT 4
- Digit separation in INR amount
- There is no ZWL currency HOT 2
- How can I multiply a float number? HOT 4
- Integer overflow in the Allocate HOT 2
- Request support for Bolívar Soverano (VES) HOT 1
- Lack of MRU, SLE and STN currency support
- NewFromFloat panics with invalid/unrecognized currency input, unlike New HOT 1
- Use number of minor units in one major instead of a fraction
- Update the README.md HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-money.