aboutsummaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
Diffstat (limited to 'utils')
-rw-r--r--utils/__init__.py0
-rw-r--r--utils/formatters.py53
2 files changed, 53 insertions, 0 deletions
diff --git a/utils/__init__.py b/utils/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/utils/__init__.py
diff --git a/utils/formatters.py b/utils/formatters.py
new file mode 100644
index 0000000..c25fbb9
--- /dev/null
+++ b/utils/formatters.py
@@ -0,0 +1,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"