Home >Web Front-end >JS Tutorial >How to convert string to number in js?

How to convert string to number in js?

青灯夜游
青灯夜游Original
2020-07-18 14:50:014599browse

JS method of converting strings to numbers: 1. Use the conversion function parseInt() or parseFloat(); parseInt() can convert the value into an integer, and parseFloat() can convert the value into a floating point number. 2. Use the Number() function to convert the value of the object into a number.

How to convert string to number in js?

Method 1: Use the conversion function parseInt() or parseFloat()

js provides parseInt () and parseFloat() two conversion functions. The former converts the value to an integer, and the latter converts the value to a floating point number. Only by calling these methods on the String type can these two functions run correctly; for other types, NaN (Not a Number) is returned.

parseInt

According to JsPerf.com’s benchmarks, most browsers respond best to parseInt. Although it is the fastest way, using preseInt will encounter some common pitfalls:

parseInt("08"); // returns 0 部分老浏览器.
parseInt("44.jpg"); // returns 44

parseInt: When no base is passed in, the default is the base passed in is 10 parseInt(num, 10), if you If you don't know the type of num attribute, don't use parseInt to convert string to number.

parseFloat

This is a great option if you don't parse hexadecimal numbers. For example:

parseInt(-0xff); // returns -255
parseInt("-0xFF"); // returns -255
parseFloat(-0xff); // returns -255
parseFloat("-0xFF"); // returns 0

Note: Negative hexadecimal digits in strings are a special case, and if you parse with parseFloat, the result will be incorrect. To avoid NaN situations in your program, you should check the converted value.

parseFloat("44.jpg"); // return 44

parseFloat: Be careful when converting hexadecimal numbers. If you don't know the type of object you want to convert, don't use parseFloat.

Method 2: Use the Number() function

Number() function converts the value of the object into a number; if the value of the object cannot be converted is a number, then the Number() function returns NaN.

Example:

var test1= new Boolean(true);
var test2= new Boolean(false);
var test3= new String("999");
var test4= new String("999 888");

document.write(Number(test1)+ "<br />");
document.write(Number(test2)+ "<br />");
document.write(Number(test3)+ "<br />");
document.write(Number(test4)+ "<br />");

Output;

1
0
999
NaN

Method 3: Use js variable weak type conversion

Give me a small example, and you will understand after you look at it.

var str= &#39;012.345 &#39;;
var x = str-0;
x = x*1;

The above example takes advantage of the weak type characteristics of js and only performs arithmetic operations to achieve type conversion from string to number. However, this method is not recommended

Recommended tutorial: "JavaScript Video Tutorial"

The above is the detailed content of How to convert string to number in js?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:what is jsNext article:what is js