from flask import current_app from flask_login import current_user def format_currency(amount, symbol=None, show_sign=False): """Format a number as currency using the app's configured symbol.""" if amount is None: return '—' try: amount = float(amount) except (TypeError, ValueError): return '—' if symbol is None: try: symbol = current_user.currency_symbol if current_user.is_authenticated \ else current_app.config.get('APP_CURRENCY_SYMBOL', '$') except Exception: symbol = '$' formatted = f"{symbol}{abs(amount):,.2f}" if show_sign: if amount < 0: formatted = f"-{formatted}" elif amount > 0: formatted = f"+{formatted}" elif amount < 0: formatted = f"-{formatted}" return formatted def format_percent(value, decimals=1): if value is None: return '—' return f"{float(value):.{decimals}f}%" def format_large_number(value): """Abbreviate large numbers: 1,500,000 → 1.5M""" if value is None: return '—' value = float(value) if abs(value) >= 1_000_000: return f"{value / 1_000_000:.1f}M" if abs(value) >= 1_000: return f"{value / 1_000:.1f}K" return f"{value:.2f}"