News for hackers. Stuff that matters.
Re:How to Handle Monetary Values in JavaScript by rtkwe on Tuesday October 30, 2018 @04:29PM (#18341002)
> Floats: 0.1 + 0.2 // returns 0.30000000000000004

The question is how much of an issue is this really? How often is 4 parts in 10 quadrillion a meaningful error when dealing with money? Especially when most of the time JS is dealing with money it will be presentation.

Re:How to Handle Monetary Values in JavaScript by john_of_peaches on Tuesday October 30, 2018 @05:03PM (#18341250)
Because value !== 0.3 and that can cause unexpected problems on the edges (which are the worst kind of issues).
Re:How to Handle Monetary Values in JavaScript by codr4 on Tuesday October 30, 2018 @06:21PM (#18341763)
I'm far from wizard on the topic, but from my experience it's not that simple since the relative magnitudes of the operands and the operator affect the size of the error in non obvious ways.

I spent 13 years maintaining a 2mloc reservation and accounting system that used floats for everything. By the time I got there and understood enough it was simply too late to do anything about it.

Just say no, really; that road leads to madness.

If you need precision; and many do; consider fixnums, rationals or bignums, in order of increasing complexity and flexibility.

Re:How to Handle Monetary Values in JavaScript by ouro on Tuesday October 30, 2018 @07:08PM (#18341999)
I might suggest reading https://doc.lagout.org/science/0_Computer%20Science/3_Theory...

This is in general how floating point math works.

Re:How to Handle Monetary Values in JavaScript by jfoutz on Tuesday October 30, 2018 @11:06PM (#18343044)
This is actually the plot of a few movies, people have been convicted of it in real life. [1]

[1] https://en.wikipedia.org/wiki/Salami_slicing

Re:How to Handle Monetary Values in JavaScript by mikeash on Tuesday October 30, 2018 @08:05PM (#18342311)
I ran into this once because a price was slightly too low, and got truncated. A $19.99 item got displayed as $19.98. You can work around this by rounding everywhere but that’s error-prone.
Re:How to Handle Monetary Values in JavaScript by partycoder on Tuesday October 30, 2018 @10:23PM (#18342873)
How much of an issue is it?

The kind of issue do not want to have: issues with money that can go very wrong when calculating discounts, tax percentages, interest or whatever.

Re:How to Handle Monetary Values in JavaScript by village-idiot on Wednesday October 31, 2018 @08:35AM (#18345106)
After doing a lot of calculations? This is a big problem.

The firm I worked at used built in Java libraries and always deferred rounding until the very last possible moment.

Re:How to Handle Monetary Values in JavaScript by jczhang on Tuesday October 30, 2018 @04:46PM (#18341144)
Yeah seriously. You'll have some front-end lib func converting that to 2 significant digits right of the decimal anyway. Weak argument imo haha.