forked from progrmoiz/python-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formats.py
75 lines (62 loc) · 2.01 KB
/
formats.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
"""
File: formats.py
Various specialized string display formatting utilities.
Test me with canned self-test or command-line argumnets.
To do: add parans for negative money, and more feature.
"""
def commas(N):
"""
Format positive integer-like N for display with
commas between digits grouping: "XXX,YYY,ZZZ"
"""
digits = str(N)
assert(digits.isdigit())
result = ''
while digits:
digits, last3 = digits[:-3], digits[-3:]
result = (last3 + ',' + result) if result else last3
return result
def commas1(N):
"""
The more easiest way to format digits
"""
return '{:,}'.format(N)
def money(N, numwidth=0, currency="$"):
"""
Format number N for display with commas, 2 decimal digits,
leading $ and sign, and optional padding: "$ -xxx,yyy.zz".
numwidth=0 for no space padding, currency='' to omit symbol,
and non-ASCII for others (e.g., pound=u'\xA3' or u'\u00A3').
"""
sign = '-' if N < 0 else ''
N = abs(N)
whole = commas(int(N))
fract = ('%2f' % N)[-2:]
number = '%s%s.%s' % (sign, whole, fract)
return '%s%*s' % (currency, numwidth, number)
if __name__ == '__main__':
def selftest():
tests = 0, 1 # fails -1, 1.23
tests += 12, 123, 1234, 12345, 123456, 1234567
tests += 2 ** 32, 2 ** 100
for test in tests:
print(commas(test))
print("-" * 80)
for test in tests:
print(commas1(test))
print("-" * 80)
tests = 0, 1, -1, 1.23, 1., 1.2, 3.1341
tests += 12, 123, 1234, 12345, 123456, 1234567
tests += 2 ** 32, (2 ** 32 + .2342)
tests += 1.2324, 1.2, 0.2345
tests += -1.242, -1.2, -15.2
tests += -(2 ** 32), -(2 ** 32 + 0.2324)
tests += 2 ** 100, -(2 ** 100)
for test in tests:
print('%s [%s]' % (money(test, 17), test))
import sys
if len(sys.argv) == 1:
selftest()
else:
print(money(float(sys.argv[1]), int(sys.argv[2])))