Search code examples
javascriptparsefloat

sum of floats gives no decimals in javascript


I have 2 strings that have a 0 and a 16,63 as follows:

var diferencia = "0";
var estalviPotencia = "16,63";

If I put an alert to see its value with two decimals I obtain:

alert(parseFloat(diferencia).toFixed(2)) -- returns 0,00
alert(parseFloat(estalviPotencia).toFixed(2)) -- returns 16,63

But if I actually sum them and then get the value I get:

alert((parseFloat(diferencia) + parseFloat(estalviPotencia)).toFixed(2)); -- returns 16,00

I can't see the error and why I am obtaining that number...

Edit por possible duplicate:

I am NOT saying that the decimals are wrong as said on that answer (which asks why there so MANY decimals but why I am NOT obtaining any decimals at all.


Solution

  • There's a comma instead of a decimal point in

    var estalviPotencia = "16,63";
    

    so it's only reading the numerical characters up to the non-numerical character.

    change it into a decimal point and it'll work correctly.