Lodash _.floor() method
What you’ll learn
- How
_.floor(number, [precision = 0])maps toMath.floorplus optional decimal or magnitude rounding. - Why negatives still round toward
−∞(_.floor(-7.9) === -8), exactly like nativeMath.floor. - How the exponential-shift trick avoids most IEEE-754 noise when
precision !== 0. - How
_.toNumbercoercion affects strings,null, andundefined. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Comfort with Math.floor and how IEEE-754 represents decimals. See _.ceil() for the upward twin and _.toNumber() for the coercion pipeline.
Math.floor: rounds toward−∞; for negativesMath.floor(-7.9) === -8(not-7).- Optional precision: clamped with
Math.min(toInteger(precision), 292)inside_createRound.
Overview
_.floor is generated by createRound('floor'): coerce with _.toNumber, normalize precision (default 0, capped at 292), then either call Math.floor(number) directly or shift the value in exponential notation, apply Math.floor on the shifted pair, and shift back—identical to _.ceil and _.round except for the underlying Math method.
Round down
Largest integer ≤ value when precision === 0.
Decimal precision
Positive precision keeps that many fraction digits, always rounding downward.
Negative precision
Snap down to tens, hundreds, etc. (_.floor(4060, -2) === 4000).
Syntax
_.floor(number, [precision = 0]) - number: value to round down (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 floor, two decimal places, and negative precision.
import floor from "lodash/floor";
console.log(floor(4.006)); // 4
console.log(floor(0.046, 2)); // 0.04
console.log(floor(4060, -2)); // 4000 Negatives go further down
_.floor rounds toward −∞, so -7.9 becomes -8, not -7. With positive precision it still rounds downward at that decimal rank: -7.9 at precision 1 stays -7.9 because it’s already on the 0.1 grid.
import floor from "lodash/floor";
const n = -7.9;
console.log("Math.floor: " + Math.floor(n)); // -8
console.log("_.floor: " + floor(n)); // -8
console.log("_.floor(n, 1): " + floor(n, 1)); // -7.9 Coercion and a 2dp “floor to cent”
_.toNumber parses numeric strings; null coerces to 0, undefined to NaN. For currency-style floors to two decimals, _.floor(amount, 2) chops fractional cents off: 15.6789 → 15.67.
import floor from "lodash/floor";
console.log(floor("3.7")); // 3 (string coerced)
console.log(floor(null)); // 0
console.log(floor(undefined)); // NaN
console.log(floor(15.6789, 2)); // 15.67
console.log(floor(49.99)); // 49 📋 _.floor vs Math.floor
| Feature | _.floor(n, p) | Math.floor(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.floor(_.toNumber(n)) | Native only |
| Negative numbers | Same direction as native (toward −∞) | Same |
| Non-zero precision | Exponential shift + Math.floor | Not available |
Pitfalls to avoid
Floor is not truncation
_.floor(-7.9) is -8, not -7. If you want “cut off the fractional part toward zero”, use Math.trunc or _.toInteger.
Binary floats still apply
The exponential-shift trick masks many IEEE-754 artifacts but it isn’t decimal math. For ledgers, keep amounts as integer minor units (cents/paise) and only divide at the boundary.
undefined and bad strings
_.floor(undefined) is NaN; _.floor('abc') is NaN. Validate before logging because JSON.stringify(NaN) serializes as null.
Throws on BigInt
_.floor(1n) throws before rounding because _.toNumber rejects BigInt.
❓ FAQ
Summary
- Purpose: round down with optional positive or negative precision, sharing one implementation with
_.ceiland_.round. - Remember:
precision === 0is nativeMath.floorafter_.toNumber; negatives round further down, not toward zero. - Next: Lodash _.max(), _.ceil() for the upward twin, or the official Lodash docs for _.floor.
_.floor and _.ceil are the same factory—createRound('floor') vs createRound('ceil')—so their precision rules are identical and only the underlying Math[method] changes. With precision === 0, _.floor(n) reduces to Math.floor(_.toNumber(n)) after a single coercion hop.
6 people found this page helpful
