In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people don't understand the knowledge points of this article "Data Type Number Instance Analysis in JavaScript", so Xiaobian summarizes the following contents for everyone. The contents are detailed, the steps are clear, and they have certain reference value. I hope everyone can gain something after reading this article. Let's take a look at this article "Data Type Number Instance Analysis in JavaScript".
preface
The Number type uses IEEE 754 format to represent integer and floating-point values (also called double-precision values in some languages).
IEEE Binary Floating-Point Arithmetic Standard
IEEE 754 specifies four ways to represent floating-point values: single precision (32 bits), double precision (64 bits), extended single precision (43 bits or more, rarely used), and extended double precision (79 bits or more, usually implemented in 80 bits).
js is double precision (64 bits)
Different numeric types have different numeric literal formats:
decimal integer
Octal (base 8)
hexadecimal (base 16)
1, floating point number
The value must contain a decimal point and must be followed by at least one digit.
Classic question: 0.1 + 0.2 == 0.3? The answer is not equal;
Because rounding errors exist in floating-point operations, they occur because IEEE754 values are used and are not unique to ECMAScript. Any language that uses this format has this problem.
2. Range of values
Minimum Value: Number.MIN_VALUE 5e-324
Maximum Value: Number.MAX_VALUE 1.797 693 134 862 315 7e+308
Value out of JavaScript range: Infinity-Infinity
Determining whether a value is finite: isFinite() function
3、NaN
Meaning: not a number
Indicates that an operation that was supposed to return a value failed (instead of throwing an error)
Any operation involving NaN always returns NaN
NaN is not equal to any value including NaN, i.e. console.log(NaN == NaN); // false
isNaN() function to determine whether the parameters passed into it are not numerical values;
isNaN() tries to convert the argument to a numeric value
console.log(isNaN(NaN)); // trueconsole.log(isNaN('abc')); // trueconsole.log(isNaN('123')); // falseconsole.log(isNaN(true)); // falseconsole.log(isNaN(12)); // false
The valueOf() method of the object is called first, and then it is determined whether the returned value can be converted to a numeric value. If not, call toString() again and test its return value.
4. Numerical conversion
Convert non-numeric values to numeric values
The Number() function can be used for any data type:
Data type conversion rule Boolean true = 1, false = 0 Numeric value returns null0 undefined NaN string Empty string returns 0 Numeric character returns the corresponding number (the previous contains plus and minus, floating point value, hexadecimal format will be converted to the corresponding decimal integer value)
Except for the above cases, NaN is returned.
Object calls the valueOf() method and converts the returned value according to the rules described above. If the conversion result is NaN, call the toString() method and follow the rules for converting strings. console.log (Number(false)); // 0console.log (Number(null)); // 0console.log (Number(undefined)); // NaNconsole.log (Number(1)); // 1console.log (Number(0012)); // 10 octal conversion console.log (Number(070)); // 56 octal conversion console.log (Number(0x1f)); // 31 Hexadecimal to decimal integer console.log (Number(018)); // 18 ignore previous 0console.log (Number(12.23)); // 12.23console.log(Number('123')); // 123console.log(Number('123bule')); // NaNconsole.log(Number(' 123')); // 123console.log(Number('')); // 0
parseInt() function ++ Use parseInt() function first when you need to get integers;++(Number() function is relatively complex to convert strings, if it is clear that you want to round, use parseInt() function first)
The parseInt() function ignores the first white space in the string and starts converting at the first non-white character; if the first character is not a numeric character, plus or minus, parseInt() immediately returns NaN.
parseInt() is more concerned with whether the string contains a numeric value, as follows:
console.log (parseInt(123));//123console.log (parseInt(0123)); // 83 interpreted as octal integer. console.log (parseInt(false)); // NaNconsole.log (parseInt('123')); // 123console.log (parseInt('0123')); // 123console.log (parseInt('123.23')); // 123console.log (parseInt('123bule')); // 123console.log (parseInt(' 123')); // 123console.log (parseInt('23.74')); // 23console.log (parseInt ('')); // NaN//distinguish console.log from Number()(Number('')); // 0console.log (Number(false)); // 0console.log (Number ('123 bule')); // 123//From this you can see that parseInt() focuses more on whether the string contains the value parseInt("0xAF", 16); // 175parseInt("AF", 16); // 175 provides a hexadecimal argument, which can omit 0xparseInt("AF"); // NaN , not allowed without providing the second parameter
Not passing the base parameter is equivalent to letting parseInt() decide how to parse it, so in order to avoid parsing errors, it is recommended to always pass it the second parameter.
The parseInt() function can take a second argument specifying the base (base);
As follows:
parseFloat() function is similar to parseInt() function. The difference is that it recognizes valid decimal points (that is, the first occurrence of decimal points, and subsequent occurrences of decimal points are ignored); the other is to always ignore the zero at the beginning of the string; parseFloat() only resolves decimal values, cannot specify the base; hexadecimal values always return 0
As follows:
console.log (parseFloat('123bule')); // 123console.log (parseFloat('23.74')); // 23.74console.log (parseFloat('023.74')); // 23.74console.log (parseFloat('23.74.2')); // 23.74console.log (parseFloat(0xAF)); // 175console.log (parseFloat('0xAF')); // 0console.log (parseFloat(012)); // 10console.log (parseFloat ('012 ')); // 12 The above is about the content of this article on "Data Type Number Instance Analysis in JavaScript". I believe everyone has a certain understanding. I hope that the content shared by Xiaobian will be helpful to everyone. If you want to know more related knowledge, please pay attention to the industry information channel.
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.