The decimal part after the comma is ignored in floating point number parsing JavaScript
P粉282627613
2023-08-21 20:25:00
<p>Here is a simple scenario. I want to display the subtraction of two values on my website: </p>
<pre class="brush:php;toolbar:false;">//The value on my website is: "75,00"
var fullcost = parseFloat($("#fullcost").text());
//The value on my website is: "0,03"
var auctioncost = parseFloat($("#auctioncost").text());
alert(fullcost); //Output: 75
alert(auctioncost); //Output: 0</pre>
<p>Can anyone tell me what I'm doing wrong? </p>
The parseFloat function of javascript does not accept regional parameters. So you need to replace
,
with
.This is "By Design".
parseFloat
The function only considers parts of the string until it encounters a non- , -, number, exponent, or decimal point. Once it sees the comma, it stops looking and only considers the "75" part.To fix this problem, convert commas to decimal points.