Is there a function in Python that allows me to round down to the nearest multiple of an integer?
round_down(19,10)=10 round_down(19,5)=15 round_down(10,10)=10
I conscientiously looked at SO and found nothing related to rounding down to a nearest base. Please keep this in mind before you post links to related questions or flag as duplicate.
You can use the MROUND function to round to the nearest multiple and the CEILING function to round up to a multiple. The MROUND function rounds a number to the nearest given multiple. The multiple to use for rounding is provided as the significance argument.
If you need to round a number to the nearest multiple of 5, you can use the MROUND function and supply 5 for number of digits. The value in B6 is 17 and the result is 15 since 15 is the nearest multiple of 5 to 17.
Whenever you want to round a number to a particular digit, look only at the digit immediately to its right. For example, if you want to round to the nearest tenth, look to the right of the tenths place: This would be the hundredths place digit. Then, if it is 5 or higher, you get to add one to the tenths digit.
def round_down(num, divisor): return num - (num%divisor) In [2]: round_down(19,10) Out[2]: 10 In [3]: round_down(19,5) Out[3]: 15 In [4]: round_down(10,10) Out[4]: 10
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