>
 





The JavaScript parseFloat() function, parses a string and returns the first floating point number in the string.

The parseFloat() function determines if the first character in the string argument is a number, parses the string from left to right until it reaches the end of the number, discards any characters that occur after the end of the number, and finally returns the number as a number (not as a string).

Only the first number in the string is returned, regardless of how many other numbers occur in the string.

If the first character in the string is not a number, the function returns the Not-a-Number value NaN.

Here are some examples about Javascript parseFloat() method:

document.write("<BR>" + parseFloat("15"))
document.write("<BR>" + parseFloat("12.12345"))
document.write("<BR>" + parseFloat("45.00000000"))
document.write("<BR>" + parseFloat("23.348  44.218  55.405"))
document.write("<BR>" + parseFloat("    55 aardvarks"))
document.write("<BR>" + parseFloat("Year 2002"))

The outputs will be:

15
12.12345
45
23.348
55
NaN

Want automatic updates? Subscribe to our RSS feed or
Get Email Updates sent directly to your inbox!

Currently rated 3.7 by 3 people

  • Currently 3.666667/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Comments

October 21. 2009 20:45

Social comments and analytics for this post

This post was mentioned on Twitter by bloggingdev: JavaScript parseFloat() Function with Examples - Converting Strings to Numbers - http://su.pr/2C76a6

uberVU - social comments | Reply

October 29. 2009 15:11

And what if the string contains a float in anuther currency, for example: 12,89 (some currencies use "," instead of "." for the decimal separator)? Does it adjust to the local settings on the current machine?

theheartcollector | Reply

March 15. 2010 14:55

A good laugh is sunshine in the house.

cash loans | Reply

Add comment




(Will not be displayed!)








Free CMS