I have noticed that some financial api's like stripe api for credit card processing require that amounts be passed in as cents, this seems like a good simplification and it is making me wonder why I don't do the same everywhere in my application I am currently using database NUMERIC unlimited length with Postgres and BigDecimal in my Java code but I am tempted by the simplicity of storing money as cents.
UPDATE For currencies that don't have cents, we can generalize the question to storing money as the smallest minor unit for a currency, so cents for dollars and whatever the equivalent is for other currencies.
The downside is that you can't represent fractional units. This is a problem if you are performing interest calculations or the like. However, if you are making a "real money" transaction, this issue goes away.
It doesn't matter if the currency is non-decimal - as long as there is a smallest unit, you can use that to represent any other amount. You may need to be a little more clever in how you display in human readable format, if that is relevant, but that's a separate issue.
In fact, using the smallest unit is likely to simplify performing conversions, as you can work out the conversion rate between the smallest unit of each currency pair. Of course, once you're into conversion rates, you'll end up needing to use BigDecimal (or the like) again.
So, in summary, it may well be best to use both smallest unit as your denomination, AND to use BigDecimal to handle fractional parts if they can arise. Don't use IEEE floats, because they can't represent all decimal numbers correctly. This will lead to the kinds of errors that will upset someone.
Not that many. Storing money as cents simplifies calculations and makes it precise. As mentioned quite a few times, never represent your money as floats in calculations.
You would need eventually present the amount to users. And the values then need to be formatted according to the formatting for a specific currency.
Working with real currencies and supporting multiple currencies in an application might require you to store money in 100th of cents to support currencies like CLF with 4 decimal places or BHD with 3 decimal places. See ISO-4217.
Not that I'm aware of. What kind of support are you looking for in such library?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With