liveBook · Manning

Number rounding

As explained in Math for Web Design

A rounding strategy specifies how a numerical value is selected when rounding it to an interval.

A calculated column width, animated position, or displayed price can contain fractions that do not fit the visual result you want. Rounding selects a usable value: a whole pixel, a fixed decimal display, a whole-number percentage, or a chosen multiple such as 10px. The important choice is direction, especially for negative values. section 2.6 section 2.6.4

Table 2.6 JavaScript rounding methods

Function

Behavior

Examples

Results

Math.round(n)

Rounds n to the nearest whole number

Math.round(2.5)

Math.round(2.4)

Math.round(-2.6)

Math.round(-2.5)

Math.round(-2.4)

3

2

-3

-2

-2

Math.floor(n)

Rounds n down (toward -∞)

Math.floor(2.9)

Math.floor(-2.9)

2

-3

Math.ceil(n)

Rounds n up (toward +∞)

Math.ceil(2.1)

Math.ceil(-2.1)

3

-2

Math.trunc(n)

Removes the fractional part of n

Math.trunc(2.4)

2

Use the direction named by the method: floor goes toward −∞, ceil toward +∞, trunc removes the fraction, and round chooses the nearest integer. section 2.6.1

`Math.round()` is nearest-value rounding, but an exact `.5` tie goes toward +∞: `Math.round(2.5)` is `3`, while `Math.round(-2.5)` is `-2`. This differs from the common shortcut “always away from zero.” `Math.floor(-2.3)` becomes `-3`, whereas `Math.ceil(-2.3)` becomes `-2`. section 2.6.1 section 2.8

Choose a rounding result that matches the UI job:

  • Round calculated pixel widths when fractional values could create blurry edges. section 2.6.1 section 2.6.4
  • Round a scale-derived font size to a fixed number of decimal places with `toFixed(2)` and convert it back with `parseFloat()` when a number is needed. section 2.6.4
  • Format currency with two displayed decimals using `amount.toFixed(2)`, including whole amounts. section 2.6.4
  • Display progress as a whole percentage with `Math.round((current / total) * 100)`. section 2.6.4

Snap CSS values to a design interval

CSS `round(strategy, number, interval)` rounds directly in a declaration. With a 10px interval, `round(down, 26px, 10px)` produces the lower multiple, while `round(up, 26px, 10px)` produces the higher one. `nearest` is the default when omitted; `to-zero` moves a negative value toward zero. The same strategies correspond to JavaScript `Math.floor()`, `Math.round()`, `Math.trunc()`, and `Math.ceil()`. section 3.2.4

z-index: round(4.7);    #1
width: round(down, 26px, 10px);    #2
width: round(nearest, 26px, 10px);    #3
margin: round(to-zero, -26px, 10px);    #4
width: round(up, 26px, 10px);    #5
width: round(27px, 10px);    #6
opacity: round(0.78, 0.25);    #7

Compare whole-number, interval, negative-margin, and opacity rounding in CSS declarations. section 3.2.4

Rounding changes a value; it does not repair every floating-point comparison. Regular JavaScript numbers approximate many decimals, so values such as `0.1 + 0.2` may not equal the expected decimal exactly. When comparison is the problem, test whether the difference is smaller than `Number.EPSILON`; when presentation is the problem, round or format the result deliberately. section 2.3.1 section 2.3.2

How Math for Web Design covers it

How deep it goes
Explains whole-number directions, negative and half-value behavior, CSS strategies, intervals, and practical display uses.
Examples
Uses column widths, font scaling, currency strings, progress percentages, negative values, and CSS interval declarations.
What it assumes
Introduces the needed rounding behavior through JavaScript and CSS examples without requiring advanced mathematics.
What you can do after
Apply it via JavaScript Math methods, toFixed(), and CSS round() to produce deliberate UI values.
How it connects
Connects floating-point precision, Number.EPSILON, pixel layouts, responsive typography, currency formatting, and progress indicators.

Also explained in

Compare how 4 books explain number rounding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Math for Web Design. The section links point to the parts of the book it draws on.