1.38.0floorTo
Rounds a number downward to the specified number of decimal places.
floorTo(value, decimals):numberParameters
valuenumberrequiredThe number to round downward.
decimalsnumberrequiredThe number of decimal places to preserve. Must be an integer.
Returns
numberReturns the value rounded downward to the specified decimal precision.
Errors
Throws a RangeError if value is NaN.
Throws a RangeError if decimals is not an integer.
Examples
Basic usage
Rounds the value downward to two decimal places.
const result = floorTo(4.129, 2);
console.log(result);
// 4.12Value already at the precision
If the value does not require rounding at the requested precision, it remains unchanged.
const result = floorTo(4.12, 2);
console.log(result);
// 4.12Zero decimals
When decimals is 0, Math.floor is used to round the value to an integer.
console.log(floorTo(4.1, 0));
// 4
console.log(floorTo(4.9, 0));
// 4Negative values
The value is rounded toward negative infinity.
console.log(floorTo(-4.123, 2));
// -4.13
console.log(floorTo(-4.1, 0));
// -5More decimal places
A larger decimals value preserves more decimal places.
console.log(floorTo(3.14159, 3));
// 3.141
console.log(floorTo(3.14159, 4));
// 3.1415Negative decimals
Negative decimal places round the value downward at positions to the left of the decimal point.
console.log(floorTo(1234, -2));
// 1200
console.log(floorTo(1250, -2));
// 1200Infinity
Finite values are rounded, while positive or negative Infinity is returned unchanged.
console.log(floorTo(Infinity, 2));
// Infinity
console.log(floorTo(-Infinity, 2));
// -InfinityNaN
NaN is rejected and causes a RangeError.
floorTo(NaN, 2);
// RangeError: floorTo: value must not be NaNNon-integer decimals
The decimals argument must be an integer.
floorTo(4.123, 2.5);
// RangeError: floorTo: decimals must be an integer