How to convert data type to integer in Javascript

PHPz
Release: 2023-04-25 13:56:32
Original
2794 people have browsed it

Javascript is an object-oriented programming language that provides a variety of data types, including integers, floating point numbers, strings, Boolean values, and more. In Javascript, use the parseInt() function to convert a string into an integer, and use the Math.floor() function to convert a floating point number into an integer. This article will introduce how to use these two functions to convert Javascript data types into integers.

1. The parseInt() function converts a string into an integer

The parseInt() function can convert a string containing an integer into an integer. For example, the following code converts the string "123" into the integer 123:

let a = "123";
let b = parseInt(a);
console.log(b); // 输出 123
Copy after login

This function can also specify a base number for parsing hexadecimal or octal numbers. For example, the following code converts the binary string "1010" to the decimal integer 10:

let a = "1010";
let b = parseInt(a, 2);
console.log(b); // 输出 10
Copy after login

If the string cannot be parsed as a number, NaN is returned. For example, the string "hello" in the following code cannot be parsed as a number, so NaN is returned:

let a = "hello";
let b = parseInt(a);
console.log(b); // 输出 NaN
Copy after login

It should be noted that if the string starts with 0, it will be judged as an octal number. For example, the string "012" in the following code is parsed as a decimal integer 10:

let a = "012";
let b = parseInt(a);
console.log(b); // 输出 10
Copy after login

To avoid this situation, you can specify the decimal number 10 in the parseInt() function:

let a = "012";
let b = parseInt(a, 10);
console.log(b); // 输出 12
Copy after login

2. The Math.floor() function converts floating-point numbers into integers

The Math.floor() function can round down floating-point numbers and convert them into integers. For example, the following code converts the floating point number 1.23 to the integer 1:

let a = 1.23;
let b = Math.floor(a);
console.log(b); // 输出 1
Copy after login

If the parameter is NaN, NaN is returned:

let a = NaN;
let b = Math.floor(a);
console.log(b); // 输出 NaN
Copy after login

It should be noted that if the parameter is positive infinity or negative infinity, then Return infinity or negative infinity:

let a = Infinity;
let b = Math.floor(a);
console.log(b); // 输出 Infinity
Copy after login

3. Summary

This article introduces how to use the parseInt() function and Math.floor() function to convert Javascript data types into integers. When using the parseInt() function, you need to pay attention to the issue of base numbers; when using the Math.floor() function, you need to pay attention to the parameter value range (cannot be NaN or infinity). Correct use of these two functions can help us better process data and write efficient Javascript code.

The above is the detailed content of How to convert data type to integer 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!