Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Round to Decimals

Numbers, Dates & Timersmedium

Course · Section 12: Numbers, Dates, Intl and Timers · Lecture 182: Math and Rounding

Implement `roundTo(n, decimals)` rounding `n` to the given number of decimal places.

Sample tests

Input: roundTo(3.14159, 2)
Output: 3.14
Input: roundTo(2.7, 0)
Output: 3

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Floating-point edge cases mean some halves round unexpectedly.
Approach & explanation (try first)

Multiplying by a power of ten, rounding, then dividing back rounds to the chosen precision.

Loading...
⌘/Ctrl + Enter

Run your code to see results.