Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verbally format a number in Python

How do pythonistas print a number as words, like the equivalent of the Common Lisp code:

[3]> (format t "~r" 1e25)
nine septillion, nine hundred and ninety-nine sextillion, nine hundred and ninety-nine quintillion, seven hundred and seventy-eight quadrillion, one hundred and ninety-six trillion, three hundred and eight billion, three hundred and sixty-one million, two hundred and sixteen thousand
like image 270
Wayne Werner Avatar asked Jul 01 '10 13:07

Wayne Werner


1 Answers

no in python core, but there is 3rd party library num2words

>>> from num2words import num2words
>>> num2words(1e25)
'ten septillion, one billion, seventy-three million, seven hundred and forty-one thousand, eight hundred and twenty-four'

>>> num2words(10000000000000000000000000)
'ten septillion'

(note that 1e25 is not converted to integer precisely, neither in your example)

like image 186
mykhal Avatar answered Sep 23 '22 22:09

mykhal