7 ms·
You probably already heard of storing amount in cents as an integer, but it's a pattern worth mentioning if it fits your use case.
by webpaymentsguy 7y ago
You probably already heard of storing amount in cents as an integer, but it's a pattern worth mentioning if it fits your use case.
- marton78 7y agoI don't know how it's done in the US, but in Europe financial calculations are done with four digits after the dot.
- catmanjan 7y agoSo store the value as milli-cents? (haha)
- Loranubi 7y agoShouldn't that be centi-cents? ^^
- inferiorhuman 7y agoOr in MySQL: innocents.
- miohtama 7y agoIt is possible, but makes is somewhat impractical, because you need to convert numbers back and forth every time you touch them on an application level. Not worth the extra headache.
- luhn 7y agoSpeaking from experience, if you exclusively deal with integers internally and then apply the appropriate formatting when outputting to the user (which you need to deal with regardless of internal representation), it makes dealing with monetary data so much easier. Integers are generally better supported, faster, and more convenient for most languages, databases, and serialization formats.
- gen220 7y ago+1 adding my anecdata to this. If you don’t need irrational numbers (few financial applications do?), the tuple of (value, scale) can take you very far.
- utxaa 7y agoyou never really deal with irrationals ... do you mean floating point?
- gen220 7y agoNope, specifically meant irrationals. Floating point (up to a certain amount of absurdity) you can describe with integers (value, scale). Real numbers you can describe with integers (numerator, denominator, scale). Irrationals are more difficult. That's why we try not to use them :). Hopefully your company's commission tiers are not defined in terms of circles or euclidean distances.
- fetbaffe 7y agoDo you then use variable length for the precision depending on the currency?
- patrickthebold 7y agoHow do you handle things like multiplying by some interest rate of 3.125 or (1 + 0.03125)^-354 as examples. Sure you can round at the end but then you have to worry about compounding errors.