Lodash _.ceil Precision Rules for Rounding Up
The _.ceil method in the Lodash JavaScript library
computes a number rounded up to a specified precision. While
JavaScript’s native Math.ceil only rounds up to the nearest
whole integer, Lodash extends this functionality by accepting an
optional precision argument. Depending on whether this precision
parameter is omitted, positive, or negative, _.ceil applies
distinct mathematical rules to round numbers to the nearest integer,
decimal place, or power of ten while handling floating-point arithmetic
errors.
The Syntax of _.ceil
The function signature for _.ceil is defined as:
_.ceil(number, [precision=0])number(number): The number to round up.[precision=0](number): The precision to which to round up. Defaults to0if omitted.
Precision Rules Applied by
_.ceil
1. Default Precision
(precision = 0)
When the precision parameter is omitted or set to
0, _.ceil behaves identically to standard
Math.ceil. It rounds the number up to the next highest
integer.
_.ceil(4.006)returns5_.ceil(6.0)returns6_.ceil(-4.2)returns-4
2. Positive Precision
(precision > 0)
When a positive integer is passed as the precision,
_.ceil rounds up to the specified number of digits to the
right of the decimal point (fractional digits).
A precision of n rounds the number up to the nearest
multiple of \(10^{-n}\).
_.ceil(6.004, 2)returns6.01_.ceil(6.012, 1)returns6.1_.ceil(0.12345, 4)returns0.1235
3. Negative Precision
(precision < 0)
When a negative integer is used, _.ceil rounds up to the
specified place to the left of the decimal point,
targeting powers of ten (tens, hundreds, thousands, etc.).
A precision of -n rounds the number up to the nearest
multiple of \(10^n\).
_.ceil(6040, -2)rounds to the nearest hundred: returns6100_.ceil(1234, -1)rounds to the nearest ten: returns1240_.ceil(450, -3)rounds to the nearest thousand: returns1000
4. Precision Normalization and Coercion
Lodash coerces the precision argument to an integer
using internal integer conversion (toInteger). If a
floating-point number is provided as the precision:
- It is truncated to its integer representation before calculating the rounding offset.
_.ceil(4.1234, 2.8)treats the precision as2, returning4.13.
If precision is NaN, null, or
undefined, it defaults to 0.
Floating-Point Precision Handling
A common issue in standard JavaScript calculations is binary
floating-point representation error (for example,
1.005 * 100 = 100.49999999999999).
To avoid the inaccuracies caused by standard multiplication and division scaling, Lodash handles precision internally using exponential notation conversion:
- The value is converted into scientific exponential notation
(
value + 'e' + precision). Math.ceilis applied to this adjusted value.- The number is converted back by reversing the exponent
(
value + 'e-' + precision).
This ensures that values on rounding boundaries are not shifted into inaccurate rounding directions by standard IEEE 754 precision limits.