from decimal import Decimal, InvalidOperation, ROUND_HALF_UP def format_money(value, currency="RUB"): """Format a monetary value for display without changing stored/calculated values.""" try: amount = Decimal(str(value if value is not None else 0)) amount = amount.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP) except (InvalidOperation, ValueError, TypeError): amount = Decimal("0.00") # Python's grouping is 1,234.56; convert it to Russian UI separators. formatted = f"{amount:,.2f}".replace(",", "\0").replace(".", ",").replace("\0", " ") currency = (currency or "RUB").strip() if currency.upper() == "RUB": return f"{formatted} ₽" return f"{formatted} {currency}"