import math from .math_round_float import round_float from .text_format_decimal import format_decimal def format_percent(value: float, decimals: int) -> str: """A fraction as a percentage with fixed places; the product is cleaned of binary noise at 12 places first, so 0.145 is "15%", not "14%".""" if isinstance(value, bool) or not isinstance(value, (int, float)) or not math.isfinite(value): raise ValueError("value must be a finite number, received %r" % (value,)) percent = round_float(float(value) * 100, 12) return format_decimal(percent, decimals, False, "") + "%"