keyvault-api / src /utils /formatters.py
hashir-qt's picture
Upload folder using huggingface_hub
e656014 verified
Raw History Blame Contribute Delete
1.11 kB
from datetime import datetime
def format_pkr(amount: int) -> str:
"""Format an integer amount as Pakistani Rupees."""
return f"Rs. {amount:,}"
def format_usd(amount: float) -> str:
"""Format a float amount as US Dollars."""
return f"${amount:,.2f}"
def format_price_display(amount: float, currency: str = "USD") -> str:
"""Format price for display with the given currency code."""
if currency == "PKR":
return format_pkr(int(amount))
if currency == "USD":
return format_usd(amount)
return f"{currency} {amount:,.2f}"
def format_date(dt: datetime) -> str:
"""Format a datetime to a human-readable string."""
return dt.strftime("%B %d, %Y at %I:%M %p")
def format_date_short(dt: datetime) -> str:
"""Format a datetime to a short date string."""
return dt.strftime("%b %d, %Y")
def truncate_text(text: str, max_length: int = 100) -> str:
"""Truncate text to max_length and append ellipsis if needed."""
if len(text) <= max_length:
return text
return text[: max_length - 3] + "..."