How to use Math.floor() in JavaScript? [SOLVED]


JavaScript

Introduction

In JavaScript, the Math.floor method is a built-in method of the Math object that is used to round a number down to the nearest integer. This method takes a number as its argument, and returns the largest integer that is less than or equal to the given number.

 

Using JavaScript Math.floor() function

Here is an example of how to use the Math.floor method to round a number down to the nearest integer:

let num = 5.75;
let rounded = Math.floor(num);
console.log(rounded);

Output

5

In this example, the Math.floor method is used to round the number 5.75 down to the nearest integer. The method is called with 5.75 as its argument, and it returns the integer 5, which is the largest integer that is less than or equal to 5.75. The result is then logged to the console.

The Math.floor method is commonly used in situations where you need to round a number down to the nearest integer, such as when you are working with discrete values or when you want to make sure that a number stays within a certain range. For example, you might use the Math.floor method to round a number down to the nearest whole number before using it as an array index, or to round a number down to the nearest multiple of a certain value.

Another common use for the Math.floor method is to calculate the number of digits in an integer. This can be useful when you need to know the number of digits in a number for formatting or display purposes. For example, the following code uses the Math.floor method to calculate the number of digits in an integer:

function getNumDigits(num) {
    return Math.floor(Math.log10(num)) + 1;
}

let num = 12345;
let numDigits = getNumDigits(num);
console.log(numDigits);

Output

5

In this example, the getNumDigits function uses the Math.floor method to calculate the number of digits in a given integer. The function first uses the Math.log10 method to calculate the base-10 logarithm of the given number. This will give us the number of digits in the number, but it will include any decimal places after the decimal point. Therefore, we need to use the Math.floor method to round the result down to the nearest integer, and then add 1 to the result to account for the decimal point itself.

In the example, the getNumDigits function is called with the integer 12345 as its argument, and it returns the integer 5, which is the number of digits in the number. The result is then logged to the console.

 

Example: Generate random number

You want to generate a random whole number that falls in a set range (for example, from 1 to 6).

You can use the Math.random() method to generate a floating-point value between 0 and 1. Usually, you’ll scale this fractional value and round it, so you end up with an integer in a specific range. Assuming your range spans from some minimum number min to a maximum number max, here’s the statement you need:

randomNumber = Math.floor(Math.random() * (max - min + 1) ) + min;

For example, if you want to pick a random number between 1 and 6, the code becomes:

const randomNumber = Math.floor(Math.random()*6) + 1;

Now possible values of randomNumber are 1, 2, 3, 4, 5, or 6.

The Math object is stocked full of static utility methods you can call at any time. This recipe uses Math.random() to get a random fractional number, and Math.floor() to truncate the decimal portion, leaving you with an integer.

To understand how this works, let’s consider a sample run-through. First, Math.random() picks a value between 0 and 1, like 0.374324823:

const randomNumber = Math.floor(0.374324823*6) + 1;

That number is multiplied by the number of values in your range (in this example, 6), becoming 2.245948938:

const randomNumber = Math.floor(2.245948938) + 1;

Then the Math.floor() function truncates this to just 2:

const randomNumber = 2 + 1;

Finally, the starting number of the range is added, giving the final result of 3. Repeat this calculation and you’ll get a different number, but it will always be an integer from the range we’ve set of 1 to 6.

 

Summary

The Math.floor method is a built-in method of the Math object in JavaScript that is used to round a number down to the nearest integer. This method is commonly used to round numbers down to the nearest whole number, to calculate the number of digits in an integer, and for other purposes where it is necessary to round a number down to the nearest integer.

 

References

Math.floor() - JavaScript | MDN (mozilla.org)

 

Olorunfemi Akinlua

Olorunfemi Akinlua

He is boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs user-friendly interfaces to enhance digital experiences across various domains. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment