I'm creating a budget application with javascript. I have to let javascript do most of the operations. So I have some controls where the user changes the desired amount for a category and my application shows the new amounts for the 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 would reflect the % increase in their parent category.
The problem is that after a few operations I loose precision and it creates some nasty bugs. Most of the times the numbers don't add up (as they should). After changing the amount for auto a few times and then set it to 0, the other amounts don't always go to 0, and sometimes they go below 0 giving me a NaN, so if I later change Auto for something greater than 0, my NaN will stay NaN.
I just heard somebody say that fractions in javascript are approximations which is another problem. I tried to google to see if this is a fact, but couldn't find anything (maybe I didn't look for the right terms)
I'm sure some of you have ran into similar problems, what would you suggest? Somebody suggested to multiply my original number by 100 and then do all operations, but I'm afraid that this won't help me get away with the problem of loosing precision.
Any ideas?
I will appreciate your help!