I'm working with latitude/longitudes that end in either .25
or .75
. I want to prompt a user for a lat/lon and have the program round it to a lat/lon ending in .25 or .75. I cannot round to .00 or .5, ONLY .25 or .75.
For examaple, if a user enters 43.04, I need Python to round that to 43.25.
I'm using Enthought Canopy Python distribution and am new to Python. Any suggestions for this?
This can work as a custom rounding function:
def customRound(num, d = [0.0, 0.25, 0.75, 1.0]): # you can change this list to any e.g. [0.0, 0.1, 0.4, 0.9, 1.0]
dec = num%1
r = num - dec
round_dec = min([(abs(i - dec),i) for i in d])[1]
return r + round_dec
You can use it like this:
>>> customRound(9.34)
9.25
>>> customRound(9.4)
9.25
>>> customRound(9.98)
10.0
>>> customRound(9.8)
9.75
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments