CyrefJS
CCyrefJS
فا
Back to documentation
Number/Since 1.38.0

ceilTo

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

ceilTo(value, decimals):number

Parameters

valuenumberrequired

The number to round upward.

decimalsnumberrequired

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

Returns

number

Returns the value rounded upward 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 upward to two decimal places.

TypeScript
const result = ceilTo(4.123, 2);

console.log(result);
// 4.13
02

Value already at the precision

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

TypeScript
const result = ceilTo(4.12, 2);

console.log(result);
// 4.12
03

Zero decimals

When decimals is 0, Math.ceil is used to round the value to the next integer.

TypeScript
console.log(ceilTo(4.1, 0));
// 5

console.log(ceilTo(4.9, 0));
// 5
04

Negative values

The value is rounded toward positive infinity.

TypeScript
console.log(ceilTo(-4.123, 2));
// -4.12

console.log(ceilTo(-4.9, 0));
// -4
05

More decimal places

A larger decimals value preserves more decimal places.

TypeScript
console.log(ceilTo(3.14159, 3));
// 3.142

console.log(ceilTo(3.14159, 4));
// 3.1416
06

Negative decimals

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

TypeScript
console.log(ceilTo(1234, -2));
// 1300

console.log(ceilTo(1250, -2));
// 1300
07

Infinity

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

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

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

NaN

NaN is rejected and causes a RangeError.

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

Non-integer decimals

The decimals argument must be an integer.

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