In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how javascript converts values to int types. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Javascript converts a value to an int type: 1, use the "parseInt" method to convert a value into an int integer; 2, use the "Math.floor (value)", "Math.ceil (value)", "Math.round (value)" methods to convert decimal values into int integers.
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
Method 1: use parseInt ()
ParseInt () is a global method that converts values to integers. The conversion process is as follows:
Parse the character at position 0 first, and return NaN directly if it is not a valid number.
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, and if it is not a valid number, directly return the valid number at position 0.
And so on, analyze each character one by one from left to right until a non-numeric character is found.
ParseInt () converts all the legal numeric characters from the previous analysis to numeric values and returns them.
Console.log (parseInt ("123abc")); / / return numeric 123console.log (parseInt ("1.73")); / / return numeric 1console.log (parseInt (".123")); / / return value NaN
The point in the floating point is an illegal character for parseInt (), so the value of the decimal part is not converted.
If it is a numeric string that begins with 0, parseInt () treats it as an octal number: it is converted to an octal value and then returned as a decimal number.
If it is a numeric string that begins with 0x, parseInt () treats it as a hexadecimal number: it is converted to a hexadecimal value and then returned as a decimal number.
Var d = 010; / / octal number string var e = 0x10; / / hexadecimal number string console.log (parseInt (d)); / / return decimal number 8console.log (parseInt (e)); / / return decimal number 16
Method 2: use Math.floor (), Math.ceil (), Math.round () to convert decimals to integers
The Math object has three static functions that can be rounded
Math.floor (): returns the largest integer that is less than the parameter value.
Math.ceil (): returns the smallest integer greater than the parameter value.
Math.round (): rounded.
Example 1: the following code simply compares the values of these three methods
Console.log (Math.floor (2.5)); / / 2console.log (Math.floor (- 2.5)); / /-3console.log (Math.ceil (2.5)); / / 3console.log (Math.ceil (- 2.5)); / /-2console.log (Math.round (2.5)); / / 3console.log (Math.round (- 2.5)); / /-2console.log (Math.round (- 2. 6); / /-3
Example 2: the following code combines the Math.floor () and Math.ceil () methods to design a number rounding function using JavaScript
Var toInt = function (num) {var num = Number (num); return num < 0? Math.ceil (num): Math.floor (num);} console.log (toInt (2.5)); / / 2console.log (toInt (- 2.5)); / /-2 Thank you for reading! This is the end of this article on "how javascript converts values into int types". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.