Today, I found one of weird problem in my project about using parseFloat() function of Javascript. The usage of parseFloat() function parses a string and returns a floating point number.

example usage of parseFloat()

view plain print about
1document.write(parseFloat("10.33"));
2<!--- return value is "10.33" --->
3
4document.write(parseFloat("I AM PPSHEIN"));
5<!--- return value is "NaN" --->

It's still fine to use parseFloat() for parsing decimal value. If our decimal value is like "1,923.50", do you know the return value of this number after parsing with parseFloat().

Return value will be like as follow:

view plain print about
1document.write(parseFloat("1,923.50"));
2<!--- return value is "1" --->

At that time, we need to skip COMMA (",") from this value first and use parseFloat() after that. That's why I need to create skipping comma function in javascript as follow.

view plain print about
1function skipComma(NumberString) {
2    NumberString = NumberString.replace(/,/g,"");
3    NumberString = NumberString.replace(/\s/g,"");
4    return NumberString;
5}

After that, we can use parseFloat() function as follow.

view plain print about
1document.write(parseFloat(skipComma("1,923.50")));
2<!--- return value is "1,923.50" --->