Home  >  Article  >  Web Front-end  >  How to convert value to int type in javascript

How to convert value to int type in javascript

青灯夜游
青灯夜游Original
2021-09-02 18:21:3416810browse

Javascript method to convert a value into an int type: 1. Use the "parseInt(value)" method to convert a value into an int integer; 2. Use "Math.floor(value)", "Math.ceil (value)" and "Math.round(value)" methods convert decimal values ​​into int integers.

How to convert value to int type in javascript

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

Method 1: Use parseInt()

parseInt() is a global method that converts a value to an integer. 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

Method 2: Use Math.floor(), Math.ceil(), Math.round() to convert decimals to integers

There are 3 Math objects Static functions can be rounded

  • Math.floor(): Returns the largest integer less than the parameter value.

  • Math.ceil(): Returns the smallest integer greater than the parameter value.

  • Math.round(): Rounding.

Example 1: The following code simply compares the values ​​​​of these three methods

console.log(Math.floor(2.5));  //2
console.log(Math.floor(-2.5));  //-3
console.log(Math.ceil(2.5));  //3
console.log(Math.ceil(-2.5));  //-2
console.log(Math.round(2.5));  //3
console.log(Math.round(-2.5));  //-2
console.log(Math.round(-2.6));  //-3

Example 2: The following code combines Math.floor() and Math.ceil( ) method, use JavaScript to design a function that rounds numbers

var toInt = function (num) {
    var num = Number(num);
    return num < 0 ? Math.ceil(num) : Math.floor(num);
}
console.log(toInt(2.5));  //2
console.log(toInt(-2.5));  //-2

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to convert value to int type 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