Home > Web Front-end > JS Tutorial > How to convert characters to floating point type in javascript

How to convert characters to floating point type in javascript

青灯夜游
Release: 2021-11-24 11:44:51
Original
13085 people have browsed it

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)".

How to convert characters to floating point type in javascript

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);
Copy after login

How to convert characters to floating point type in javascript

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>");
Copy after login

How to convert characters to floating point type in javascript

[Related recommendations:

javascript learning tutorial]

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template