Three ways JavaScript converts floating-point numbers to integers _javascript tips

Source: Internet
Author: User
Tags bitwise bitwise operators

Summary

For the time being, I thought of 3 methods. If readers think of other useful methods, they can also communicate

parseint
Bitwise operators
Math.floor Math.ceil

Description

First, parseint

1. Examples

parseint ("13nash");//13
parseint ("")/NaN
parseint ("0xA")//10 (hexadecimal)
parseint ("")//13
parseint ("070")//es3 is 56 (octal) ES5 for parseint
(070)//es3 and ES5 are all parseint
(22.5)//22

2. Conversion rules:

(1). According to the example (1), parseint will resolve to stop until it is not a number
(2). According to the instance (2) parseint parsing the empty string is Nan, not 0
(3). According to the example (3), parseint can convert the 16 number into the 10 system
(4). According to the example (4) parseint ignore the space of the string

3. Disadvantages:

(1). According to the example (5), we can know that parseint is incompatible when converting octal arrays. ES3 will think of 070 as a octal value, but ES5 will take 070 as a decimal.
(2). According to the example (6) (7) We can know that the parseint will change the parameter to a string and then execute into an integer after the execution.

4. Explanation: Why (5) (6) execution is performed to convert 070 to integers, but the result is different? This is also the 2nd way to solve the problem of how to get the disadvantage.

Because I saw in the official document that the if string was not a string, then it was converted to one. This passage. This means that if the argument is not a string, it converts it to a string and then to an integer. For example (6) parseint (070), in fact, is to convert 070 to a string, you can try the 070+ "" or string (070) can know that 070 will be converted to "56", because 070 is a octal. Then it becomes parseint ("56"), and the last integer that comes out is 56. Whether you're in ES3 or ES5, you're 56.

Second, bitwise operator

1. Examples

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

2. Principle: JavaScript does not have an integer concept, all numeric types are double-precision floating-point numbers. When using the bitwise operator, it turns the operand into an integer to facilitate operation. and 0 is different from other values, or bitwise OR will not change the operation value.

Iii. Math.floor and Math.ceil

1. Examples

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

2. Insufficient: Math.floor gets the smallest integer of the number, and Math.ceil gets the largest integer. So it would have been--2.3, we'd have to get 2, but with Math.floor it was--3. and 2.3 with Math.ceil is 3, but we want 2 on the line.

3. Resolve:

Self-defining functions function
GetInt (val) {return
val>0? Math.floor (val): Math.ceil (val);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.