Sass math.round()

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

Definition and Usage

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

Official samples.

math.round(4) → 4 · math.round(4.2) → 4 · math.round(4.9) → 5.

Compile-time rounding.

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 round() still works but is deprecated in modern Dart Sass. New stylesheets should use math.round after @use "sass:math".

Syntax

SCSS
@use "sass:math";

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

math.round(4);    // 4
math.round(4.2);  // 4
math.round(4.9);  // 5
math.round(4.5px); // 5px

// Global name (legacy / deprecated in modern Dart Sass)
round(4.9); // 5

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

Pattern: compute a fractional length or count → math.round for the nearest whole value → use math.floor or math.ceil when the rounding direction must be forced.

Units and Halves

Nearest whole number, with units preserved:

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

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

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

Nearest whole number for common fractions.

SCSS
@use "sass:math";

@debug math.round(4);   // 4
@debug math.round(4.2); // 4
@debug math.round(4.9); // 5

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

How It Works

  1. 4 is already whole → stays 4.
  2. 4.2 is nearer to 4.
  3. 4.9 is nearer to 5.
  4. Matches the official Sass docs samples.

Example 2 — Units Are Preserved

Rounding changes the numeric part only.

SCSS
@use "sass:math";

.units {
  --px: #{math.round(4.5px)};
  --neg: #{math.round(-4.2)};
  --neg2: #{math.round(-4.6)};
}

How It Works

  1. 4.5px rounds to 5px.
  2. -4.2 is nearer to -4.
  3. -4.6 is nearer to -5.
  4. Units stay attached when present.

Example 3 — Halfway Values

.5 cases round away from the lower integer for positive numbers.

SCSS
@use "sass:math";

.half {
  --h: #{math.round(4.5)};
}

How It Works

  1. 4.5 is exactly halfway.
  2. Dart Sass rounds it to 5.
  3. Verify edge cases if your design depends on banker's rounding.
  4. Use floor/ceil when the direction must be forced.

Example 4 — round vs floor vs ceil

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 round when closest is good enough.

Example 5 — Emit a Whole CSS Length

Snap a fractional width token to the nearest pixel.

SCSS
@use "sass:math";

$w: 12.4px;

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

How It Works

  1. Store the fractional token in a variable.
  2. math.round snaps to the nearest whole px.
  3. CSS sees 12px with no fraction left.
  4. Useful for icon slots and grid gutters.

Edge Cases & Pitfalls

Most round bugs are needing a forced direction (floor/ceil), or using the deprecated global name.

dir

Nearest is not always enough

Need never-overshoot? Use math.floor. Need never-undershoot? Use math.ceil.

global

Global round() is deprecated

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

stage

Compile time, not runtime

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

units

Units stay attached

math.round(4.5px) is 5px, not a unitless 5.

Sass Support

Prefer the module form with Dart Sass 1.23+.

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

Compile with the Dart Sass CLI.

Key Takeaways

  • Rounds to the nearest whole number.
  • Keeps units such as px on the result.
  • Different from floor/ceil — pick round when closest is fine.
  • Prefer math.round over deprecated global round().

One line: when a fractional Sass number should snap to the closest whole value, call math.round($number).

Compile Notes

Practical checklist:

checklist.txt
1. @use "sass:math"
2. Pass a number (units optional)
3. Expect the nearest whole value
4. Prefer math.round over global round()
5. Use floor/ceil when the direction must be forced

Frequently Asked Questions

math.round($number) rounds a number to the nearest whole number. Whole numbers stay the same. For example math.round(4.2) is 4 and math.round(4.9) is 5. Units such as px are preserved.
Add @use "sass:math"; at the top of your SCSS file, then call math.round($number). Example: math.round(4.5px) compiles to 5px.
round goes to the nearest whole number. floor always rounds down (toward -infinity). ceil always rounds up (toward +infinity). Pick round when either direction is fine as long as you land on the closest integer.
Yes. It still picks the nearest whole number. math.round(-4.2) is -4; math.round(-4.6) is -5.
Yes. The global round($number) name still works for older stylesheets, but Dart Sass deprecates global built-ins. New projects should prefer math.round() from sass:math.
Same idea, different stage. Sass math.round() 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.sin()

Return the sine of an angle in Sass.

math.sin() →

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