JavaScript Number Rounding Challenge
This challenge focuses on mastering common rounding techniques in JavaScript. You'll implement a function that takes a number and a specified number of decimal places and returns the rounded number. Understanding how to precisely control decimal places is crucial for financial calculations, data display, and many other applications.
Problem Description
Your task is to create a JavaScript function named roundToDecimalPlaces that accepts two arguments:
number: The numerical value to be rounded.decimalPlaces: An integer representing the number of decimal places to round to.
The function should return the number rounded to the specified decimalPlaces. You need to handle standard rounding behavior where numbers with a fractional part of 0.5 or greater are rounded up, and those less than 0.5 are rounded down.
Key Requirements:
- The function must correctly round positive and negative numbers.
- The function must correctly handle cases where
decimalPlacesis 0. - The function should return a
numbertype.
Expected Behavior:
- If
numberis 123.456 anddecimalPlacesis 2, the output should be 123.46. - If
numberis 123.454 anddecimalPlacesis 2, the output should be 123.45. - If
numberis -123.456 anddecimalPlacesis 2, the output should be -123.46. - If
numberis 123.999 anddecimalPlacesis 0, the output should be 124. - If
numberis 123 anddecimalPlacesis 3, the output should be 123.000 (or equivalent representation of a number with trailing zeros if the language handles it that way intrinsically, but the value should be correct).
Edge Cases to Consider:
- Numbers that are exactly halfway between two values (e.g., 1.5, 2.5).
- Negative numbers.
- Zero
decimalPlaces. - Numbers with fewer decimal places than requested (e.g., rounding 10.5 to 3 decimal places).
Examples
Example 1:
Input: number = 123.45678, decimalPlaces = 3
Output: 123.457
Explanation: The number is rounded to 3 decimal places. The fourth decimal digit (8) is greater than or equal to 5, so the third decimal digit (6) is rounded up to 7.
Example 2:
Input: number = -5.6789, decimalPlaces = 2
Output: -5.68
Explanation: The number is rounded to 2 decimal places. The third decimal digit (8) is greater than or equal to 5, so the second decimal digit (7) is rounded up to 8.
Example 3:
Input: number = 99.995, decimalPlaces = 2
Output: 100.00
Explanation: When rounding to 2 decimal places, the third decimal digit (5) causes the preceding digit (9) to round up. This results in a carry-over, changing 99.99 to 100.00.
Example 4:
Input: number = 7.1, decimalPlaces = 0
Output: 7
Explanation: Rounding to 0 decimal places means rounding to the nearest whole number. The first decimal digit (1) is less than 5, so the number is rounded down.
Constraints
- The input
numberwill be a finite floating-point number. - The input
decimalPlaceswill be a non-negative integer. - The solution should aim for efficiency, but standard JavaScript rounding methods are acceptable.
Notes
JavaScript has built-in methods for rounding numbers, but sometimes you need more control over the number of decimal places. Consider how you can manipulate the number to utilize these built-in functions effectively. Think about how multiplying and dividing by powers of 10 can help shift the decimal point.