在 Python 中口头格式化一个数字 [英] Verbally format a number in Python
本文介绍了在 Python 中口头格式化一个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
pythonistas 如何将数字打印为单词,类似于 Common Lisp 代码:
[3]>(格式 t "~r" 1e25)九千亿、九百九十九千亿、九百九十九千亿、七百七十八亿亿、一千九千六亿、三千八亿、三亿六千一百万、二十万一万六千
解决方案
python 核心中没有,但是有 3rd 方库 num2words
<预><代码>>>>从 num2words 导入 num2words>>>num2words(1e25)'十个七十亿,七千三百万,七十四万一千,八百二十四'>>>num2words(10000000000000000000000000)'十节'(注意 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屋!
查看全文