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

后端 未结 3 1495
隐瞒了意图╮
隐瞒了意图╮ 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:43

    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?

提交回复
热议问题