Javascript - Global parseFloat() Function

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

Description

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.

Only the first number in the string is returned!

Leading and trailing spaces are allowed.

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

Syntax

parseFloat(string)

Parameter Values

Parameter Require Description
stringRequired.The string to be parsed

Return

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

Example

Parse different strings:

Demo

var a = parseFloat("10") + "\n";
var b = parseFloat("10.00") + "\n";
var c = parseFloat("10.33") + "\n";
var d = parseFloat("3 5 6") + "\n";
var e = parseFloat("   6   ") + "\n";
var f = parseFloat("40 years") + "\n";
var g = parseFloat("I am 45") + "\n";

var n = a + b + c + d + e + f + g;
console.log(n);//from   w w w.j  a  va  2 s. com

Result