def Num2MoneyFormat( change_number ):
"""
. Convert numbers to uppercase currency format ( format_word.__len__() - 3 + 2 decimal places )
change_number be in favor of float, int, long, string
"""
format_word = ["cents", "cents", "dollars",
"pick up", "hundred", "thousand", "ten thousand", "one thousand", "one thousand".
"picks", "hundreds", "thousands", "billions".
"pick up", "hundred", "thousand", "ten thousand", "one thousand", "one thousand".
"p", "h", "k", "m"]
format_num = ["zero", "one", "two", "two", "three", "four", "four", "five", "five", "six", "seven", "eight", "nine"]
if type( change_number ) == str:
# - If it's a string, try converting it to a float or int first.
if '.' in change_number:
try: change_number = float( change_number )
except: raise ValueError, '%s can\'t change'%change_number
else:
try: change_number = int( change_number )
except: raise ValueError, '%s can\'t change'%change_number
if type( change_number ) == float:
real_numbers = []
for i in range( len( format_word ) - 3, -3, -1 ):
if change_number >= 10 ** i or i < 1:
real_numbers.append( int( round( change_number/( 10**i ), 2)%10 ) )
elif isinstance( change_number, (int, long) ):
real_numbers = [ int( i ) for i in str( change_number ) + '00' ]
else:
raise ValueError, '%s can\'t change'%change_number
zflag = 0 # mark 0 consecutive times to remove the word million, or insert a zero when appropriate
start = len(real_numbers) - 3
change_words = []
for i in range(start, -3, -1): #so that i corresponds to the actual number of digits, and negative numbers to the number of minutes in the angle
if 0 <> real_numbers[start-i] or len(change_words) == 0:
if zflag:
change_words.append(format_num[0])
zflag = 0
change_words.append( format_num[ real_numbers[ start - i ] ] )
change_words.append(format_word[i+2])
elif 0 == i or (0 == i%4 and zflag < 3): #Control 10,000/$.
change_words.append(format_word[i+2])
zflag = 0
else:
zflag += 1
if change_words[-1] not in ( format_word[0], format_word[1]):
# - last two digits other than "angle, cent" are rounded to the nearest whole digit.
change_words.append("whole")
return ''.join(change_words)