Decimals to 2 places for money in Python 3

后端 未结 5 944
伪装坚强ぢ
伪装坚强ぢ 2020-12-15 05:26

How do I get my decimals to stay at 2 places for representing money using the decimal module?

I\'ve setting the precision, and damn near everything else

5条回答
  •  忘掉有多难
    2020-12-15 06:18

    Falsehoods programmers believe about money:

    • Monetary values can be stored or represented as a floating point.
    • All currencies have a decimal precision of 2.
    • All ISO 4217 defined currencies have a decimal precision.
    • All currencies are defined in ISO 4217.
    • Gold is not a currency.
    • My system will never have to handle obscure currencies with more than 2 decimal places.
    • Floating point values are OK if the monetary value of transactions is "small".
    • A system will always handle the same currency (therefore we do not persist the currency, only the monetary value).
    • Storing monetary values as signed long integers will make them easier to work with, just multiply them by 100 after all arithmetic is done.
    • Customers will never complain about my rounding methods.
    • When I convert my application from language X to language Y, I don't have to verify if the rounding behavior is the same.
    • On exchanging currency A for currency B, the exchange rate becomes irrelevant after the transaction.

提交回复
热议问题