Math.floor is a function in JavaScript that rounds a number down to the nearest integer. It is often used when you need to truncate a decimal value. In this article, we will answer common questions about what math.floor does and how to use it in your code. We will also provide examples of how it can be useful in real-world applications.
What is Javascript Math.floor?
Before we look at where the floor function may be used, let’s first understand what it does.
Math.floor is a function that rounds a number down to the nearest integer. It is often used when you need to truncate a decimal value. For example, if you have a decimal value of 0.55 and you use math.floor on it, the result will be 0. In JavaScript, there are two ways to use math.floor: as a method or as a property.
As a Method: The first way to use math.floor is as a method. To do this, simply call the function with the number you want to round down as an argument. The Javascript Math.ceil() function, for example, returns the closest integer that is greater or equal to the supplied number.
Using Math.floor()
Math.floor( 45.95); // 45
Math.floor( 45.05); // 45
Math.floor( 4 ); // 4
Math.floor(-45.05); // -46
Math.floor(-45.95); // -46
Decimal adjustment
/**
* Decimal adjustment of a number.
*
* @param {String} type The type of adjustment.
* @param {Number} value The number.
* @param {Integer} exp The exponent (the 10 logarithm of the adjustment base).
* @returns {Number} The adjusted value.
*/
function decimalAdjust(type, value, exp) {
// If the exp is undefined or zero...
if (typeof exp === 'undefined' || +exp === 0) {
return Math[type](value);
}
value = +value;
exp = +exp;
// If the value is not a number or the exp is not an integer...
if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
return NaN;
}
// Shift
value = value.toString().split('e');
value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp)));
// Shift back
value = value.toString().split('e');
return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp));
}
// Decimal round
const round10 = (value, exp) => decimalAdjust('round', value, exp);
// Decimal floor
const floor10 = (value, exp) => decimalAdjust('floor', value, exp);
// Decimal ceil
const ceil10 = (value, exp) => decimalAdjust('ceil', value, exp);
// Round
round10(55.55, -1); // 55.6
round10(55.549, -1); // 55.5
round10(55, 1); // 60
round10(54.9, 1); // 50
round10(-55.55, -1); // -55.5
round10(-55.551, -1); // -55.6
round10(-55, 1); // -50
round10(-55.1, 1); // -60
// Floor
floor10(55.59, -1); // 55.5
floor10(59, 1); // 50
floor10(-55.51, -1); // -55.6
floor10(-51, 1); // -60
// Ceil
ceil10(55.51, -1); // 55.6
ceil10(51, 1); // 60
ceil10(-55.59, -1); // -55.5
ceil10(-59, 1); // -50
Conclusion
As you can see, the Math.floor function is a simple but powerful tool that can be used in a variety of situations. We hope this article has helped you to better understand how it works and when you might want to use it in your own code. Thanks for reading!