from decimal import Decimal, ROUND_HALF_UP
[docs]
def classic_round(number: float, number_of_digits: int) -> float:
# first round 10 digits to avoid .4999999999999999 that should be .5 issues (can cause issues if we need precision higher than 10) # noqa: E501
if number_of_digits >= 10:
raise ValueError(f"number of digits should not exceed 9 (current value: {number_of_digits})")
return float(Decimal(str(number)).quantize(Decimal(f'1E-10'), ROUND_HALF_UP).quantize(Decimal(f'1E-{number_of_digits}'), ROUND_HALF_UP))