JavaScript parseFloat () function
Definition and Usage
parseFloat () function to parse a string and returns a float.
If the function specified string in the first character is a number. If so, the string is parsed until it reaches the end of the numbers up, and then return to the digital numbers, rather than as a string.
grammar
parseFloat(string)
参数 | 描述 |
---|---|
string | 必需。要被解析的字符串。 |
Browser Support
All major browsers support parseFloat () function
Tips and Notes
Note: The string only returns the first number.
Note: Leading and trailing spaces are allowed.
Note: If the first character string can not be converted to digital, then parseFloat () returns NaN.
Examples
Examples
Use parseFloat () to parse different strings:
<script>
document.write(parseFloat("10") + "<br>");
document.write(parseFloat("10.33") + "<br>");
document.write(parseFloat("34 45 66") + "<br>");
document.write(parseFloat(" 60 ") + "<br>");
document.write(parseFloat("40 years") + "<br>");
document.write(parseFloat("He was 40") + "<br>");
</script>
document.write(parseFloat("10") + "<br>");
document.write(parseFloat("10.33") + "<br>");
document.write(parseFloat("34 45 66") + "<br>");
document.write(parseFloat(" 60 ") + "<br>");
document.write(parseFloat("40 years") + "<br>");
document.write(parseFloat("He was 40") + "<br>");
</script>
Examples of the above output:
10
10.33
34
60
40
NaN
10.33
34
60
40
NaN
try it"
JavaScript Global Functions