Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounding down integers to nearest multiple

Tags:

python

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.

like image 503
The Unfun Cat Avatar asked Oct 26 '12 07:10

The Unfun Cat


People also ask

How do you round down to the nearest multiple?

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.

How do you round to the nearest multiple of 5?

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.

How do you round integers to the nearest ten?

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.


1 Answers

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 
like image 137
inspectorG4dget Avatar answered Sep 28 '22 04:09

inspectorG4dget