How to use Math.floor() In Javascript

The Math.floor() method rounds a number down to the nearest integer.

const num = 3.14159;
// Rounds down to 2 decimal places
const roundedNum = Math.floor(num * 100) / 100;
console.log(roundedNum); // Output: 3.14

How to Round a Number to a Certain Number of Decimal Places in JavaScript ?

Rounding a number to a certain number of decimal places in JavaScript means adjusting the number to a specified precision after the decimal point. This is commonly done using methods like toFixed() or Math.round() to format the number for precise calculations or display.

Here’s how you can achieve it using these methods:

Similar Reads

Using toFixed() method

The toFixed() method returns a string representing the number rounded to a specified number of decimal places....

Using Math.round()

The Math.round() method rounds a number to the nearest integer....

Using Math.floor()

The Math.floor() method rounds a number down to the nearest integer....

Using Math.ceil()

The Math.ceil() method rounds a number up to the nearest integer....