首页 > 解决方案 > 将大数转换为单词,然后将其四舍五入

问题描述

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

标签: pythonstringnumbers

解决方案


使用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

或者

使用humanize

import humanize
print(humanize.intword(s))

输出

154.3 million

推荐阅读