在Python中用数字格式化一个数字 [英] Verbally format a number in Python

查看:235
本文介绍了在Python中用数字格式化一个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python如何打印一个数字作为单词,就像Common Lisp代码的等价物一样:

  [3]> (格式t〜r1e25)
九次九,九九九,九九九,七百七十八万亿,十九万六千,三百和80亿,3110万,2116万美元, python核心中没有,但有第三方库 num2words

 >>> from num2words import num2words 
>>> num2words(1e25)
'十亿,十亿,七千三百七十四万一千八百二十四'

>>> num2words(10000000000000000000000000)
'ten septillion'

(注意1e25不会被转换为整数确切地说,在你的例子中)


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

解决方案

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)

这篇关于在Python中用数字格式化一个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆