blob: c25fbb98526ac035e16c3957a2d280159add6d21 (
plain)
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
|
def fmt_large(value) -> str:
"""Format a number in billions/millions with suffix."""
try:
v = float(value)
except (TypeError, ValueError):
return "N/A"
if abs(v) >= 1e12:
return f"${v / 1e12:.2f}T"
if abs(v) >= 1e9:
return f"${v / 1e9:.2f}B"
if abs(v) >= 1e6:
return f"${v / 1e6:.2f}M"
return f"${v:,.0f}"
def fmt_currency(value) -> str:
"""Format a dollar amount with commas."""
try:
return f"${float(value):,.2f}"
except (TypeError, ValueError):
return "N/A"
def fmt_pct(value, decimals: int = 2) -> str:
"""Format a ratio as a percentage (0.12 → 12.00%)."""
try:
return f"{float(value) * 100:.{decimals}f}%"
except (TypeError, ValueError):
return "N/A"
def fmt_ratio(value, decimals: int = 2) -> str:
"""Format a plain ratio/multiple (e.g. P/E)."""
try:
return f"{float(value):.{decimals}f}x"
except (TypeError, ValueError):
return "N/A"
def fmt_number(value, decimals: int = 2) -> str:
"""Format a plain number."""
try:
return f"{float(value):,.{decimals}f}"
except (TypeError, ValueError):
return "N/A"
def delta_color(value) -> str:
"""Return 'normal', 'inverse' or 'off' for st.metric delta_color."""
try:
return "normal" if float(value) >= 0 else "inverse"
except (TypeError, ValueError):
return "off"
|