CyrefJS
CCyrefJS
فا
Back to documentation
Number/Since 1.38.0

floorTo

Rounds a number downward to the specified number of decimal places.

floorTo(value, decimals):number

Parameters

valuenumberrequired

The number to round downward.

decimalsnumberrequired

The number of decimal places to preserve. Must be an integer.

Returns

number

Returns 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

01

Basic usage

Rounds the value downward to two decimal places.

TypeScript
const result = floorTo(4.129, 2);

console.log(result);
// 4.12
02

Value already at the precision

If the value does not require rounding at the requested precision, it remains unchanged.

TypeScript
const result = floorTo(4.12, 2);

console.log(result);
// 4.12
03

Zero decimals

When decimals is 0, Math.floor is used to round the value to an integer.

TypeScript
console.log(floorTo(4.1, 0));
// 4

console.log(floorTo(4.9, 0));
// 4
04

Negative values

The value is rounded toward negative infinity.

TypeScript
console.log(floorTo(-4.123, 2));
// -4.13

console.log(floorTo(-4.1, 0));
// -5
05

More decimal places

A larger decimals value preserves more decimal places.

TypeScript
console.log(floorTo(3.14159, 3));
// 3.141

console.log(floorTo(3.14159, 4));
// 3.1415
06

Negative decimals

Negative decimal places round the value downward at positions to the left of the decimal point.

TypeScript
console.log(floorTo(1234, -2));
// 1200

console.log(floorTo(1250, -2));
// 1200
07

Infinity

Finite values are rounded, while positive or negative Infinity is returned unchanged.

TypeScript
console.log(floorTo(Infinity, 2));
// Infinity

console.log(floorTo(-Infinity, 2));
// -Infinity
08

NaN

NaN is rejected and causes a RangeError.

TypeScript
floorTo(NaN, 2);
// RangeError: floorTo: value must not be NaN
09

Non-integer decimals

The decimals argument must be an integer.

TypeScript
floorTo(4.123, 2.5);
// RangeError: floorTo: decimals must be an integer
Built with@cyref/js·Official documentation