Global parseFloat() Function - Javascript Global

Javascript examples for Global:parseFloat

Description

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

This function parses the string until it reaches the end of the number, and returns the number as a number.

Leading and trailing spaces are allowed.

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

Parameter Values

Parameter Description
stringRequired. The string to be parsed

Return Value:

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

The following code shows how to Parse different strings:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {//from w w w.j a  va  2s.c o m
    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("asdf 4") + "<br>";

    var n = a + b + c + d + e + f + g;
    document.getElementById("demo").innerHTML = n;
}
</script>

</body>
</html>

Related Tutorials