0

I'm tryng to sum two floating point numbers in this way:

total= parseFloat(total) + parseFloat(($(this).parent().parent().parent().parent().next("div").find(".value").text()));
totale = total.toFixed(2);
console.log(total);

At first, total is 0 and the jQuery selector takes the second number that is "35,15". This results in total=35. Anyone could explain what am I doing wrong? Is the comma in number format the problem?

slash89mf
  • 1,057
  • 2
  • 13
  • 35

4 Answers4

1

replace comma with dot using replace(/,/g, '.') :

total= parseFloat(total) + parseFloat(($(this).parent().parent().parent().parent().next("div").find(".value").text()).replace(/,/g, '.'));
totale = total.toFixed(2);
console.log(totale);
Mohamed Ali RACHID
  • 3,245
  • 11
  • 22
0

The problem is the ,. It needs to be a ..

Nathan Arthur
  • 8,287
  • 7
  • 55
  • 80
UXDart
  • 2,500
  • 14
  • 12
  • Is there any way to transform it with dot? – slash89mf Jun 14 '17 at 15:09
  • the ugly way, is to do a `.replace(",", ".")` – UXDart Jun 14 '17 at 15:10
  • the real solution is to show a msg to the user saying that they should use `.` because if someone enters `11,234.56` value you app will do everything wrong. if someone enters `11,234` (not 234 decimals, but thousands) again it will do it wrong. – UXDart Jun 14 '17 at 15:17
0

The problem here is parseFloat("35,15") will be 35 because this is not starndard convention. If you want to parse this number you must replace the ,into a .. Like this parseFloat(total.replace(',', '.'))

dloeda
  • 1,516
  • 15
  • 23
0

You can use replace(',','.') to replace the comma with a decimal point and then parse the number.

total= parseFloat(total) + parseFloat(($(this).parent().parent().parent().parent().next("div").find(".value").text().replace(',','.')));
totale = total.toFixed(2);
console.log(total);
Piyush
  • 1,162
  • 9
  • 17