-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise33.py
More file actions
50 lines (34 loc) · 1.14 KB
/
Copy pathExercise33.py
File metadata and controls
50 lines (34 loc) · 1.14 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
def commaFormat(number: int) -> str:
"""
Return a string of the number with comma formatting
:param number: Number in string
:type number: int
:raise TypeError: If number is not a int
:return: str
:rtype: str
"""
numberStr = str(number)
if "." in numberStr:
fractionalPart = numberStr[numberStr.index(".") :]
numberStr = numberStr[: numberStr.index(".")]
else:
fractionalPart = ""
triplet = ""
commaNumber = ""
for i in range(len(numberStr) - 1, -1, -1):
triplet = numberStr[i] + triplet
if len(triplet) == 3:
commaNumber = triplet + "," + commaNumber
triplet = ""
if triplet != "":
commaNumber = triplet + "," + commaNumber
return commaNumber[:-1] + fractionalPart
assert commaFormat(1) == "1"
assert commaFormat(10) == "10"
assert commaFormat(100) == "100"
assert commaFormat(1000) == "1,000"
assert commaFormat(10000) == "10,000"
assert commaFormat(100000) == "100,000"
assert commaFormat(1000000) == "1,000,000"
assert commaFormat(1234567890) == "1,234,567,890"
assert commaFormat(1000.123456) == "1,000.123456"