4 ms·
> Solution: Round after every operation No, the solution is not to use floating point numbers to store money. I worked on an iOS app in fintech for years, and
by arcticbull 7y ago
> Solution: Round after every operation
No, the solution is not to use floating point numbers to store money.
I worked on an iOS app in fintech for years, and let me assure you, using floating point numbers to calculate currency is an exercise in frustration and lack of correctness. When balances are wrong you're losing your customers money, which in turn loses trust in your product. You know it's inexact, an approximation, and your customers want an exact answer.
Just do it with a fixed-precision decimal number, and represent it as a string.
- deleted 7y ago[deleted]
- analog31 7y agoIndeed, I think the underlying issue is that people use the last decimal as a kind of primitive error checking code that worked before the computer age. So it's a social matter. The legal implications stem from the social ones.
- Supermancho 7y ago> Just do it with a fixed-precision decimal number, and represent it as a string. Why? If you are going to do that, might as well use an integer of cents. It's more compact and if you're worried about these calculations and storing this kind of information, it's likely you are storing lots of it (or it wouldn't be a problem).
- arcticbull 7y agoIt’d need to be integer cents plus a scale factor which determines the smallest unit of currency. Japan has a scale factor of 0 as compared to the US 2. A string incorporates this conveniently. Either is fine.
- jdashg 7y agoHow do the balances get incorrect with 2^53 cents of precisely-roundable range?
- arcticbull 7y agoIn part it's defense against mistakes. You've got a big org, lots of people contributing and your cultural enforcement of the fact everything has to be rounded after every single step falls down. If you're going to create wrappers to automate that part why not just use fixed-precision? There's not really a good reason to use floating point and a few good reasons not to.