Skip to content

Instantly share code, notes, and snippets.

@cjwinchester
Last active August 29, 2015 14:19
Show Gist options
  • Save cjwinchester/47671cacdb340ae1a1e0 to your computer and use it in GitHub Desktop.
Save cjwinchester/47671cacdb340ae1a1e0 to your computer and use it in GitHub Desktop.
Python function to convert numbers to AP style. (Work in progress.)
# 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