在 Python 中口头格式化数字

发布于 2024-09-08 11:30:16 字数 392 浏览 7 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

讽刺将军 2024-09-15 11:30:16

python 核心中没有,但有第 3 方库 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 并未精确转换为整数,在你的例子中都没有)

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)

心如荒岛 2024-09-15 11:30:16

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

并且

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

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

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

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

>>> humanizer('STACK OVER FLOW')
['STA Trilyon', 'CK  Milyar', 'OVE Milyon', 'R F Bin', 'LOW']

I just started a work in Turkish, it may be helpful.

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

It return a list of strings, and comes with easy testing functions randomizer() prettizer() and the core function humanizer()

It may handle very large numbers because it did not use dividing approach, but uses string segmentation and manipulation.

You can enter even number or string.

Since i did not write a number verification it can even handle a non number text
:)

>>> humanizer('STACK OVER FLOW')
['STA Trilyon', 'CK  Milyar', 'OVE Milyon', 'R F Bin', 'LOW']
淡墨 2024-09-15 11:30:16

这是一种实现方法:

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

Here as a way to do it:

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]

It does this:

>>> abbreviate(11423)
11.43 K
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文