user3406280 user3406280 - 4 months ago 5x
Javascript Question

Javascript parseFloat(number) - parseFloat(number2) returning int

I'm so sorry, but I need help :(

I've tried to convert string to decimal and it works, but I have some problem:

number = document.getElementById("totalcost").innerHTML; //It is a string, but I am sure that it is a decimal
number2 = prodCost; //it is a string but in fact it is a decimal too
alert(parseFloat(number)); // prints good (if number is 88,9 it will print 88,9)
alert(parseFloat(number2)); // it's ok too

alert(parseFloat(number) - parseFloat(number2)); // this is not ok :(
//if number=88,9 and number2=17,77 I get 71 but i need 71,13

Oh, guys, I'm so sorry, I'm stupit. Thank you so much! I have been working for the 9th hour in a row.. I'm really sorry, thank you all!


This looks like a locale problem: parseFloat only recognizes a period as a decimal point; it stops parsing when it gets to the comma, giving you only integer values. Unfortunately, there is no way to change this behavior. You need to replace the commas with periods in your number strings in order to get a decimal number.