-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIntegerToEnglishWords.py
More file actions
61 lines (57 loc) · 1.88 KB
/
IntegerToEnglishWords.py
File metadata and controls
61 lines (57 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 273. Integer to English Words
class Solution:
def __init__(self):
self.num1 = ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"]
self.num2 = ["Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen",
"Eighteen", "Nineteen"]
self.tens = ["Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]
def numberToWords(self, num):
"""
:type num: int
:rtype: str
"""
res = ""
if num == 0:
return "Zero"
if num >= 1000000000:
res += self.threeNumbers(num//1000000000) + " Billion"
num = num % 1000000000
if num >= 1000000:
if res: # 注意在前面右数字时加空格,否则就是开头,不需要空格
res += " "
res += self.threeNumbers(num // 1000000) + " Million"
num = num % 1000000
if num >= 1000:
if res:
res += " "
res += self.threeNumbers(num // 1000) + " Thousand"
num = num % 1000
if num >= 1:
if res:
res += " "
res += self.threeNumbers(num)
return res
# 输出最多三个数字的英文读法
def threeNumbers(self, num):
res = ""
hundred = num // 100
if hundred > 0:
res += self.num1[hundred-1] + " Hundred"
num = num % 100
decade = num // 10
if decade > 1:
if res:
res += " "
res += self.tens[decade-2]
elif decade == 1:
num = num % 10
if res:
res += " "
res += self.num2[num]
return res
num = num % 10
if num > 0:
if res:
res += " "
res += self.num1[num-1]
return res