Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the downsides to storing money values as cents / minor units?

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.

  1. What are the downsides of storing money as cents.
  2. Are there specific operations that are hard to do with money as cents?
  3. Does money stored as cents work with all world wide currencies or does accounting for different currencies become a gigantic mess of if else special cases?
  4. Is there a good java library for working with 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.

like image 331
ams Avatar asked Sep 21 '13 16:09

ams


2 Answers

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.

like image 154
Marcin Avatar answered Nov 15 '22 06:11

Marcin


  1. What are the downsides of storing money as cents.

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.

  1. Are there specific operations that are hard to do with money as cents?

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.

  1. Does money stored as cents work with all world wide currencies or does accounting for different currencies become a gigantic mess of if else special cases?

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.

  1. Is there a good java library for working with money as cents?

Not that I'm aware of. What kind of support are you looking for in such library?

like image 3
denis.solonenko Avatar answered Nov 15 '22 05:11

denis.solonenko