Numbers in JavaScript (Part 1)

Numbers in JavaScript (Part 1)

Numbers in JavaScript are stored in 64-bit format [IEEE-754] also known as "double precision" or "double" in short.

Exponentials

Some numbers are either too small or too large to be perfect written in normal notation. For example, in 12 grams of carbon there is about 600 000 000 000 000 000 000 000 atoms. The easiest way to write such a number is to write it in standard form as 6 × 1023 atoms. Lets see how JavaScript deals with such a number:

let atoms = 600000000000000000000000;

console.log(atoms);               //> 6e+23

As you can see, JavaScript prints the number as 6e+23 which is shorter. 6e23 or 6e+23 means 6 × 1023. The number is shortened by using "e" and adding the zeroes count. For example, 2e3 is short for 2000, 8e7 is short for 80 000 000.

let num = 6e2;

console.log(num);               //> 600

The same applies for very small numbers. Lets say a certain program runs in 1 millisecond. In seconds we would write that number as 0.001 seconds. In standard form we write it as 6 × 10-3 which basically translate to 3 zeroes before the 1. Such a number, in JavaScript, is written as 1e-3.

let time = 1e-3;

console.log(time);               //> 0.001

Hexadecimal, binary and octal numbers

Hexadecimal numbers are commonly used to represent colors, encode characters, etc. We use 0x at the beginning of the number to tell the JavaScript engine that this is a hexadecimal number.

For instance:

let color1 = 0xFED;
let color2 = 0xfed;

console.log( color1 );               //> 4077
console.log( color2 );               //> 4077

The hexadecimal counting system is in base 16 in which the numbers are counted as follows: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. Another thing you should note is that in hexadecimal the letters are case insensitive: F and f are the same.

Binary numbers (base 2) are written starting with 0b.

let maskBits = 0b11011101;

console.log( maskBits );            //> 221

Octal numbers (base 8) are written starting with 0o.

let maskBytes = 0o230307;

console.log( maskBytes );            //> 78023

toString

The number.toString(base) method returns a string representing the number in the given base. For example, here is how to convert a number to different bases:

let myAge = 33;

console.log(myAge.toString(2));            //> 100001
console.log(myAge.toString(3));            //> 1020
console.log(myAge.toString(4));            //> 201
console.log(myAge.toString(5));            //> 113
console.log(myAge.toString(6));            //> 53
console.log(myAge.toString(7));            //> 45
console.log(myAge.toString(8));            //> 41

The base can be anything from 2 to 36. The default base is 10.

Take note that if you call this method as it is on a number you get an error:

console.log(23.toString(2));        //> SyntaxError: Invalid or unexpected token

This is because JavaScript treats a dot after a number as a decimal point. So the correct way to call it on a number would be:

console.log(23..toString(2));            //> 10111

Rounding numbers

JavaScript has a built-in Math library for dealing with numbers. The Math library has several built-in functions for rounding numbers:

Math.floor

Math.floor(num) rounds down a number to the nearest integer:

console.log(Math.floor(3.2));     //> 3
console.log(Math.floor(3.4));     //> 3
console.log(Math.floor(3.6));     //> 3
console.log(Math.floor(3.8));     //> 3
console.log(Math.floor(-3.2));     //> -4
console.log(Math.floor(-3.4));     //> -4
console.log(Math.floor(-3.6));     //> -4
console.log(Math.floor(-3.8));     //> -4

Math.ceil

Math.ceil(num) rounds up a number to the nearest integer:

console.log(Math.ceil(3.2));     //> 4
console.log(Math.ceil(3.4));     //> 4
console.log(Math.ceil(3.6));     //> 4
console.log(Math.ceil(3.8));     //> 4
console.log(Math.ceil(-3.2));     //> -3
console.log(Math.ceil(-3.4));     //> -3
console.log(Math.ceil(-3.6));     //> -3
console.log(Math.ceil(-3.8));     //> -3

Math.round

Math.round(num) rounds off a number to the nearest integer:

console.log(Math.round(3.2));     //> 3
console.log(Math.round(3.4));     //> 3
console.log(Math.round(3.6));     //> 4
console.log(Math.round(3.8));     //> 4
console.log(Math.round(-3.2));     //> -3
console.log(Math.round(-3.4));     //> -3
console.log(Math.round(-3.6));     //> -4
console.log(Math.round(-3.8));     //> -4

Now here is the thing about rounding off; if the digit after the decimal point is greater than or equal to 5, the number is rounded up. Otherwise it is rounded down. Just like in elementary algebra.

Math.trunc

Math.trunc removes anything after the decimal point without rounding:

console.log(Math.trunc(3.2));     //> 3
console.log(Math.trunc(3.4));     //> 3
console.log(Math.trunc(3.6));     //> 3
console.log(Math.trunc(3.8));     //> 3
console.log(Math.trunc(-3.2));     //> -3
console.log(Math.trunc(-3.4));     //> -3
console.log(Math.trunc(-3.6));     //> -3
console.log(Math.trunc(-3.8));     //> -3

This method is not supported by Internet Explorer.

Rounding to a certain degree of accuracy

For rounding off a number to a specified degree of accuracy we use the num.toFixed() method:

let num = 9.87654321;

console.log( num.toFixed(1) );     //> 9.9
console.log( num.toFixed(2) );     //> 9.88
console.log( num.toFixed(3) );     //> 9.877
console.log( num.toFixed(4) );     //> 9.8765
console.log( num.toFixed(5) );     //> 9.87654
console.log( num.toFixed(6) );     //> 9.876543
console.log( num.toFixed(7) );     //> 9.8765432
console.log( num.toFixed(8) );     //> 9.87654321

However there is something you should note about this method, it returns a string representation of a number, not a number. For example:

let num = 1.2345;

let one = num.toFixed(1);
let two = num.toFixed(2);

console.log(one);        //> 1.2
console.log(two);        //> 1.23

let three = one + two;

// the numbers are concatenated because they are string type
console.log(three);       //> 1.21.23

We can make it return a number type by prepending a + sign to the method.

let num = 1.2345;

let one = +num.toFixed(1);
let two = +num.toFixed(2);

console.log(one);        //> 1.2
console.log(two);        //> 1.23

let three = one + two;

console.log(three);       //> 2.4299999999999997