JavaScript Global method: parseFloat()
[this page | pdf | back links]
The JavaScript
Global
parseFloat() parses a string and
returns a floating point number, assuming that the string can be interpreted as
such a number. It finds the first character after leading spaces, works out if
this is one that can appear in a number and then continues parsing the string
until it reaches the end of any part that is interpretable as a number,
returning that number as a number value, not a string. If the first available
non-space character is not numerical then the method returns NaN. Only the first of multiple
numbers will be returned.
It
has the following syntax with the following parameters:
parseFloat(x)
Parameter
|
Required / Optional
|
Description
|
x
|
Required
|
String forming the input
parameter
|
EXAMPLE:
HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
table,th,tr,td {border: 1px solid black; border-collapse :collapse;}
</style>
</head>
<body>
<table>
<tr>
<th>Example</th>
<th>Resulting value of <code>x</code></th>
</tr>
<tr>
<td><code id="Example"></code></td>
<td><code id="Result"></code></td>
</tr>
<tr>
<td><code id="Example2"></code></td>
<td><code id="Result2"></code></td>
</tr>
</table>
<script>
document.getElementById("Example").innerHTML =
'var x = parseFloat(" 3.4, 4.4");';
document.getElementById("Result").innerHTML =
parseFloat(" 3.4, 4.4");
document.getElementById("Example2").innerHTML =
'var x2 = parseFloat(" a3.4, 4.4");';
document.getElementById("Result2").innerHTML =
parseFloat(" a3.4, 4.4");
</script>
</body>
</html>
|
FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedJavaScriptMethodGlobalParseFloat() {
return (parseFloat("3.4") == 3.4);
} |
NAVIGATION LINKS
Contents | Prev | Next | JavaScript DOM (and BOM)