Lodash _.floor() method

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

What you’ll learn

  • How _.floor(number, [precision = 0]) maps to Math.floor plus optional decimal or magnitude rounding.
  • Why negatives still round toward −∞ (_.floor(-7.9) === -8), exactly like native Math.floor.
  • How the exponential-shift trick avoids most IEEE-754 noise when precision !== 0.
  • How _.toNumber coercion affects strings, null, and undefined.
  • 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 negatives Math.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

javascript
_.floor(number, [precision = 0])
  • number: value to round down (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 floor, two decimal places, and negative precision.

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

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.

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

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

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

📋 _.floor vs Math.floor

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

Pitfalls to avoid

Direction

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.

Money

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.

NaN

undefined and bad strings

_.floor(undefined) is NaN; _.floor('abc') is NaN. Validate before logging because JSON.stringify(NaN) serializes as null.

BigInt

Throws on BigInt

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

❓ FAQ

No. At precision 0 lodash sets number = _.toNumber(number) and returns Math.floor(number). So _.floor(-7.9) and Math.floor(-7.9) are both -8—toward negative infinity, not toward zero.
It rounds down to a magnitude. _.floor(4060, -2) becomes 4000 (previous hundred). Positive precision keeps decimal places: _.floor(0.046, 2) is 0.04.
Lodash routes through _.toNumber. null becomes 0, so _.floor(null) is 0. undefined becomes NaN, so _.floor(undefined) is NaN. Numeric strings parse normally (e.g. _.floor('3.7') is 3).
It can be, but be careful: _.floor(0.1 + 0.2, 2) is 0.3 only because the exponential-shift trick masks the IEEE-754 noise—not because lodash uses decimal math. For ledgers, work in integer minor units.
No. _.toNumber throws 'Cannot convert a BigInt value to a number' before any rounding happens. Use Math.floor on a Number copy if you can accept truncation.

Summary

  • Purpose: round down with optional positive or negative precision, sharing one implementation with _.ceil and _.round.
  • Remember: precision === 0 is native Math.floor after _.toNumber; negatives round further down, not toward zero.
  • Next: Lodash _.max(), _.ceil() for the upward twin, or the official Lodash docs for _.floor.
Did you know?

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

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