寻求帮助来更改数字输入并将其转换为简短的单词:
for instance 4,000,000,000 -> 4 Trillion,
ones = ["", "one ","two ","three ","four ", "five ", "six ","seven ","eight ","nine "]
tens = ["ten ","eleven ","twelve ","thirteen ", "fourteen ", "fifteen ","sixteen ","seventeen ","eighteen ","nineteen "]
twenties = ["","","twenty ","thirty ","forty ", "fifty ","sixty ","seventy ","eighty ","ninety "]
thousands = ["","thousand ","million ", "billion ", "trillion ", "quadrillion ", "quintillion ", "sextillion ", "septillion ","octillion ", "nonillion ", "decillion ", "undecillion ", "duodecillion ", "tredecillion ", "quattuordecillion ", "quindecillion", "sexdecillion ", "septendecillion ", "octodecillion ", "novemdecillion ", "vigintillion "]
def wordNumber(number):
number = int(number)
number = str(input("Enter the digits: "))
print(wordNumber(number))
我完全不知道下一步该去哪里了,我已经看过很多在线程序,这些程序用完整的数字(10)完成 - > 10。
答案 0 :(得分:1)
Humanize库会为您做到这一点:
>>> humanize.intword(12345591313)
'12.3 billion'
答案 1 :(得分:0)
这需要很多代码。你可以这样做: https://stackoverflow.com/a/19506803/3260314
或者只是使用模块。示例:num2words
>>> from num2words import num2words
>>> num2words(42)
forty-two
>>> num2words(42, ordinal=True)
forty-second
>>> num2words(42, lang='fr')
quarante-deux
答案 2 :(得分:0)
如果您需要手动执行此操作,以下是一种方法的部分示例。
def f(number):
ones = [
"",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
]
teens = {
10:"ten",
11:"eleven",
12:"twelve",
13:"thirteen",
14:"fourteen",
15:"fifteen",
16:"sixteen",
17:"seventeen",
18:"eighteen",
19:"nineteen",
}
tens = [
"",
"teen",
"twenty",
"thirty",
"fourty",
"fifty",
"sizty",
"seventy",
"eighty",
"ninety",
]
hundred = [ "%shundred" % one for one in ones ]
place_value = {1:ones,2:tens,3:hundred}
number_string = ""
number = list(str(number))
number.reverse()
number = [(place+1, int(digit)) for place, digit in enumerate(number)]
number.reverse()
number_iter = number.__iter__()
for place, digit in number_iter:
print place, digit
if place == 2 and digit == 1:
place, digit = number_iter.next()
number_string += teens[10 + digit]
else:
number_string += place_value[place][digit]
print number_string