Sass Numbers

Beginner
9 min read
Updated: Nov 2026
5 examples
Dart Sass

Definition and Usage

A Sass number has two parts: the numeric value and optional units (for example 16px). Numbers can also be unitless.

Real-world unit math.

Multiply and divide units the way physics does—px * px is square pixels; math.div(5px, 2s) is pixels per second.

Stay unit-clean.

Prefer $n * 1px over #{$n}px. Interpolation makes a string, not a number.

50% ≠ 0.5.

Percentages are a unit. Convert with math.div($pct, 100%) or $dec * 100%.

Syntax

SCSS
@use "sass:math";

100;          // unitless
16px;         // value + unit
5.2e3;        // scientific → 5200

4px * 6px;                 // complex units → calc(...)
math.div(5px, 2s);         // px / s
1in + 6px;                 // compatible → converts

math.div(50%, 100%);       // 0.5
0.5 * 100%;                // 50%
$size * 1px;               // attach a unit safely

Pattern: write a number (with units if needed) → do math with matching / compatible units → use math.div for division.

Common Patterns

Everyday number ideas:

PatternResult idea
16px / 100Number with units, or unitless
5.2e3Scientific notation (emits expanded form)
math.div($a, $b)Division (preferred over /)
1in + 6pxCompatible units convert automatically
1in + 1sIncompatible → compile error
$n * 1pxAttach a unit without making a string

SCSS Examples

Open View Compiled CSS for verified Dart Sass output (matches official Sass docs samples where shown).

Example 1 — Official Number Forms

Unitless, decimals, units, and scientific notation.

SCSS
.demo {
  --int: #{100};
  --dec: #{0.8};
  --px: #{16px};
  --sci: #{5.2e3};
  --sci2: #{6e-2};
}

How It Works

  1. 100 / 0.8 are unitless numbers.
  2. 16px keeps its unit.
  3. Scientific form expands in CSS.
  4. No int vs float split—like JavaScript.

Example 2 — Official Unit Math

Multiply/divide units; convert compatible lengths.

SCSS
@use "sass:math";

.demo {
  --sq: #{4px * 6px};
  --rate: #{math.div(5px, 2s)};
  --sum: #{1in + 6px};
}

How It Works

  1. px * px becomes complex units in calc().
  2. math.div builds a rate (px / s).
  3. 1in + 6px converts (CSS: 1in = 96px).
  4. 1in + 1s would error (incompatible).

Example 3 — Official Unit Cancellation

A seconds-per-pixel ratio times a distance cancels to time.

SCSS
@use "sass:math";

$transition-speed: math.div(1s, 50px);

@mixin move($left-start, $left-stop) {
  position: absolute;
  left: $left-start;
  transition: left ($left-stop - $left-start) * $transition-speed;

  &:hover {
    left: $left-stop;
  }
}

.slider {
  @include move(10px, 120px);
}

How It Works

  1. 1s / 50px is the speed ratio.
  2. 110px of travel × ratio → 2.2s.
  3. px cancels; seconds remain.
  4. Matches the official Sass docs sample.

Example 4 — Percentages vs Decimals

50% is not equal to 0.5—convert explicitly.

SCSS
@use "sass:math";

$pct: 50%;
$dec: 0.5;

.demo {
  --same: #{$pct == $dec};
  --to-dec: #{math.div($pct, 100%)};
  --to-pct: #{$dec * 100%};
  --via-fn: #{math.percentage($dec)};
}

How It Works

  1. 50% == 0.5 is false.
  2. math.div(..., 100%) yields a decimal.
  3. * 100% or math.percentage() yields a percent.
  4. Keep units meaningful for CSS.

Example 5 — Unit-Clean Attachment

Multiply by 1px instead of interpolating a fake number.

SCSS
@use "sass:math";

$size: 16;

.box {
  width: $size * 1px;
  height: $size * 1px;
  margin: math.div(24px, 2);
}

How It Works

  1. $size is unitless.
  2. * 1px creates a real length number.
  3. math.div keeps units on the result.
  4. Avoid #{$size}px (that is a string).

Edge Cases & Pitfalls

Most number bugs are stringified units or mixing incompatible units.

#{}

Do not build units with interpolation

#{$n}px is a string. Use $n * 1px or store the unit on the variable. See interpolation.

%

Percentages are not decimals

Convert explicitly with unit arithmetic or math.percentage().

/

Prefer math.div for division

Slash has other meanings in CSS. Details on math.div() and numeric operators.

units

Incompatible units error early

That is usually a real math bug—fix the units rather than stripping them.

Sass Support

Numbers and unit math are core Sass on Dart Sass.

FeatureStatus
Numbers with unitsSupported
Complex units as calc()Dart Sass (CSS emission; fuller support since 1.96)
~10 digit CSS precisionDart Sass default
sass:math helpersSupported

Compile with the Dart Sass CLI.

Key Takeaways

  • Value + units make a Sass number.
  • Unit math multiplies, divides, and cancels like the real world.
  • 50% ≠ 0.5—convert with unit arithmetic.
  • $n * 1px, never #{$n}px.

One line: keep numbers unit-clean so Sass can convert, cancel, and catch bad math for you.

Compile Notes

Practical checklist:

checklist.txt
1. Give quantities the units they mean
2. Use math.div for division
3. Convert % ↔ decimals explicitly
4. Attach units with $n * 1px (not #{$n}px)
5. Let incompatible-unit errors guide your math

Frequently Asked Questions

A Sass number has two parts: the numeric value and optional units. For example, in 16px the value is 16 and the unit is px. Numbers can also be unitless.
No. 50% is a number with a % unit and is not the same as 0.5. Convert with math.div($percentage, 100%) or $decimal * 100% (or math.percentage()).
Interpolation creates an unquoted string that looks like a number but cannot be used with number math or functions. Prefer $number * 1px or keep the unit on the variable.
Sass converts compatible units automatically (for example 1in + 6px). Combining incompatible units such as 1in + 1s throws a compile error.
Sass uses 64-bit floats internally and serializes with up to about 10 digits after the decimal for CSS output and equality checks.
No. Sass does not distinguish whole numbers from decimals. math.div(5, 2) returns 2.5—similar to JavaScript.

Next: Sass Strings

Learn quoted vs unquoted strings and when interpolation helps.

Strings →

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