Python输入阿拉伯数字,输出对应的英文完整代码
时间: 2023-05-24 16:05:23 浏览: 864
以下是Python代码实现,可以将阿拉伯数字转换为对应的英文完整代码:
```python
# 定义字典,将数字转换为英文代码
NUM_TO_EN = {
1: "one", 2: "two", 3: "three", 4: "four",
5: "five", 6: "six", 7: "seven", 8: "eight",
9: "nine", 10: "ten", 11: "eleven", 12: "twelve",
13: "thirteen", 14: "fourteen", 15: "fifteen",
16: "sixteen", 17: "seventeen", 18: "eighteen",
19: "nineteen", 20: "twenty", 30: "thirty",
40: "forty", 50: "fifty", 60: "sixty",
70: "seventy", 80: "eighty", 90: "ninety",
100: "hundred", 1000: "thousand", 1000000: "million",
1000000000: "billion", 1000000000000: "trillion"
}
def num_to_english(num):
if num in NUM_TO_EN:
return NUM_TO_EN[num]
if num < 100:
return NUM_TO_EN[num//10*10] + "-" + NUM_TO_EN[num%10]
for n, w in reversed(sorted(NUM_TO_EN.items())):
if num >= n:
current_count = num // n
thousands = '' if n < 1000 else num_to_english(current_count)
return thousands + " " + w + " " + num_to_english(num%n)
return ""
# 测试
print(num_to_english(123456789)) # one hundred twenty-three million four hundred fifty-six thousand seven hundred eighty-nine
```
阅读全文