Last active
August 29, 2015 14:19
-
-
Save cjwinchester/47671cacdb340ae1a1e0 to your computer and use it in GitHub Desktop.
Python function to convert numbers to AP style. (Work in progress.)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # note: this doesn't actually work yet | |
| def numToAP(num): | |
| numstripped = int(str(num).replace('$','').replace(',','')) | |
| if numstripped < 10: | |
| apnums = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] | |
| return apnums[numstripped] | |
| elif numstripped > 999999: | |
| striplength = len(str(numstripped)) | |
| if striplength >= 7 and striplength <= 9: | |
| div = float(numstripped / 1000000) | |
| return str('{0:.2f}'.format(div) + ' million').replace('.00','') | |
| elif striplength >= 10 and striplength <= 12: | |
| div = float(numstripped / 1000000000) | |
| return str('{0:.2f}'.format(div) + ' billion').replace('.00','') | |
| elif numstripped > 999 and numstripped <= 999999: | |
| return str('{:,}'.format(numstripped)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment