feat: Add format_pct helper method

This commit is contained in:
Matthias
2025-11-23 08:13:48 +01:00
parent 2fa0503993
commit b92535deea
2 changed files with 16 additions and 0 deletions

View File

@@ -18,6 +18,7 @@ from freqtrade.util.formatters import (
fmt_coin, fmt_coin,
fmt_coin2, fmt_coin2,
format_duration, format_duration,
format_pct,
round_value, round_value,
) )
from freqtrade.util.ft_precise import FtPrecise from freqtrade.util.ft_precise import FtPrecise
@@ -44,6 +45,7 @@ __all__ = [
"format_date", "format_date",
"format_ms_time", "format_ms_time",
"format_ms_time_det", "format_ms_time_det",
"format_pct",
"get_dry_run_wallet", "get_dry_run_wallet",
"FtPrecise", "FtPrecise",
"PeriodicCache", "PeriodicCache",

View File

@@ -1,5 +1,7 @@
from datetime import timedelta from datetime import timedelta
from numpy import isnan
from freqtrade.constants import DECIMAL_PER_COIN_FALLBACK, DECIMALS_PER_COIN from freqtrade.constants import DECIMAL_PER_COIN_FALLBACK, DECIMALS_PER_COIN
@@ -80,3 +82,15 @@ def format_duration(td: timedelta) -> str:
h, r = divmod(td.seconds, 3600) h, r = divmod(td.seconds, 3600)
m, _ = divmod(r, 60) m, _ = divmod(r, 60)
return f"{d}d {h:02d}:{m:02d}" return f"{d}d {h:02d}:{m:02d}"
def format_pct(value: float | None) -> str:
"""
Format a float value as percentage string with 2 decimals
None and NaN values are formatted as "N/A"
:param value: Float value to format
:return: Formatted percentage string
"""
if value is None or isnan(value):
return "N/A"
return f"{value:.2%}"