Nearest multiple of 10

This code implements a function called roundToNearest that rounds a given number (represented as a string) to the nearest multiple of 10. Here’s a summary and step-by-step explanation:

Summary:
The function takes a string representation of a numbe…


This content originally appeared on DEV Community and was authored by Aniket Vaishnav

img

This code implements a function called roundToNearest that rounds a given number (represented as a string) to the nearest multiple of 10. Here's a summary and step-by-step explanation:

Summary:
The function takes a string representation of a number and rounds it to the nearest multiple of 10. If the last digit is 5 or less, it rounds down; otherwise, it rounds up. The function handles cases where rounding up causes carry-over to higher digits.

Step-by-step explanation:

  1. Input validation:

    • Check if the input string is null or empty. If so, throw an IllegalArgumentException.
  2. Rounding down:

    • If the last digit is '5' or less, replace it with '0' and return the result.
  3. Rounding up:

    • If the last digit is greater than '5', the function proceeds to round up.
  4. Initialization:

    • Convert the input string to a character array.
    • Set the last digit to '0'.
    • Initialize a boolean toAdd to true, which indicates if we need to carry over to the next digit.
  5. Rounding process:

    • Iterate through the digits from right to left (excluding the last digit).
    • If toAdd is false, break the loop (no more carry-over needed).
    • If the current digit is '9':
      • Set it to '0'.
      • Keep toAdd as true (continue carry-over).
    • If the current digit is not '9':
      • Increment the digit.
      • Set toAdd to false (no more carry-over needed).
  6. Handling overflow:

    • After the loop, if toAdd is still true, it means we need to add a new leading digit '1'.
  7. Result formation:

    • Combine the prefix (if any) with the modified character array.
    • Return the result as a string.

The code handles various cases, including numbers that require multiple carry-overs (like 999 rounding to 1000) and ensures correct rounding for all input values.


This content originally appeared on DEV Community and was authored by Aniket Vaishnav


Print Share Comment Cite Upload Translate Updates
APA

Aniket Vaishnav | Sciencx (2024-10-19T20:27:58+00:00) Nearest multiple of 10. Retrieved from https://www.scien.cx/2024/10/19/nearest-multiple-of-10/

MLA
" » Nearest multiple of 10." Aniket Vaishnav | Sciencx - Saturday October 19, 2024, https://www.scien.cx/2024/10/19/nearest-multiple-of-10/
HARVARD
Aniket Vaishnav | Sciencx Saturday October 19, 2024 » Nearest multiple of 10., viewed ,<https://www.scien.cx/2024/10/19/nearest-multiple-of-10/>
VANCOUVER
Aniket Vaishnav | Sciencx - » Nearest multiple of 10. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/19/nearest-multiple-of-10/
CHICAGO
" » Nearest multiple of 10." Aniket Vaishnav | Sciencx - Accessed . https://www.scien.cx/2024/10/19/nearest-multiple-of-10/
IEEE
" » Nearest multiple of 10." Aniket Vaishnav | Sciencx [Online]. Available: https://www.scien.cx/2024/10/19/nearest-multiple-of-10/. [Accessed: ]
rf:citation
» Nearest multiple of 10 | Aniket Vaishnav | Sciencx | https://www.scien.cx/2024/10/19/nearest-multiple-of-10/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.