Created
October 18, 2024 02:06
-
-
Save Afeez1131/416536c54f6fad1944defb2692684cce to your computer and use it in GitHub Desktop.
round given number to the nearest whole number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
def custom_round(number): | |
# Multiply by 100 and check if the last two digits are >= 45 | |
decimal_part = (number * 100) % 100 | |
# If the decimal part is >= 45, round up | |
if decimal_part >= 45: | |
return math.ceil(number) | |
else: | |
return math.floor(number) | |
# Test cases | |
print(custom_round(13.4)) # Output: 13 | |
print(custom_round(13.5)) # Output: 14 | |
print(custom_round(14.5)) # Output: 15 | |
print(custom_round(14.45)) # Output: 15 | |
print(custom_round(24.45)) # Output: 25 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment