round, floor, ceil and trunc side by side
Look at the -2.5 row: Math.round sends halves towards positive infinity, so it gives -2, not -3. The last value clamps 140 into the range 0 to 100.
The built-in Math object gives you rounding (round, floor, ceil, trunc), min/max, powers, roots and random numbers. Rounding has rules that surprise people, toFixed returns a string, and money is safest stored as whole paise or cents.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 5 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write roundTo(x, places) that rounds a number to a given number of decimal places and returns a number (not a string), so roundTo(3.14159, 2) is 3.14.
Write toPaise(text) that turns a typed amount like "4.35" or "12" into whole paise (435, 1200).
Write percentOff(pricePaise, percent) that returns the discounted price in paise, rounded to the nearest paisa.
Explain it without notes
What is the difference between Math.floor and Math.trunc?
Why does (1.005).toFixed(2) give "1.00"?
How should an app store and add up prices?
How do you make a random integer between two values, and when is Math.random the wrong tool?
Preview
Console
Nothing logged yet.Expected console output
2.4 2 2 3 2
2.5 3 2 3 2
2.6 3 2 3 2
-2.4 -2 -3 -2 -2
-2.5 -2 -3 -2 -2
-2.6 -3 -3 -2 -2
9 9 NaN -Infinity
7.5 -1 1024 9 5
1 100