User:Eleanorg/Thematic1.1/Manipulate Longitude 2 - percent: Difference between revisions
(Created page with "Converts any number within a given range into a percentage value, based on its position within the range. In this case, the number is a longitude value, which is converted to an ...") |
No edit summary |
||
Line 3: | Line 3: | ||
Heavily commented for your viewing pleasure. | Heavily commented for your viewing pleasure. | ||
< | <source lang=python> | ||
import re | import re | ||
### convert longitude into 5 digit integer | ### convert longitude into 5 digit integer |
Revision as of 15:45, 30 November 2011
Converts any number within a given range into a percentage value, based on its position within the range. In this case, the number is a longitude value, which is converted to an integer between 12528 and 41285.
Heavily commented for your viewing pleasure. <source lang=python>
import re
- convert longitude into 5 digit integer
longitude = str(001.2528) # must be a string for regex to work longint = int(re.sub("\.", "", longitude)) # removes decimal point and converts back to an integer
- convert integer into percentage of given range
- converts longitude into a percentage, where 1.2528 = 0% and 4.1285 = 100%
- range between numbers is 28703, so 1% of range is 28703/100 ---> 287.03. Longitude equivalent to 1% will be 12528 + (28703/100)
- thus to convert longitude to percentage, work out its distance from 12528 and divide this distance by 287.03.
percent = int((longint - 12528) / 287.03) </script>