File size: 754 Bytes
f7323a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""Data formatting utilities for the financial dashboard."""

import pandas as pd


def format_financial_value(value) -> str:
    """Format financial values with appropriate units."""
    if pd.isna(value):
        return "N/A"
    if abs(value) >= 1e9:
        return f"${value/1e9:.2f}B"
    elif abs(value) >= 1e6:
        return f"${value/1e6:.2f}M"
    else:
        return f"${value:.2f}"


def format_percentage(value: float, decimals: int = 2) -> str:
    """Format percentage values."""
    if pd.isna(value):
        return "N/A"
    return f"{value:.{decimals}f}%"


def format_currency(value: float, decimals: int = 2) -> str:
    """Format currency values."""
    if pd.isna(value):
        return "N/A"
    return f"${value:,.{decimals}f}"