JavaScript parseFloat
The JavaScript parseFloat function is used to parse the parameter and return a float-point number. The parameter of parseFloat can be an expression or a simple string. When the prefix of a string is a number, the parseFloat function The number will be parsed. If the prefix cannot be converted to a number or a floating point number, parseFloat returns NaN.
Basic syntax of JavaScript parseFloat function
parseFloat (parameter to be parsed)
The parameter to be parsed can be a self-string or expression, but if it is an expression, please pay attention to the calculation process to avoid causing parseFlaot to end the analysis. The parsing process of parseFloat will start from the first character. If it encounters a character that cannot be parsed as a number or a floating point number, parseFloat will end the parsing action and parse the number before the character and return the result.
JavaScript parseFloat function example
document.write(parseFloat("10")); // directly returns the integer 10
document.write(parseFloat("3.14")); // directly returns the floating point number 3.14
document. write(parseFloat("314e-2")); // Expression, return the answer 3.14
document.write(parseFloat("10 20 30")); //Return the first number 10
document.write(parseFloat(" 60 ")); // Skip the spaces and return the number 60
document.write(parseFloat("40 years")); // Only parse the number 40
document.write(parseFloat("He was 40")); // First The character is H and cannot be parsed as a number, so NaN is returned
</script>
The first example is only a simple parse number 10, and the second example is only a simple parse floating point number 3.14. The third example is pare, an expression. 314e-2 means that the decimal point is pushed forward by two digits, which is 3.14. The items that JavaScript parseFloat can calculate are +,-and scientific symbols such as e, E, etc., and the rest The symbol will be ignored. After the fourth example parses the first number 10, a space is encountered immediately, so the space and the string to the right of the space are ignored. The initial blank of the fifth string is ignored by the parseFloat function, and the number 60 is successfully parsed. The sixth string has a similar meaning. The spaces after the number are ignored. The last example is because of the first The character is the English letter H, and it is impossible to parse numbers or floating-point numbers, so parseFloat returns NaN.
Post a Comment
0 Comments