8

pythonistas如何将数字打印为单词,就像Common Lisp代码的等价物:

[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
4

3 回答 3

8

在 python 核心中没有,但有 3rd 方库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'

(请注意,在您的示例中,1e25 并未精确地转换为整数)

于 2010-07-01T13:44:17.070 回答
1

我刚开始用土耳其语工作,这可能会有所帮助。

https://github.com/guneysus/humanizer-tr

它返回一个字符串列表,并带有简单randomizer() prettizer()的测试功能和核心功能humanizer()

它可以处理非常大的数字,因为它没有使用分割方法,而是使用字符串分割和操作。

您可以输入偶数或字符串。

由于我没有写数字验证,它甚至可以处理非数字文本:)

>>> humanizer('STACK OVER FLOW')
['STA Trilyon', 'CK  Milyar', 'OVE Milyon', 'R F Bin', 'LOW']
于 2013-12-11T21:30:16.783 回答
0

这是一种方法:

def abbreviate(x):
    abbreviations = ["", "K", "M", "B", "T", "Qd", "Qn", "Sx", "Sp", "O", "N", 
    "De", "Ud", "DD"]
    thing = "1"
    a = 0
    while len(thing) < len(str(x)) - 3:
        thing += "000"
        a += 1
    b = int(thing)
    thing = round(x / b, 2)
    return str(thing) + " " + abbreviations[a]

它这样做:

>>> abbreviate(11423)
11.43 K
于 2019-02-12T14:05:06.177 回答