- 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.
- BigInt numbers are used for representing integers of arbitrary length. We only use them in a few unique areas.
The Ways of Writing a Number
Let’s say you want to write one billion. This is the most common way:
let billion = 1000000000;
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:
As 0.000001 includes 6 zeroes, it will be 1e-6.
Hexadecimal, Binary and Octal Numbers
0x and then the number.
Here is an example:
Here is an example of the octal numbers :
Generally, developers use binary and octal numeral systems more seldom. These numeral systems are also supported using the following prefixes: 0b and 0o.
This method returns a string representation of num, with a particular base, which can vary from 2 to 36. But the default is 10.
A typical operation while working with numbers is rounding.
Below you can find several built-in functions used for rounding:
With the help of this function, you can easily round down. For example, 6.2 becomes 6, -2.2 becomes-3.
This function does the opposite. It rounds up the numbers. For example, 3.1 will become 4, -2.2 will become -2 .
Using this function will round to the nearest integer. For instance, 3.1 becomes 3, 5.6 becomes 6, and -2.2 becomes -2.
This function removes anything after the decimal point. For example, 6.1 will become 6.
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.
In case the number is too large, it will overflow the 64-bit storage and will potentially give an infinity.
For better understanding, look at this example:
The loss of precision is also a common thing. Check out the following (falsy!) test:
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.
It is vital to know that these are not standard numbers. Therefore, you need to use special functions to check for them.
The isNaN(value) converts its argument into a number. Afterward, tests whether it’s isNaN(value) or not.
- isFinite(value) will convert its argument to a number and return true in case the number is regular, not NaN/Infinity/-Infinity.
You can wonder why it is not possible to use the comparison of === NaN. Just because the NaN is unique in that will not equal anything, even itself:
Let’s have a look at this example:
There are cases when developers use isFinite for validating whether a string value is a regular number, like this:
ParseInt and ParseFloat
A numeric conversion that uses a + or Number() is strict. If a value is not exactly a number, it will fail as follows:
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:
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:
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: