将大量数字转换为单词然后四舍五入

时间:2019-03-22 12:50:13

标签: python string numbers

我说我有很多。 154,342,231 如何使用python将其转换为:“ 154 million” 并且如果我有一个像这样的数字:6,213,341,987转换为“ 6.2 billion?”

2 个答案:

答案 0 :(得分:3)

使用num2words

import num2words as n2w

s = '154,342,231'
s = s.replace(",", "")
print(n2w.num2words(s))

输出

one hundred and fifty-four million, three hundred and forty-two thousand, two hundred and thirty-one point zero

OR

使用humanize

import humanize
print(humanize.intword(s))

输出

154.3 million

答案 1 :(得分:1)

有一个名为Humanize的Python库可以完成此工作。试试这个:

import humanize
a = '154,342,231'
a = int(a.replace(',',''))
humanize.intword(a)    # '154.3 million'