Skip to main content

2806. Account Balance After Rounded Purchase

link

class Solution:
def accountBalanceAfterPurchase(self, purchaseAmount: int) -> int:

num = round(float(purchaseAmount) / 10 + 0.001) * 10

return 100 - int(num)

Care about the round in python

In Python 3, the round() function is used to round a floating-point number to the nearest integer, with ties (e.g., 0.5) being rounded to the nearest even integer. This is also known as "round half to even" or "banker's rounding".

import math

def custom_round(number):
floor_value = math.floor(number)
if number - floor_value >= 0.5:
return math.ceil(number)
else:
return floor_value

result = custom_round(0.5)
print(result) # Output: 1