Sass math.round() Function

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
sass:math · Dart Sass

What You’ll Learn

math.round() is a bounding helper in the sass:math module. It rounds a number to the nearest whole number and keeps CSS units. You will learn module loading, how round differs from floor and ceil, compiler notes, and five compiled SCSS examples.

01

Concept

Nearest whole

02

Module

@use "sass:math"

03

Units

Preserved

04

Vs floor/ceil

Nearest, not directed

05

When

Compile time

06

Practice

5 examples

What Is math.round()?

Round means “go to the closest whole number.” Values closer to the lower integer go down; values closer to the higher integer go up. Whole numbers stay unchanged.

  • math.round(4)4
  • math.round(4.2)4
  • math.round(4.9)5

Use it for everyday snap-to-pixel sizing when you do not need the strict “never overshoot” rule of floor or the “never undershoot” rule of ceil.

💡
Beginner tip

Browsers never see math.round. Dart Sass evaluates it and writes the final whole number into your .css file.

📝 Syntax

Load the math module, then call the function:

styles.scss
@use "sass:math";

math.round($number)

Parameters

ParameterTypeRequiredDescription
$numberNumberYesAny Sass number to round to the nearest whole value, with or without a unit (for example 4.2, 12.6px, 1.4rem).

Return value

TypeSituationResult
NumberAlready a whole numberSame value (unit kept)
NumberCloser to the lower integerRounds down (e.g. 4.24)
NumberCloser to the higher integerRounds up (e.g. 4.95)

📦 Loading sass:math

styles.scss
// Recommended — namespaced
@use "sass:math";
$size: math.round(4.6);

// Optional — bring members into the current namespace
@use "sass:math" as *;
$size: round(4.6);

// Optional — custom namespace
@use "sass:math" as m;
$size: m.round(4.6);
⚠️
Put @use first

Keep @use "sass:math"; near the top of the file, before most other rules.

📏 How Units Work

math.round() rounds the numeric part and keeps the unit attached.

InputOutputNotes
math.round(4.2px)4pxNearer to 4
math.round(4.9px)5pxNearer to 5
math.round(4px)4pxAlready whole
math.round(1.4rem)1remRelative unit kept
math.round(33.6%)34%Percentage kept

🛠 Compatibility

This is about Sass compilers, not browsers. Compiled CSS is always plain numbers.

Implementation@use "sass:math"Global round()
Dart Sass (1.23+)Yes — preferredYes
LibSassNo built-in modulesYes (legacy)
Ruby SassNo built-in modulesYes (legacy)

⚡ Quick Reference

GoalCode
Import math@use "sass:math";
Nearest wholemath.round($n)
Closer to lowermath.round(4.2)4
Closer to highermath.round(4.9)5
Legacy global callround($n)
Debug while learning@debug math.round(4.2px);

📋 round vs floor vs ceil

All three are bounding helpers in sass:math. Round chooses by distance; floor and ceil choose by direction.

Inputmath.roundmath.floormath.ceil
4.2445
4.9545
4444
-4.2-4-5-4

Choose round for normal nearest-integer snap. Choose floor when you must never overshoot. Choose ceil when you must never undershoot.

📋 Sass math.round() vs CSS Rounding

Sass math.round()CSS rounding functions
When it runsCompile time (Dart Sass)In the browser
Appears in CSS output?No — only the resultYes (where supported)
Needs modern browser support?NoYes
Best forFixed design tokens & SCSS mathRuntime / dynamic values

Examples Gallery

Each example uses @use "sass:math". Open View Compiled CSS to see what Dart Sass emits.

📚 Getting Started

Official-style round checks and unit preservation.

Example 1 — Basic math.round()

Whole numbers stay put; fractions go to the nearest integer.

styles.scss
@use "sass:math";

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

.panel {
  width: math.round(119.6px);
}

How It Works

119.6px is nearer to 120 than to 119, so the compiled width is 120px.

Example 2 — Units Stay Attached

Round does not strip rem or other units.

styles.scss
@use "sass:math";

$gap: 1.4rem;
$pad: 1.6rem;

.card {
  margin-block: math.round($gap);
  padding-block: math.round($pad);
}

How It Works

1.4rem rounds to 1rem; 1.6rem rounds to 2rem.

📈 Practical Patterns

Pixel snap, percentages, and side-by-side bounds.

Example 3 — Snap a Computed Length to Pixels

After division or scaling, round so CSS gets a clean whole pixel.

styles.scss
@use "sass:math";

$base: 17px;
$scale: 1.35;

.icon {
  width: math.round($base * $scale);
  height: math.round($base * $scale);
}

How It Works

17 * 1.35 = 22.95, which rounds to 23px for both axes.

Example 4 — Round a Percentage Share

Snap fractional percentages to whole percents for cleaner output.

styles.scss
@use "sass:math";

$share: 33.333%;

.col {
  width: math.round($share);
}

How It Works

33.333% is nearer to 33% than to 34%, so the compiled width is 33%.

Example 5 — Round Beside Floor and Ceil

See how the three helpers treat the same fractional width.

styles.scss
@use "sass:math";

$raw: 4.6px;

.round-demo {
  width: math.round($raw);
}

.floor-demo {
  width: math.floor($raw);
}

.ceil-demo {
  width: math.ceil($raw);
}

How It Works

4.6px is nearer to 5, so round and ceil agree here, while floor stays at 4px.

🚀 Real-World Use Cases

  • Pixel snapping — clean fractional lengths after scale or division.
  • Design tokens — round computed spacing or type sizes to whole units.
  • Percent shares — simplify long fractional percentages in output CSS.
  • Icon / avatar sizes — keep width and height on matching whole pixels.
  • Default bound — reach for round when floor/ceil strictness is not required.
  • Paired teaching — compare with floor and ceil to show nearest vs directed rounding.

🧠 How Compilation Works

1

Write SCSS

Add @use "sass:math"; and call math.round($n).

Source
2

Dart Sass runs

The compiler loads the math module and evaluates the call.

Compile
3

Pick the nearest whole

The value snaps to the closest integer; the unit stays attached.

Round
4

Plain CSS ships

The browser only receives the final whole number or length.

⚠️ Common Pitfalls

  • Forgetting @usemath.round is undefined until you load sass:math.
  • Using round when floor/ceil is required — nearest can still overshoot or undershoot relative to a hard budget.
  • Expecting runtime behavior — changing a CSS variable later will not re-run Sass math.round.
  • Assuming CSS round() appears in output — Sass writes the resolved number, not a CSS function call.
  • LibSass pipelines — built-in modules need Dart Sass; migrate or use global round() carefully.

💡 Best Practices

✅ Do

  • Use @use "sass:math" and math.round() in new SCSS
  • Use round for everyday nearest-integer snap
  • Compare with floor / ceil when the design has a hard bound
  • Check results with @debug while learning
  • Prefer Dart Sass for built-in modules

❌ Don’t

  • Expect the browser to evaluate math.round
  • Use round to enforce “never overflow” (use floor) or “never short” (use ceil)
  • Round every value blindly if sub-pixel precision matters in the design
  • Rely on LibSass for @use "sass:math"
  • Confuse Sass round with CSS round() left in the stylesheet

Key Takeaways

Knowledge Unlocked

Five things to remember about math.round()

Compile-time nearest whole—units included.

5
Core concepts
📦 02

Module

sass:math

@use
⚖️ 03

Rule

nearest

Round
📏 04

Units

preserved

CSS
05

When

compile time

Sass

❓ Frequently Asked Questions

math.round($number) rounds a number to the nearest whole number. For example math.round(4.2) is 4 and math.round(4.9) is 5. Whole numbers stay the same. 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.9px) compiles to 5px.
round goes to the nearest whole number. floor always rounds down (toward -infinity). ceil always rounds up (toward +infinity). Use round for typical snap-to-integer sizing.
Yes. The global round($number) name still works for older stylesheets. New projects should prefer math.round() from sass:math.
Yes. math.round(12.4px) is 12px. Only the numeric part is rounded; the unit stays attached.
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.
Did you know?

Official Sass docs list math.round under Bounding Functions with ceil, floor, clamp, min, and max. Round is the default “closest integer” tool; floor and ceil are the directional specialists.

Conclusion

math.round() snaps a number to the nearest whole value at compile time and keeps units. Use it for everyday pixel and percent cleanup—and switch to floor or ceil when your design needs a one-way bound.

Continue with math.floor(), math.ceil(), or the Sass introduction.

More Sass Math

Compare with directed rounding in the math module.

math.ceil() →

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.

5 people found this page helpful