Comments (4)
Just a thought, but I would add here that while forcing the responsibility on lib users to do their own float conversion minimizes complexity for go-money
, it also seems handling it in the library itself could prevent a lot of user error. Having a well-tested package that exports conversion functionality would relieve users of the library of a lot of duty they may not be knowledgable enough to have, especially for those new to Go. It's a decent enough use case to take these integers and display/work with floats on the frontend, so it would be nice to have this handled in an authoritative library and not have every lib user think about it.
from go-money.
Hi @vendion,
Why you see problem using 350
instead of 3.50
?
By the way, floating-points and currency are working well together.
"If I had a dime for every time I've seen someone use FLOAT to store currency, I'd have $999.997634" -- Bill Karwin
if your project requires usage of float, go-money
shouldn't be responsible for type conversion. My suggestion would be to do type conversion inside your project (if required)
from go-money.
So some of the issues I see with taking ints are: 1) 350
isn't as obvious as 3.50
if hardcoded (for whatever reason that may be) at first glance it's impossible to know if that's three hundred and fifty, or three dollars (or insert your currency here) and fifty cents. 2) It is fairly common, at least from my experience, to use a datatype similar to a float with fixed precession to store currency in a database (https://www.postgresql.org/docs/current/static/datatype-money.html).
One advantage Go's *big.Float
has is the ability to set the precision on it as well https://golang.org/pkg/math/big/#Float.SetPrec so it could be set to a precision of 2 places if need be. So in the case of the Bill Karwin quote he would have either $999.99
or maybe $1000
depending on the rounding mode (by default it's the latter https://play.golang.org/p/p2wKfpbiofy BTW). Although that may not be the best example.
I wasn't trying to say that this implementation was bad/wrong or anything, it just seems odd to me and when I was originally looking for a good way to handle currency, it seemed like *big.Float
was suggested more often, and even used in another package to handle formatting currency https://github.com/leekchan/accounting#caution
from go-money.
Anyways, closing this as you pretty much answered my question with the fact that go-money
won't handle conversions
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
- Question about the rounding for NewFromFloat HOT 5
- 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
- wrong fraction length HOT 3
- Add Must* versions of methods that return error
- 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.