Home >Web Front-end >JS Tutorial >How to convert characters to floating point type in javascript
Methods for converting javascript characters into floating point types: 1. Use the parseFloat() method, which can parse a string and return a floating point number, with the syntax "parseFloat (character variable)"; 2. Use " " Operator, syntax "(character variable)".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
javascript character type conversion to floating point type
Method 1. Use parseFloat() method
## The #parseFloat() method is used to accept a string and convert it to a floating point number; it parses a string and returns a floating point number. If the string does not contain a numeric value, or if the first character of the string is not a numeric value, NaN is returned, indicating that it is not a numeric value. Example:var n = "-33.565"; var floatValue = parseFloat(n); console.log(floatValue);
Method 2: Using the " " operator
In javascript, you can use JavaScript's implicit type conversion function converts string values into floats.Implicit type conversion automatically performs type conversion based on the operator.Example:
var n = "33.565"; document.write("转换前的值 为:" + n + ",类型为" + typeof n + "<br>"); var floatValue = +(n); document.write("转换后的值 为:" + n + ",类型为" + typeof floatValue + "<br>");[Related recommendations:
The above is the detailed content of How to convert characters to floating point type in javascript. For more information, please follow other related articles on the PHP Chinese website!