diff --git a/src/humanize/number.py b/src/humanize/number.py index 2fb22c6..201702e 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -501,7 +501,13 @@ def clamp( raise ValueError(msg) -def metric(value: float, unit: str = "", precision: int = 3) -> str: +def metric( + value: float, + unit: str = "", + precision: int = 3, + *, + ascii_only: bool = False, +) -> str: """Return a value with a metric SI unit-prefix appended. Examples: @@ -532,6 +538,7 @@ def metric(value: float, unit: str = "", precision: int = 3) -> str: value (int, float): Input number. unit (str): Optional base unit. precision (int): The number of digits the output should contain. + ascii_only (bool): Render the micro prefix as ``u`` instead of ``μ``. Returns: str: @@ -560,6 +567,8 @@ def metric(value: float, unit: str = "", precision: int = 3) -> str: ordinal_ = "mμnpfazyrq"[(-exponent - 1) // 3] else: ordinal_ = "" + if ascii_only and ordinal_ == "μ": + ordinal_ = "u" value_ = format(value, f".{digits}f") if not (unit or ordinal_) or unit in ("°", "′", "″"): space = ""