def _format_number_compactly(
    val: int | float,
    currency: str | None = None,
    as_integer: bool = False,
    fn: Callable[..., str] | None = None,
) -> str:
    """
    Format a single numeric value compactly, using a currency if provided.
    """

    from great_tables.vals import fmt_currency, fmt_integer, fmt_number, fmt_scientific

    if fn is not None and isinstance(fn, Callable):
        res = fn(val)

        # Check whether the result is a single string value; if not, raise an error
        if not isinstance(res, str):
            raise ValueError("The result of the formatting function must be a single string value.")

        return res

    if _is_na(val):
        return "NA"

    if val == 0:
        return "0"

    if abs(val) < 0.01:
        use_subunits = True
        decimals = None

        n_sigfig = 2
        compact = False

    elif abs(val) < 1:
        use_subunits = True
        decimals = None

        n_sigfig = 2
        compact = False

    elif abs(val) < 100:
        use_subunits = True
        decimals = None

        n_sigfig = 3
        compact = False

    elif abs(val) < 1000:
        use_subunits = True
        decimals = None

        n_sigfig = 3
        compact = False

    elif abs(val) < 10000:
        use_subunits = False
        decimals = 2

        n_sigfig = 3
        compact = True

    elif abs(val) < 100000:
        use_subunits = False
        decimals = 1

        n_sigfig = 3
        compact = True

    elif abs(val) < 1000000:
        use_subunits = False
        decimals = 0

        n_sigfig = 3
        compact = True

    elif abs(val) < 1e15:
        use_subunits = False
        decimals = 1

        n_sigfig = 3
        compact = True

    else:
        use_subunits = False
        decimals = None
        n_sigfig = 2
        compact = False

    # Format value accordingly

    if currency is not None:
        if abs(val) >= 1e15:
            val_formatted = fmt_currency(
                1e15,
                currency=currency,
                use_subunits=False,
                decimals=0,
                # compact=True,
            )

            val_formatted = f">{val_formatted}"

        else:
            val_formatted = fmt_currency(
                val,
                currency=currency,
                use_subunits=use_subunits,
                decimals=decimals,
                # compact=compact,
            )

    else:
        if abs(val) < 0.01 or abs(val) >= 1e15:
            val_formatted = fmt_scientific(
                val,
                exp_style="E",
                n_sigfig=n_sigfig,
                decimals=1,
            )

        else:
            if as_integer and val > -100 and val < 100:
                val_formatted = fmt_integer(val)

            else:
                val_formatted = fmt_number(
                    val,
                    n_sigfig=n_sigfig,
                    decimals=1,
                    compact=compact,
                )

    return val_formatted[0]

    Format a single numeric value compactly, using a currency if provided.
    
great_tables/_utils_nanoplots.py