如何在python中将整数转换为等效的字符串?

时间:2014-08-26 18:17:53

标签: python

我想将100One Hundred或任何数字转换为其等效字符串。

我想避免写一个冗长的程序。最好的方法是什么?

3 个答案:

答案 0 :(得分:8)

您可以使用humanizehttps://pypi.python.org/pypi/humanize

等软件包
>>> import humanize
>>> humanize.intcomma(12345)
'12,345'
>>> humanize.intword(123455913)
'123.5 million'
>>> humanize.intword(12345591313)
'12.3 billion'
>>> humanize.apnumber(4)
'four'
>>> humanize.apnumber(41)
'41'

答案 1 :(得分:2)

您可以使用humanize

>>> import humanize
>>> humanize.intword(100)
'One Hundred'

您也可以尝试使用pynum2word

>>> import num2word
>>> num2word.to_card(15)
'fifteen'

答案 2 :(得分:0)

我终于在python中使用了inflect库。

我只是,

    import inflect

    i = inflect.engine()
    print i.number_to_words(number)

其余的答案更好,但这对我来说效果最好。