diff options
| author | Tyler <tyler@tylerhoang.xyz> | 2026-03-28 23:01:14 -0700 |
|---|---|---|
| committer | Tyler <tyler@tylerhoang.xyz> | 2026-03-28 23:01:14 -0700 |
| commit | 23675b39b8055a8568cdcf71f66482b9d0cf90a9 (patch) | |
| tree | 14e42cf710b47072e904b1c21d7322352ae1823c /utils/formatters.py | |
Initial commit — Prism financial analysis dashboard
Streamlit app with market bar, price chart, financial statements,
DCF valuation engine, comparable companies, and news feed.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'utils/formatters.py')
| -rw-r--r-- | utils/formatters.py | 53 |
1 files changed, 53 insertions, 0 deletions
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" |
