Search code examples
javascriptparsefloat

Javascript parse float is ignoring the decimals after my comma


Here's a simple scenario. I want to show the subtraction of two values show on my site:

//Value on my websites HTML is: "75,00"
var fullcost = parseFloat($("#fullcost").text()); 

//Value on my websites HTML is: "0,03"
var auctioncost = parseFloat($("#auctioncost").text());

alert(fullcost); //Outputs: 75
alert(auctioncost); //Ouputs: 0

Can anyone tell me what I'm doing wrong?


Solution

  • This is "By Design". The parseFloat function will only consider the parts of the string up until in reaches a non +, -, number, exponent or decimal point. Once it sees the comma it stops looking and only considers the "75" portion.

    To fix this convert the commas to decimal points.

    var fullcost = parseFloat($("#fullcost").text().replace(',', '.'));