Hvor can I en python do the following rounding:
Round to the nearest 05 decimal
7,97 -> 7,95
6,72 -> 6,70
31,06 -> 31,05
36,04 -> 36,
An extension of the accepted answer.
def round_to(n, precision):
correction = precision if n >= 0 else -precision
return round(int(n/precision+correction)*precision, len(str(precision).split('.')[1]))
test_cases = [101.001, 101.002, 101.003, 101.004, 101.005, 101.006, 101.007, 101.008, 101.009]
[round_to(-x, 0.003) for x in test_cases]
[-101.001, -101.001, -101.001, -101.004, -101.004, -101.004, -101.007, -101.007, -101.007]