X
HOME HTML CSS JAVASCRIPT SQL PHP JQUERY XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

JavaScript parseFloat() Function

Function Reference JavaScript Global Functions

Example

Parse different strings:

var a = parseFloat("10") + "<br>";
var b = parseFloat("10.00") + "<br>";
var c = parseFloat("10.33") + "<br>";
var d = parseFloat("34 45 66") + "<br>";
var e = parseFloat(" 60 ") + "<br>";
var f = parseFloat("40 years") + "<br>";
var g = parseFloat("He was 40") + "<br>";

var n = a + b + c + d + e + f + g;

The result of n will be:

10
10
10.33
34
60
40
NaN

Try it yourself »


Definition and Usage

The parseFloat() function parses a string and returns a floating point number.

This function determines if the first character in the specified string is a number. If it is, it parses the string until it reaches the end of the number, and returns the number as a number, not as a string.

Note: Only the first number in the string is returned!

Note: Leading and trailing spaces are allowed.

Note: If the first character cannot be converted to a number, parseFloat() returns NaN.


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The parseFloat() function is supported in all major browsers.


Syntax

parseFloat(string)

Parameter Values

Parameter Description
string Required. The string to be parsed

Return Value

Type Description
Number A floating point number. If the first character cannot be converted to a number, NaN is returned


Function Reference JavaScript Global Functions

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates FREE Website Templates Free HTML Templates

SHARE THIS PAGE

facebook