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
Concept
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.
Foundation
📝 Syntax
Load the math module, then call the function:
styles.scss
@use "sass:math";
math.round($number)
Parameters
Parameter
Type
Required
Description
$number
Number
Yes
Any Sass number to round to the nearest whole value, with or without a unit (for example 4.2, 12.6px, 1.4rem).
Return value
Type
Situation
Result
Number
Already a whole number
Same value (unit kept)
Number
Closer to the lower integer
Rounds down (e.g. 4.2 → 4)
Number
Closer to the higher integer
Rounds up (e.g. 4.9 → 5)
Modules
📦 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.
Numbers
📏 How Units Work
math.round() rounds the numeric part and keeps the unit attached.
Input
Output
Notes
math.round(4.2px)
4px
Nearer to 4
math.round(4.9px)
5px
Nearer to 5
math.round(4px)
4px
Already whole
math.round(1.4rem)
1rem
Relative unit kept
math.round(33.6%)
34%
Percentage kept
Support
🛠 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 — preferred
Yes
LibSass
No built-in modules
Yes (legacy)
Ruby Sass
No built-in modules
Yes (legacy)
Cheat Sheet
⚡ Quick Reference
Goal
Code
Import math
@use "sass:math";
Nearest whole
math.round($n)
Closer to lower
math.round(4.2) → 4
Closer to higher
math.round(4.9) → 5
Legacy global call
round($n)
Debug while learning
@debug math.round(4.2px);
Compare
📋 round vs floor vs ceil
All three are bounding helpers in sass:math. Round chooses by distance; floor and ceil choose by direction.
Input
math.round
math.floor
math.ceil
4.2
4
4
5
4.9
5
4
5
4
4
4
4
-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.
Compare
📋 Sass math.round() vs CSS Rounding
Sass math.round()
CSS rounding functions
When it runs
Compile time (Dart Sass)
In the browser
Appears in CSS output?
No — only the result
Yes (where supported)
Needs modern browser support?
No
Yes
Best for
Fixed design tokens & SCSS math
Runtime / dynamic values
Hands-On
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.
4.6px is nearer to 5, so round and ceil agree here, while floor stays at 4px.
Applications
🚀 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.
Watch Out
⚠️ Common Pitfalls
Forgetting @use — math.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.
Pro Tips
💡 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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about math.round()
Compile-time nearest whole—units included.
5
Core concepts
📝01
Call
math.round($n)
API
📦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.
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.