Verbally format a number in Python

前端 未结 3 735
春和景丽
春和景丽 2021-01-05 04:33

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 nin         


        
3条回答
  •  南笙
    南笙 (楼主)
    2021-01-05 05:20

    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)

提交回复
热议问题