How To Round Down A Number In JavaScript?

Round Down a Number in JavaScript

In JavaScript, there are many built-in methods used to round a number. Besides, you can also write your function to round a number. This article will show you how to round down a number in JavaScript. Read on to find out how.

Round down a number in JavaScript

Using Math.floor() method

The Math.floor() method rounds a number down to the nearest integer. The return result is an integer less than or equal to a given number.

Syntax:

Math.floor(x);

Parameters:

  • x: a number you want to round down.

If you pass an integer to the method, it will return that integer.

If a non-numeric value is passed, the method will return NaN.

To round down a number, we pass the number to be rounded to the Math.floor() method. It will then return an integer. Like this:

let number = 323.65;

// Use Math.floor
number = Math.floor(number);
console.log(number);

Output:

323

Using Math.trunc() method

The Math.trunc() method cuts off the decimal part of a number and returns the integer part.

Syntax:

Math.trunc(x)

Parameters:

  • x: a number you want to round down.

Note before using Math.trunc(): if you pass a negative number to Math.trunc(), you must subtract one from the return result. Because in the case of negative numbers, Math.trunc() only cuts off the decimal part; it doesn’t round down.

See the example below to understand.

let positiveNumber = 323.65;
let negativeNumber = -323.65;

// Use Math.trunc for positive number
let number = Math.trunc(positiveNumber);
console.log(number); // expected output: 323

// Use Math.trunc for negative number
number = Math.trunc(negativeNumber) - 1;
console.log(number); // expected output: -324

// Use Math.floor for negative number
number = Math.floor(negativeNumber);
console.log(number); // expected output: -324

Output:

323
-324
-324

Using the remainder operation.

In this case, we will use the remainder operator to remove the decimal part of a number. When we get the remainder of a number by 1, the decimal part of the number will be returned. If we use a number minus its decimal part, that means we have rounded the number.

Like the solution above, this solution only cut out the decimal part. Therefore, for negative numbers, we have to subtract 1 to round down the number. Look carefully at the example below to understand.

let positiveNumber = 323.65;
let negativeNumber = -323.65;

// Use % to round down a number
function roundDown(num) {
  if (num > 0) {
    // For the positive number
    return num - (num % 1);
  } else {
    // For the negative number
    return num - (num % 1) - 1;
  }
}

// Using roundDown() for positive number
console.log(roundDown(positiveNumber)); // expected output: 323

// Using roundDown() for negative number
console.log(roundDown(negativeNumber)); // expected output: -324

// Using Math.floor() for negative number
console.log(Math.floor(negativeNumber)); // expected output: -324

Output:

323
-324
-324

Summary

In summary, we have shown you 3 ways to round down a number in JavaScript using Math.floor(), Math.trunc(), and remainder operation. We recommend using Math.floor() because it is the best way in this case. If you have any problem, please comment below.

Have a nice day!

Maybe you are interested:

Leave a Reply

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