File size: 1,811 Bytes
c44a3f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cdb54bf
 
 
c44a3f3
 
 
 
 
 
 
 
 
 
 
1efaba3
 
 
 
 
 
c44a3f3
 
 
 
 
a593bad
 
 
 
 
 
 
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""Shared numeric formatting for leaderboard UI."""

from __future__ import annotations


def round2(value: float | int | None) -> float | None:
    if value is None or isinstance(value, bool):
        return None
    return round(float(value), 2)


def fmt_number(value: float | int | None, *, prefix: str = "", suffix: str = "") -> str:
    """Format a number with 2 decimals, but keep extra precision for small non-zero values."""
    if value is None or isinstance(value, bool):
        return "β€”"
    amount = float(value)
    if amount == 0:
        return f"{prefix}0.00{suffix}"
    absolute = abs(amount)
    if absolute >= 0.01:
        return f"{prefix}{amount:.2f}{suffix}"
    if absolute >= 1e-8:
        text = f"{amount:.8f}".rstrip("0").rstrip(".")
        return f"{prefix}{text}{suffix}"
    return f"{prefix}{amount:.2g}{suffix}"


def fmt_cost(value: float | None) -> str:
    if value is None:
        return "β€”"
    if value <= 0:
        from src.i18n import t

        return t("free")
    return fmt_number(value, prefix="$")


def fmt_pct_value(value: float | None) -> str:
    if value is None:
        return "β€”"
    if value <= 1:
        return fmt_number(value * 100, suffix="%")
    return fmt_number(value, suffix="%")


def fmt_steps(value: float | int | None) -> str:
    if value is None or isinstance(value, bool):
        return "β€”"
    return str(int(round(float(value))))


def fmt_duration(seconds: float | None) -> str:
    if seconds is None:
        return "β€”"
    if seconds < 60:
        return fmt_number(seconds, suffix="s")
    if seconds < 3600:
        return fmt_number(seconds / 60, suffix="m")
    hours = int(seconds // 3600)
    minutes = int((seconds % 3600) // 60)
    if minutes:
        return f"{hours}h {minutes}m"
    return f"{hours}h"