Math round/floor/ceil/trunc Playground

round, floor, ceil and trunc on your own numbers: where does each take a negative number, or one at exactly .5? Also prices to cents and banker’s rounding. An…

Why it matters

Rounding is in prices, pages, progress bars, coordinates. Using floor where you meant trunc is a silent off-by-one for negative numbers, Math.round(-2.5) giving -2 is a frequent calculation bug, and Math.round(1.005 * 100) / 100 gives 1.

The key idea

round, floor, ceil have a fixed direction; trunc goes toward zero

Common mistakes

Try it in this playground