Thursday 15 July 2010

python - How can I round an integer to the nearest 20th? -



python - How can I round an integer to the nearest 20th? -

how can round integers this? i've looked round() function, doesn't have want.

5 > 0 9 > 0 11 > 20 14 > 20 19 > 20 20 > 40 29 > 40

define lambda:

>>> nearest_20 = lambda x: (1 + (x-1)/20)*20 if x % 20 >= 10 else (x/20)*20 >>> nearest_20(5) 0 >>> nearest_20(0) 0 >>> nearest_20(20) 20 >>> nearest_20(11) 20

python python-2.7

No comments:

Post a Comment