Currency operations with javaScript?

I am creating a budget app with javascript. I have to let javascript do most of the operations. Therefore, I have some controls in which the user changes the desired amount for the category, and my application shows new amounts for subcategories. So if I have + Auto $ 50.23 - Gas $ 30.25 - Maintenance $ 6.27 - Insurance $ 10.02 -...

So, if the user changes Auto 50.23 to 90.00, the amounts for gas, maintenance, insurance, etc. will reflect an increase in% of their parent category.

The problem is that after several operations I lose accuracy and creates some unpleasant errors. In most cases, the numbers do not add up (as they should). After changing the amount for the car several times, and then set it to 0, the remaining amounts are not always 0, and sometimes they drop below 0, giving me NaN, so if I change Auto to something more than 0 later, my NaN will remain NaN.

I just heard someone say that fractions in javascript are approximations, which is another problem. I tried to find out if this is a fact, but I didn’t find anything (maybe I didn’t look for the right conditions)

I'm sure some of you have run into similar problems, what would you suggest? Someone suggested that I multiply my original number by 100, and then do all the operations, but I'm afraid that this will not help me deal with the problem of accuracy loss.

Any ideas?

I would be grateful for your help!

+5
source share
2 answers

, 0,1 + 0,2 0,3. JavaScript, IEEE (IEEE 754). , . , , . , , 100. . 100, . , , .

"JavaScript: . Copyright 2008 Yahoo! Inc., 978-0-596-51774-8 ".

+9

. . .

, .

+3