-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormatSpecifier.py
More file actions
71 lines (56 loc) · 2.1 KB
/
FormatSpecifier.py
File metadata and controls
71 lines (56 loc) · 2.1 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
62
63
64
65
66
67
68
69
70
71
#Format Specifier = {value:flags} format a value based on what flags are inserted
#flags are inserted
# used in the context of the f sting usually "print"
# .(number)f = round to that many decimal places (fixed point) decimal point precision
# .(number) = allocate that many spaces
# :03 = allocate and zero pad that many spaces
# :< = left justify
# :> = right justify
# :^ = center align
# :+ = use a plus sign to indicate positive value
# := = place sign to leftmost position
# : = insert space before positive numbers
# :, = comma separator
price1 = 3777.14159
price2 = -9877.65
price3 = 1277.37
# .(number)f = round to that many decimal places (fixed point) decimal point precision
#print(f"Price 1 is ${price1:.1f}")
#print(f"Price 2 is ${price2:.1f}")
#print(f"Price 3 is ${price3:.1f}")
# .(number) = allocate that many spaces (can insert 0 before the number for zero padding)
#print(f"Price 1 is ${price1:10}")
#print(f"Price 2 is ${price2:10}")
#print(f"Price 3 is ${price3:10}")
# :< = left justify
#print(f"Price 1 is ${price1:<10}")
#print(f"Price 2 is ${price2:<10}")
#print(f"Price 3 is ${price3:<10}")
# :> = right justify
#print(f"Price 1 is ${price1:>10}")
#print(f"Price 2 is ${price2:>10}")
#print(f"Price 3 is ${price3:>10}")
# :^ = center align
#print(f"Price 1 is ${price1:^10}")
#print(f"Price 2 is ${price2:^10}")
#print(f"Price 3 is ${price3:^10}")
# :+ = use a plus sign to indicate positive value
#print(f"Price 1 is ${price1:+}")
#print(f"Price 2 is ${price2:+}")
#print(f"Price 3 is ${price3:+}")
# := = place sign to leftmost position
#print(f"Price 1 is ${price1:=}")
#print(f"Price 2 is ${price2:=}")
#print(f"Price 3 is ${price3:=}")
# : = insert space before positive numbers
#print(f"Price 1 is ${price1: }")
#print(f"Price 2 is ${price2: }")
#print(f"Price 3 is ${price3: }")
# :, = comma separator
print(f"Price 1 is ${price1:,}")
print(f"Price 2 is ${price2:,}")
print(f"Price 3 is ${price3:,}")
#mixed or combined
print(f"Price 1 is ${price1:+,.2f}")
print(f"Price 2 is ${price2:+,.2f}")
print(f"Price 3 is ${price3:+,.2f}")