1

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.

Miquel Coll
  • 759
  • 15
  • 49

1 Answers1

3

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.

Ben Green
  • 428
  • 1
  • 7
  • 18
  • That's indeed the solution. Is there any way to work WITH the commas? because I'm seeing I'll have to put a lot of .replace(',', '.') everywhere.... – Miquel Coll Apr 22 '16 at 10:29
  • 1
    Unfortunately str.replace is the way to go - probably best to write a function to handle the replace - parse - convertToFixed method, then you won't be having to rewrite it all each time – Ben Green Apr 22 '16 at 10:31