3 ms·
What's wrong with just using a u64?
by andromeduck 9y ago
What's wrong with just using a u64?
- usr1106 9y agoYes, storing everything in cents and format a decimal separator (which is a localization mess https://en.wikipedia.org/wiki/Decimal_separator https://en.wikipedia.org/wiki/Decimal_separator ) before any output is one option. That works fine for addition and subtraction. When you divide (or do percent calculations) integer cents is no longer, you need to deal with rounding manually/some custom class with every calculation.
- andromeduck 9y agoYeah but that's rounding error which isn't limited to just money. There's no universal precision/representation between all the currencies an currencies change all the time so it would be wildly inappropriate to bake into a programming (language?) standard. Also, what's with the aversion to traits/types? This is the whole point of having operators/types.
- usr1106 9y agoCorrect. Precision must be parameterized for every variable. 2 is the most commonly used value (for money), but not the only reasonable one. And rounding needs to be done at certain steps but not a others. Typically internal calculations are expected to be done with maximum precision, and every time you have an item visible to the customer you round. Often to full cents, but there are cases where you show a fixed number of decimal cents even to the customer. I doubt that cases where you show maximum precision to a customer are common.
- wiz21c 9y agoProblem is that code is written by many people, so you have to agree on a way to divide (for example). Then, it means that you have to enforce the way you code it and current standard programming languages (python, java, c,...) don't help you there. We need a decimal format because people who read the code are sometimes not coders how know about the various tricks to pack decimals in uint64. So, u64 is perfectly doable, sure, but it doesn't scale with the various types of people who write/read the code. (same thing for floats : you can do money computations with them, but that'll work only if the people understand the trade offs of the representation; and believe me, they're not legions)