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

后端 未结 3 1494
隐瞒了意图╮
隐瞒了意图╮ 2021-02-05 05:37

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

3条回答
  •  有刺的猬
    2021-02-05 05:46

    Trade is usually computed by dollar (1 Australian Dollar = 0.94 US Dollars at time of posting). It is trivial to say that 100 Australian cents = 94 US cents, however there are many currencies where cents do not exist. The primary advantage of cents is that you can store values as integers, whereas dollars must be stored as floating- or fixed-point decimal values.

    A downside of storing monetary values in cents (as integers), is that rounding errors can occur. For example 20 Australian Cents = 18.8 US Cents.

    For currencies that have cents, it will probably often be simple to do computations, but a lot of the time you will have to convert to dollars (or primary currency, for non-dollar currencies), as that is what the exchange rates are based off.

    Personally, I'd always use the primary currency over a subdivision, they're easier to work with, less prone to rounding errors, and easier to read ($150.50 is easier to read than 15050¢.)

提交回复
热议问题