Lodash _.ceil() method
What you’ll learn
- How
_.ceil(number, [precision = 0])maps toMath.ceilplus optional decimal or magnitude rounding. - Why negatives behave like native
Math.ceil(toward+∞), not “a different lodash rule.” - How
_.toNumbercoercion affects strings,null, andundefined. - When the exponential-shift path runs (finite number + non-zero precision) and why it exists.
- Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Comfort with Math.ceil and how IEEE-754 doubles represent decimals. See _.add() for the Math hub entry point and _.toNumber() for coercion details.
Math.ceil: rounds toward+∞; for negatives,Math.ceil(-4.75) === -4.- Optional second argument: precision is clamped with
Math.min(toInteger(precision), 292)in the implementation.
Overview
_.ceil is generated by createRound('ceil'): coerce with _.toNumber, normalize precision (default 0, capped at 292), then either call Math.ceil(number) directly or shift the value in exponential notation, apply Math.ceil on the shifted pair, and shift back—the same pattern Lodash uses for _.round and _.floor.
Round up
Smallest integer ≥ value when precision === 0.
Decimal precision
Positive precision keeps that many fraction digits, always rounding upward.
Negative precision
Snap up to tens, hundreds, etc. (_.ceil(6040, -2) === 6100).
Syntax
_.ceil(number, [precision = 0]) - number: value to round up (coerced with
_.toNumber). - precision: optional; decimal places when positive, magnitude when negative. Omitted or
nullbehaves as0. - Returns: a number (possibly
NaNif the input does not coerce to a finite number before rounding).
Lodash docs baseline
The three official examples: integer ceil, two decimal places, and negative precision.
import ceil from "lodash/ceil";
console.log(ceil(4.006)); // 5
console.log(ceil(6.004, 2)); // 6.01
console.log(ceil(6040, -2)); // 6100 Negatives match Math.ceil
Some older tutorials claim lodash “fixes” negative rounding. It does not: with precision === 0 the implementation calls Math.ceil on the coerced number. With a positive precision, both still round toward +∞ at that decimal rank.
import ceil from "lodash/ceil";
const n = -4.75;
console.log("Math.ceil: " + Math.ceil(n)); // -4
console.log("_.ceil: " + ceil(n)); // -4
console.log("_.ceil(n, 1): " + ceil(n, 1)); // -4.7 Coercion and two-decimal “billing” ceil
_.toNumber parses numeric strings. For currency-style ceilings to two decimals, _.ceil(amount, 2) bumps fractional cents up: 245.651 → 245.66. Values already on a two-decimal grid stay unchanged (245.67 → 245.67).
import ceil from "lodash/ceil";
console.log(ceil("3.2")); // 4 (string coerced)
console.log(ceil(null)); // 0
console.log(ceil(245.67, 2)); // 245.67
console.log(ceil(245.651, 2)); // 245.66 📋 _.ceil vs Math.ceil
| Feature | _.ceil(n, p) | Math.ceil(n) |
|---|---|---|
| Second argument (precision) | Yes | No (always integer result) |
| Input coercion | _.toNumber first | Standard ToNumber via call |
p === 0 on a finite number | Same as Math.ceil(_.toNumber(n)) | Native only |
| Negative numbers | Same direction as native (toward +∞) | Same |
| Non-zero precision | Exponential shift + Math.ceil | Not available |
Pitfalls to avoid
Negatives are not “fixed”
Lodash does not redefine ceiling for negatives. If you need rounding toward zero or toward negative infinity, use Math.trunc, Math.floor, or _.floor instead.
Binary floats still apply
The shift trick reduces some artifacts but does not turn IEEE-754 into decimal arithmetic. For ledger-grade sums, work in integer minor units or a decimal library.
undefined and bad strings
_.ceil(undefined) is NaN. JSON.stringify(NaN) is null, which confuses logging—use Number.isNaN checks after coercion.
Throws on BigInt
_.ceil(1n) throws before rounding because _.toNumber rejects BigInt.
❓ FAQ
Summary
- Purpose: round up with optional positive or negative precision, sharing one implementation with
_.floorand_.round. - Remember:
precision === 0is nativeMath.ceilafter_.toNumber; negatives are not a special lodash case. - Next: Lodash _.divide(), or the official Lodash docs for _.ceil (same
createRoundpattern as_.floor/_.round).
_.ceil, _.floor, and _.round are three thin wrappers around the same factory: createRound('ceil') (etc.). When precision is non-zero and the value is finite, lodash uses the exponential-shift trick from the MDN Math.round examples to reduce floating-point drift—then still calls the native Math[method] on the shifted mantissa.
6 people found this page helpful
