In python, there's a builtin function round(),
it rounds a number like this:
round(1900, -3) == 2000
is there a builtin function that can round a number downward, like this:
function(1900, -3) == 1000
You can use floor division:
def round_down(x, k=3):
n = 10**k
return x // n * n
res = round_down(1900) # 1000
math.floor
will also work, but with a drop in performance, see Python integer division operator vs math.floor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With