Search code examples
javascriptarraysdivisionparsefloat

Javascript calcul doesn't work if number upper than 1000


I try to calculate a simple division in javascript but if my number is < 1000 The result is not good.

Here my code :

var cal = parseFloat(currentAmount.toString()) / parseFloat(currencyArray[i][j].toString())

if currencyArray[i][j] equal to 1,3703 and currentAmount = 100 my result is good (72.98) but if i put 1000 my result is not good (0.7298... )


Solution

  • Due to the particular value you get, I'm going to guess that your currentAmount isn't 1000 as you claim, but rather 1,000 or 1 000 or even 1'000 in some countries. When parseFloat gets hold of that, it sees only 1 because , isn't a valid character in numbers. 1 / 1.3703 is 0.7298....

    Numbers must not have thousand separators, and decimals must be a point. (As someone who grew up in France, I know this can be confusing!)