I’m having an issue with some javascript where I am calculating a percentage. It always comes in 1% off:
pcOff = parseInt((1-(PriceFrom/PriceFromRRP))*100);
PriceFrom and PriceFromRRP are captured from a JSON return and in the case of PriceFrom = '40.00' and PriceFromRRP = '50.00' pcOff is being set to 19; it should be of 20. Something funny is going on here. Can anyone shed some light?
gives:
When you
parseIntthat, the parser reads digits until it hits a non-digit – i.e. the19Your best bet (for this example) would be to divide last:
This prevents the floating point error appearing in the first place