Numbers
JavaScript Numbers (JavaScript编号)
Two types of numbers can be highlighted in modern JavaScript: Regular and bigInt.
Regular ones are stored in the format of 64-bit IEEE-754. It is also known as “double-precision floating-point numbers”. Developers use these numbers most in their practice. (-常规文件以64位IEEE-754格式存储。它也被称为“双精度浮点数”。开发人员在实践中最常使用这些数字。)
BigInt numbers are used for representing integers of arbitrary length. We only use them in a few unique areas. (- BigInt数字用于表示任意长度的整数。我们只在少数独特的地区使用它们。)
The Ways of Writing a Number
The Ways of Writing a Number (书写数字的方法)
Let’s say you want to write one billion. This is the most common way:
let billion = 1000000000;
In our practice, we commonly try to keep away from writing many zeroes. We prefer to write, for example, “1bn”. Now, we will learn how to shorten a number in JavaScript. (在我们的实践中,我们通常会尽量避免写很多零。 例如,我们更喜欢写“10亿”。 现在,我们将学习如何在JavaScript中缩短数字。)
To shorten a number in JavaScript, you need to append the letter “e” to the number and specify the zeroes count. It will look like this:
let million = 1e6; // 1 million, literally: 1 and 6 zeroes
console.log(2.5e6); // 2.5 millions (2,500,000)
let exponential = 2.56e3;
console.log(exponential); // 2560
Now, imagine you want to write “one microsecond”. You need to do it as follows:
let ms = 0.000001;
If you are eager to avoid writing a large number of zeros, you should act like this:
let ms = 1e-6; // six zeroes to the left from 1
console.log(ms);
As 0.000001 includes 6 zeroes, it will be 1e-6. (由于0.000001包括6个零,因此它将是1e-6。)
Hexadecimal, Binary and Octal Numbers
Hexadecimal, Binary and Octal Numbers (十六进制、二进制和八进制数字)
In Javascript, we use hexadecimal numbers, also known as Hex, for representing colors, encoding characters, and a lot more. Fortunately, there is a shorter way to write them:
0x and then the number. (0x ,然后是数字。)
Here is an example:
let hex = 0xfff;
console.log(hex); // 4095
console.log(0xff); // 255
console.log(0xFF); // 255 (the same, case doesn't matter)
Here is an example of the octal numbers :
let octal = 030;
console.log(octal); // 24
Generally, developers use binary and octal numeral systems more seldom. These numeral systems are also supported using the following prefixes: 0b and 0o.
For instance:
let a = 0b11111111; // binary form of 255
let b = 0o377; // octal form of 255
console.log(a == b); // true
toString (Base)
toString (Base)
This method returns a string representation of num, with a particular base, which can vary from 2 to 36. But the default is 10. (此方法返回具有特定基数的num字符串表示形式,基数从2到36不等。但默认值为10。)
For example:
let num = 255;
console.log(num.toString(16)); // ff
console.log(num.toString(2)); // 11111111
Rounding
Rounding (取整)
A typical operation while working with numbers is rounding. (处理数字时的典型操作是舍入。)
Below you can find several built-in functions used for rounding:
Math.floor
With the help of this function, you can easily round down. For example, 6.2 becomes 6, -2.2 becomes-3. (借助此功能,您可以轻松向下舍入。例如, 6.2变为6 , -2.2变为-3。)
Math.ceil
This function does the opposite. It rounds up the numbers. For example, 3.1 will become 4, -2.2 will become -2 . (这个函数的作用正好相反。它将数字四舍五入。例如, 3.1将变为4 , -2.2将变为-2。)
Math.round
Using this function will round to the nearest integer. For instance, 3.1 becomes 3, 5.6 becomes 6, and -2.2 becomes -2. (使用此函数将四舍五入到最接近的整数。例如, 3.1变为3 , 5.6变为6 , -2.2变为-2。)
Math.trunc
This function removes anything after the decimal point. For example, 6.1 will become 6. (此函数删除小数点后的任何内容。例如, 6.1将变为6。)
Imprecise Calculation
Imprecise Calculation (计算不精确)
In-64 bit format IEEE-754, there are 64 bits for storing a number. 52 of them are used for storing the digits, 11 of them - for the position of the decimal point, and 1- for the sign. (在64位格式的IEEE-754中,有64位用于存储数字。其中52位用于存储数字, 11位用于小数点的位置, 1位用于符号。)
In case the number is too large, it will overflow the 64-bit storage and will potentially give an infinity. (如果数字太大,它将溢出64位存储,并可能产生无穷大。)
For better understanding, look at this example:
console.log(2e500); // Infinity
The loss of precision is also a common thing. Check out the following (falsy!) test:
console.log(0.1 + 0.2 == 0.3); // false
Tests: isFinite and isNan
Tests: isFinite and isNan
First of all, let’s check out the following two unique numeric values:
Infinity (and -Infinity). This numeric value is higher (less) than anything else. NaN shows an error. (无穷大(和-无穷大)。此数值高于(小于)任何其他数值。 NaN显示错误。)
It is vital to know that these are not standard numbers. Therefore, you need to use special functions to check for them. (重要的是要知道这些不是标准数字。因此,您需要使用特殊函数来检查它们。)
console.log(isNaN(NaN)); // true
console.log(isNaN("str")); // true
Let’s have a look at this example:
console.log(isFinite("23")); // true
console.log(isFinite("str")); // false, because a special value: NaN
console.log(isFinite(Infinity)); // false, because a special value: Infinity
There are cases when developers use isFinite for validating whether a string value is a regular number, like this:
let num = +prompt("Enter a number", '');
console.log(isFinite(num));// if you don't enter Infinity, -Infinity or Nan, then will be true
ParseInt and ParseFloat
ParseInt and ParseFloat (ParseInt和ParseFloat)
A numeric conversion that uses a + or Number() is strict. If a value is not exactly a number, it will fail as follows:
console.log(+"200px"); // NaN
The spaces at the start or the end of the string are the only exception, as they are ignored. (字符串开头或结尾的空格是唯一的例外,因为它们被忽略。)
By the way, in CSS, you can meet values in units, like “20pt” or “50px”. Moreover, there are many countries where the symbol of the currency goes after the amount. For example, “19$”. If you want to extract a numeric value out of it, you can use parseInt and parseFloat. These functions will read from a string to the point they can’t. If an error occurs, the number that has been gathered will be returned. The parseInt will return an integer, while parseFloat- the floating-point number, as follows:
console.log(parseInt('50px')); // 50
console.log(parseFloat('22.5em')); // 22.5
console.log(parseInt('20.4$')); // 20, only the integer part is returned
console.log(parseInt('22.2')); // 22, only the integer part is returned
console.log(parseFloat('22.2.4')); // 22.2, the second point stops the reading
In some situations, parseInt and parseFloat functions will not return the NaN. It can happen when no digits are found. Check out the following example:
console.log(parseInt('a13')); // NaN, the first symbol stops the process
The parseInt() function has an alternative parameter which specifies the numeral system base. So, parseInt may parse the strings of hex numbers, binary numbers, etc:
console.log(parseInt('0xff', 16)); // 255
console.log(parseInt('ff', 16)); // 255, without 0x also works
console.log(parseInt('2n9', 36)); // 3429