Sass math.floor()

Beginner
8 min read
Updated: Nov 2026
5 examples
Dart Sass 1.23+

Definition and Usage

math.floor($number) rounds $number down to the next lowest whole number (toward -infinity). Whole numbers stay the same. Units such as px are kept. Load it with @use "sass:math" (Dart Sass 1.23+ for modules; global floor() also exists).

Official samples.

math.floor(4) → 4 · math.floor(4.2) → 4 · math.floor(4.9) → 4.

Compile-time floor.

Sass resolves the value when you compile. The CSS file only contains the final whole number—unlike browser CSS rounding functions.

Prefer the module form.

Global floor() still works but is deprecated in modern Dart Sass. New stylesheets should use math.floor after @use "sass:math".

Syntax

SCSS
@use "sass:math";

math.floor($number); //=> number (units preserved)

math.floor(4);    // 4
math.floor(4.2);  // 4
math.floor(4.9);  // 4
math.floor(4.9px); // 4px

// Global name (legacy / deprecated in modern Dart Sass)
floor(4.9); // 4

// Module API — prefer math.floor after @use "sass:math"

Pattern: compute a fractional length or count → math.floor so you never overshoot → compare with math.ceil and math.round when the rounding direction differs.

Units and Signs

Floor always moves toward -infinity:

InputResult
math.floor(4)4
math.floor(4.2)4
math.floor(4.9)4
math.floor(4.9px)4px
math.floor(-4.2)-5
math.floor(-4.2px)-5px
floor vs ceil vs round.

For 4.2: floor → 4, ceil → 5, round → 4. For -4.2: floor → -5, ceil → -4.

SCSS Examples

Examples target Dart Sass with @use "sass:math". Open View Compiled CSS for verified output (matches official Sass docs samples).

Example 1 — Official Docs Samples

Whole numbers stay put; fractions move down.

SCSS
@use "sass:math";

@debug math.floor(4);   // 4
@debug math.floor(4.2); // 4
@debug math.floor(4.9); // 4

.basic {
  --a: #{math.floor(4)};
  --b: #{math.floor(4.2)};
  --c: #{math.floor(4.9)};
}

How It Works

  1. 4 is already whole → stays 4.
  2. 4.2 moves down to 4.
  3. 4.9 also moves down to 4.
  4. Matches the official Sass docs samples.

Example 2 — Units Are Preserved

Floor changes the numeric part only.

SCSS
@use "sass:math";

.units {
  --px: #{math.floor(4.9px)};
}

How It Works

  1. 4.9px is a length with a fraction.
  2. Floor yields 4 with the same unit.
  3. Output is 4px — ready for CSS properties.
  4. Works the same for rem, %, and other units.

Example 3 — Negative Numbers

Toward -infinity means -4.2 becomes -5, not -4.

SCSS
@use "sass:math";

.neg {
  --n: #{math.floor(-4.2)};
  --px: #{math.floor(-4.2px)};
}

How It Works

  1. Floor always moves toward -infinity.
  2. -4.2 steps to -5.
  3. Units still stick — -4.2px → -5px.
  4. Not the same as truncating toward zero.

Example 4 — floor vs ceil vs round

Same input, three different whole numbers.

SCSS
@use "sass:math";

.compare {
  --ceil: #{math.ceil(4.2)};
  --floor: #{math.floor(4.2)};
  --round: #{math.round(4.2)};
}

How It Works

  1. ceil never undershoots → 5.
  2. floor never overshoots → 4.
  3. round picks nearest → 4 for 4.2.
  4. Choose floor for counts you must not over-size.

Example 5 — Emit a Whole CSS Length

Guarantee a pixel-snapped width that never exceeds a fractional token.

SCSS
@use "sass:math";

$w: 12.9px;

.box {
  width: math.floor($w);
}

How It Works

  1. Store the fractional token in a variable.
  2. math.floor drops it to the previous whole px.
  3. CSS sees 12px with no fraction left.
  4. Useful for fit-to-container counts and max tracks.

Edge Cases & Pitfalls

Most floor bugs are mixing it up with ceil on negatives, or using the deprecated global name.

sign

Negatives go toward -infinity

math.floor(-4.2) is -5. Use math.ceil if you need -4.

global

Global floor() is deprecated

Prefer math.floor after @use "sass:math" to avoid Dart Sass 3.0 removals.

stage

Compile time, not runtime

Sass cannot floor a live CSS custom property. Use CSS math if the value must stay dynamic.

units

Units stay attached

math.floor(4.9px) is 4px, not a unitless 4.

Sass Support

Prefer the module form with Dart Sass 1.23+.

APIStatus
math.floor()Dart Sass 1.23+ via @use "sass:math"
Global floor()Works; deprecated in modern Dart Sass
UnitsPreserved on the result
LibSass / Ruby SassUse global floor() if modules are unavailable

Compile with the Dart Sass CLI.

Key Takeaways

  • Rounds down to the next whole number (toward -infinity).
  • Keeps units such as px on the result.
  • Different from ceil/round — pick floor when overshoot is forbidden.
  • Prefer math.floor over deprecated global floor().

One line: when a fractional Sass number must never overshoot, call math.floor($number).

Compile Notes

Practical checklist:

checklist.txt
1. @use "sass:math"
2. Pass a number (units optional)
3. Expect the next whole value toward -infinity
4. Prefer math.floor over global floor()
5. Use ceil/round when the direction differs

Frequently Asked Questions

math.floor($number) rounds a number down to the next lowest whole number. Whole numbers stay the same. For example math.floor(4.9) is 4 and math.floor(4) is 4. Units such as px are preserved.
Add @use "sass:math"; at the top of your SCSS file, then call math.floor($number). Example: math.floor(4.9px) compiles to 4px.
floor always rounds down (toward -infinity). ceil always rounds up (toward +infinity). round goes to the nearest whole number. Pick floor when you must never overshoot—for example max columns that still fit.
Yes. Floor still means toward negative infinity. math.floor(-4.2) is -5, not -4. That is different from truncating toward zero.
Yes. The global floor($number) name still works for older stylesheets, but Dart Sass deprecates global built-ins. New projects should prefer math.floor() from sass:math.
Same idea, different stage. Sass math.floor() runs when you compile SCSS. CSS rounding functions run in the browser. Use Sass when the value must be fixed in the output CSS.

Next: Sass math.hypot()

Return the length of an n-dimensional vector from its components.

math.hypot() →

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.

8 people found this page helpful