Home > Web Front-end > JS Tutorial > Three methods for converting floating point numbers into integers using javascript_javascript skills

Three methods for converting floating point numbers into integers using javascript_javascript skills

WBOY
Release: 2016-05-16 16:43:18
Original
1260 people have browsed it

Summary

I can only think of 3 methods for now. If readers think of other useful methods, you can also share them

parseInt
Bit operator
Math.floor Math.ceil

Description

1. parseInt

1. Example

parseInt("13nash");//13
parseInt("")// NaN
parseInt("0xA") //10(十六进制)
parseInt(" 13")//13
parseInt("070")//ES3为56(八进制) ES5为70
parseInt(070)//ES3和ES5都为56
parseInt(22.5)//22
Copy after login

2. Conversion rules:

(1). According to example (1), parseInt will parse until it is not a number and stop
(2). According to example (2), parseInt is NaN when parsing an empty string, not 0
(3). According to example (3), parseInt can convert hexadecimal numbers into decimal
(4). According to example (4), parseInt ignores spaces in strings

3. Disadvantages:

(1). According to example (5), we can know that parseInt is incompatible when converting octal arrays. ES3 will treat 070 as an octal value, but ES5 will treat 070 as a decimal value.
(2). According to examples (6) (7), we can know that parseInt will first convert the parameters into strings and then execute them into integers

4. Explanation: Why are the executions of (5) and (6) all about converting 070 into an integer, but the results are different? This is also the second point in solving how to obtain shortcomings.

Because I saw in the official documentation that If string is not a string, then it is converted to one. This passage. That is to say, if the parameter is not a string, it will first convert it into a string and then into an integer. For example, parseInt(070) in Example (6) actually converts 070 into a string first. You can try 070 "" or String(070) to know that 070 will be converted into "56" because 070 is an octal number. . Then it becomes parseInt("56"), and the final integer is 56. Whether you are on ES3 or ES5, it is 56

2. Bit operators

1. Example

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

2. 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, XORing 0 with other values ​​or bitwise OR will not change the operation value

3. Math.floor and Math.ceil

1. 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
Copy after login

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.

3. Solution:

//自行定义一个函数
function getInt(val){
return val>0 ? Math.floor(val):Math.ceil(val);
}
Copy after login
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