Skip to content

Instantly share code, notes, and snippets.

@Afeez1131
Created October 18, 2024 02:06
Show Gist options
  • Save Afeez1131/416536c54f6fad1944defb2692684cce to your computer and use it in GitHub Desktop.
Save Afeez1131/416536c54f6fad1944defb2692684cce to your computer and use it in GitHub Desktop.
round given number to the nearest whole number
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