You would think that this is a fairly rookie error and that big companies would know better, but I regularly see this on Uber: https://i.imgur.com/qDACtG0.png
On the surface one would think big companies should "know better". I mean, a large company is the accumulation of centuries or even millennia of career experience. Unfortunately, they also contain just as much accumulated nonsense and foolishness; and enough bureaucracy and organizational cruft to hide it forever.
Your balance is $0.30000000004
11–20 of 69 posts
Re: Your balance is $0.30000000004
#12Work with cents everywhere and format the output in the UI. Done.
Re: Your balance is $0.30000000004
#13Work with cents everywhere and format the output in the UI. Done.
Re: Your balance is $0.30000000004
#14Work with cents everywhere and format the output in the UI. Done.
Unless I have to actually work with floats, I try to only do everything with int math to keep things sane. Not to mention, that it usually makes calculations faster.
Re: Your balance is $0.30000000004
#15Work with cents everywhere and format the output in the UI. Done.
Not that simple, though. That's the "division and rounding" method of the article: share a $.99 fee equally between two parties. That's either 49 cents each or 50 cents each if you're not careful. In both cases, where is the extra cent?
Unless you're storing half cents in their account and not showing it.
Which is perfectly legitimate by the way, because if you earn a fraction of a cent interest accumulated over a couple months that might actually be a cent and you have to pay it.
The solution might be to store everything as 1/100 of a cent, as an integer.
Re: Your balance is $0.30000000004
#16Work with cents everywhere and format the output in the UI. Done.
I would rather recommend using an arbitrary precision decimal library like Decimal.js, and using strings to represent money in JSON. And of course using an appropriate database type for storing the data.
Re: Your balance is $0.30000000004
#17Earlier quoted context omitted.
Unless I have to actually work with floats, I try to only do everything with int math to keep things sane. Not to mention, that it usually makes calculations faster.
Decimal floating point exists ( https://en.wikipedia.org/wiki/Decimal64_floating-point_forma... ), is that considered useable for financial data, or does it turn out in practice to also suffer from the same issues? Since, e.g., dividing through 3 will not be exact here either of course.
http://csharphelper.com/blog/2017/05/compare-the-performance...
Re: Your balance is $0.30000000004
#18Work with cents everywhere and format the output in the UI. Done.
This approach doesn't work when dealing with fractional cents, like charging $0.0001 per hour for something. I would rather recommend using an arbitrary precision decimal library like Decimal.js, and using strings to represent money in JSON. And of course using an appropriate database type for storing the data.
Re: Your balance is $0.30000000004
#19Work with cents everywhere and format the output in the UI. Done.
Or just work in floats and round as the last step. Or do both selectively depending on which rounding error works in your favor, but don't tell anyone that's what you're doing.
The real problem is that nobody at the company seems to have looked at the parts of their software that everyone sees, so what else have they not looked at?
Re: Your balance is $0.30000000004
#20Good example of a leaky abstraction. JavaScript developers almost never have to worry about or completely understand the intricacies of floating-point arithmetic, until errors like this happen. https://en.wikipedia.org/wiki/Leaky_abstraction
This is not a leaky abstraction. JavaScript doesn't pretend that the numeric type is anything other than an IEEE-754 float. It would be a leaky abstraction if JavaScript said "this type can represent all real numbers!" but that would be insanity.