Categories
JavaScript JavaScript Basics

The Ultimate Guide for Manipulating Numbers in JavaScript

Spread the love

Rounding Numbers

JavaScript has multiple ways to round a number. Some choices are Math.round, number.toFixed, and number.toPrecision. You can also write your own function to round a number up or down to the nearest increment.


Math.round

Math.round rounds a number to the nearest integer. If the decimal part of the number is less than 0.5, it is rounded down. Otherwise, if the decimal part of the number is 0.5 or higher it will be rounded up.

The function returns the rounded number as the value.

For example:

Math.round(12.5); // 13  
Math.round(12.49); // 12

Number.toFixed

You can set the number of digits that appears after the decimal place with the toFixed function. The function returns the string representation of the number as the value.

It can be used like this:

const a = 12.8888888888;  
const b = a.toFixed(2); // 12.88

Number.toPrecision

Number.toPrecision is similar to toFixed.

It returns the string representation of a number but you can round it to the specified number of significant digits, which you can specify or let it automatically round to the correct number of significant digits.

const a = 12.8888888888;  
const b = a.toPrecision(2); // 13, since 2 significant digits is specified  
const c = a.toPrecision(); // 12.8888888888, since all digits are significant in the original number

Round to the Nearest Increment

You can round to the nearest increment, up or down, as you specify:

const roundNumberUp = (num, increment) => {   
  return Math.ceil(num / increment) * increment;  
}  
console.log(roundNumberUp(12.2, 0.5)) // 12.5

What this does is it takes the original number, divides it by the increment you want to round up to, then takes the ceiling of that, then multiplies by the increment. This means that the number should always round up.

Similarly, you can round down to the nearest increment with floor.

const roundNumberUp = (num, increment) => {   
  return Math.floor(num / increment) * increment;  
}  
console.log(roundNumberUp(12.2, 0.5)) // 12.5

What this does is it takes the original number, divides it by the increment you want to round up to, then takes the floor of that, then multiplies by the increment. This means the number should always round down.


Exponentiation

There are multiple ways to compute exponents with JavaScript.

The newest way is the exponentiation operator **, available with ES2016 or higher.

For example, we can do this:

const a = 2 ** 3; // 8

It is right associative, so a ** b ** c is equal to a ** (b ** c). This works with all exponents.

For example:

const a = 2 ** (3 ** 4);  
const b = 2 ** 3 ** 4;  
a == b // true, both are 2.4178516392292583e+24

Detailed browser compatibility is available on the Mozilla website.

We can also use the Math.pow function, like this:

const a = Math.pow(2,3) // 8

It takes two arguments, the first is the base and the second is the exponent. Math.pow works with all exponents.

Math.pow is compatible with all recent browsers.


Rounding Numbers

JavaScript has multiple ways to round a number. Some choices are: Math.round, number.toFixed, and number.toPrecision. You can also write your own function to round a number up or down to the nearest increment.


Math.round

Math.round rounds a number to the nearest integer.

If the decimal part of the number is less than 0.5, it is rounded down. Otherwise, if the decimal part of the number is 0.5 or higher, it will be rounded up. The function returns the rounded number as the value.

For example:

Math.round(12.5); // 13  
Math.round(12.49); // 12

Number.toFixed

You can set the number of digits that appear after the decimal place withthe toFixed function. The function returns the string representation of the number as the value. It can be used like this:

const a = 12.8888888888;  
const b = a.toFixed(2); // 12.88

Number.toPrecision

Number.toPrecision is similar to toFixed.

It returns the string representation of a number but you can round it to the specified number of significant digits, which you can specify or let it automatically round to the correct number of significant digits.

const a = 12.8888888888;  
const b = a.toPrecision(2); // 13, since 2 significant digits is specified  
const c = a.toPrecision(); // 12.8888888888, since all digits are significant in the original number

Round to the Nearest Increment

You can round to the nearest increment up or down you specify:

const roundNumberUp = (num, increment) => {   
  return Math.ceil(num / increment) * increment;  
}  
console.log(roundNumberUp(12.2, 0.5)) // 12.5

What this does is take the original number, divide by the increment you want to round up to, then take the ceiling of that, then multiply by the increment. This means the number should always round up.

Similarly, you can round down to the nearest increment with floor.

const roundNumberUp = (num, increment) => {   
  return Math.floor(num / increment) * increment;  
}  
console.log(roundNumberUp(12.2, 0.5)) // 12.5

What this does is take the original number, divide by the increment you want to round up to, then take the floor of that, then multiply by the increment. This means the number should always round down.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *