Lodash _.ceil() method

Beginner
⏱️ 6 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • How _.ceil(number, [precision = 0]) maps to Math.ceil plus optional decimal or magnitude rounding.
  • Why negatives behave like native Math.ceil (toward +∞), not “a different lodash rule.”
  • How _.toNumber coercion affects strings, null, and undefined.
  • 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

javascript
_.ceil(number, [precision = 0])
  • number: value to round up (coerced with _.toNumber).
  • precision: optional; decimal places when positive, magnitude when negative. Omitted or null behaves as 0.
  • Returns: a number (possibly NaN if the input does not coerce to a finite number before rounding).
1

Lodash docs baseline

The three official examples: integer ceil, two decimal places, and negative precision.

javascript
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
Try it Yourself
2

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.

javascript
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
Try it Yourself
3

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.651245.66. Values already on a two-decimal grid stay unchanged (245.67245.67).

javascript
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
Try it Yourself

📋 _.ceil vs Math.ceil

Feature_.ceil(n, p)Math.ceil(n)
Second argument (precision)YesNo (always integer result)
Input coercion_.toNumber firstStandard ToNumber via call
p === 0 on a finite numberSame as Math.ceil(_.toNumber(n))Native only
Negative numbersSame direction as native (toward +∞)Same
Non-zero precisionExponential shift + Math.ceilNot available

Pitfalls to avoid

Myth

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.

Money

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.

NaN

undefined and bad strings

_.ceil(undefined) is NaN. JSON.stringify(NaN) is null, which confuses logging—use Number.isNaN checks after coercion.

BigInt

Throws on BigInt

_.ceil(1n) throws before rounding because _.toNumber rejects BigInt.

❓ FAQ

No. With default precision (0), lodash sets number = _.toNumber(number) and returns Math.ceil(number). So _.ceil(-4.75) and Math.ceil(-4.75) are both -4—toward positive infinity, not toward zero.
It rounds up to a magnitude: _.ceil(6040, -2) becomes 6100 (next hundred). Positive precision keeps decimal places: _.ceil(6.004, 2) is 6.01.
Inputs run through _.toNumber first. Non-numeric strings like 'abc' become NaN, and Math.ceil(NaN) is NaN. null becomes 0; undefined becomes NaN.
No. _.toNumber throws 'Cannot convert a BigInt value to a number' before rounding. Use native BigInt division or convert explicitly if you accept lossy Number conversion.
Use import ceil from "lodash/ceil"; or require('lodash/ceil') so bundlers can tree-shake the rest of Lodash.

Summary

  • Purpose: round up with optional positive or negative precision, sharing one implementation with _.floor and _.round.
  • Remember: precision === 0 is native Math.ceil after _.toNumber; negatives are not a special lodash case.
  • Next: Lodash _.divide(), or the official Lodash docs for _.ceil (same createRound pattern as _.floor / _.round).
Did you know?

_.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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful