Home  >  Article  >  Web Front-end  >  How to convert data to integer in javascript

How to convert data to integer in javascript

青灯夜游
青灯夜游Original
2021-06-23 14:14:5815431browse

Javascript method to convert integers: 1. Use the parseInt() function, the syntax is "parseInt("value to be converted")". 2. Use bit operators, such as "~~123.45". 3. Use the "Math.floor(value)" and "Math.ceil(value)" syntax.

How to convert data to integer in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Method 1: parseInt() function

parseInt() is a global method that converts values ​​to integers. The conversion process is as follows:

  • First parse the character at position 0. If it is not a valid number, NaN will be returned directly.

  • If the character at position 0 is a number or can be converted to a valid number, continue to parse the character at position 1. If it is not a valid number, return the valid character at position 0 directly. number.

  • And so on, analyzing each character one by one from left to right until a non-numeric character is found.

  • parseInt() will convert all previously analyzed legal numeric characters into numerical values ​​and return them.

console.log(parseInt("123abc"));  //返回数字123
console.log(parseInt("1.73"));   //返回数字1
console.log(parseInt(".123"));   //返回值NaN

Dots in floating-point numbers are illegal characters for parseInt(), so the decimal part of the value will not be converted.

If it is a numeric string starting with 0, parseInt() will treat it as an octal number: first convert it to an octal value, and then convert it to a decimal number and return it.

If it is a numeric string starting with 0x, parseInt() will treat it as a hexadecimal number: first convert it to a hexadecimal value, and then convert it to a decimal number and return .

var d = 010;  //八进制数字字符串
var e = 0x10;  //十六进制数字字符串
console.log(parseInt(d));  //返回十进制数字8
console.log(parseInt(e));  //返回十进制数字16

parseInt() also supports base mode, which can convert digital strings in different bases such as binary, octal, and hexadecimal into integers. The base mode is specified by the second parameter of the parseInt() function.

Example 1

The following code converts the hexadecimal digit string "123abc" into a decimal integer.

var a = "123abc";  
console.log(parseInt(a,16));  //返回十进制整数1194684

Example 2

The following code converts binary, octal and decimal digit strings into decimal integers.

console.log(parseInt("10",2));  //把二进制数字 10 转换为十进制整数,为 2
console.log(parseInt("10",8));  //把八进制数字 10 转换为十进制整数,为 8
console.log(parseInt("10",10));  //把十进制数字 10 转换为十进制整数,为 10

Example 3

If the first parameter is a decimal value and contains a 0 prefix, in order to avoid being misunderstood as an octal number, the second parameter should be specified The parameter value is 10, which means the base mode is explicitly defined instead of the default base mode.

console.log(parseInt("010"));  //把默认基模式数字 010 转换为十进制整数为 10
console.log(parseInt("010",8));  //把八进制数字 010 转换为十进制整数为 8
console.log(parseInt("010",10));  //把十进制数字 010 转换为十进制整数为 10

Method 2: Bit operators

console.log(0 | "123.45")//123
console.log(0 | 123.45)//123
console.log(0 ^ 123.45)//123
console.log(~~123.45)//123

Principle: JavaScript has no concept of integers, and all numerical types are double-precision floating point numbers . When using bitwise operators, they will first convert the operands into integers to facilitate operations. However, XOR or bitwise OR between 0 and other values ​​will not change the operation value

Method 3: Math.floor and Math.ceil

Example

console.log(Math.floor(2.3))//2
console.log(Math.floor(-2.3))//-3
console.log(Math.ceil(2.3))//3
console.log(Math.ceil(-2.3))//-2

The two are insufficient:

Math.floor gets the smallest integer of the number; while Math.ceil gets the largest integer. Therefore, if we round up -2.3, we will get -2, but using Math.floor we will get -3. And 2.3 uses Math.ceil to get 3, but what we want is 2.

Solution:

//自行定义一个函数
function getInt(val){
return val>0 ? Math.floor(val):Math.ceil(val);
}

[Related recommendations: javascript learning tutorial]

The above is the detailed content of How to convert data to integer in javascript. 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