python 3.x - Changing float with decimal point into words -


how change input float (for example 50300.45) words in form of voucher (fifty thousand 3 hundred , 45/100 dollars) in python?

to spell out money represented decimal string 2 digits after point: spell out integer part, spell out fraction:

#!/usr/bin/env python import inflect # $ pip install inflect  def int2words(n, p=inflect.engine()):     return ' '.join(p.number_to_words(n, wantlist=true, andword=' '))  def dollars2words(f):     d, dot, cents = f.partition('.')     return "{dollars}{cents} dollars".format(         dollars=int2words(int(d)),         cents=" , {}/100".format(cents) if cents , int(cents) else '')  dollars in ['50300.45', '100', '00.00']:     print(dollars2words(dollars)) 

output

fifty thousand 3 hundred , 45/100 dollars 1 hundred dollars 0 dollars 

here's inflect module helps convert integer english words. see how tell python convert integers words


Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -