diff --git a/pint/delegates/formatter/_format_helpers.py b/pint/delegates/formatter/_format_helpers.py index 995159e65..501c72c7a 100644 --- a/pint/delegates/formatter/_format_helpers.py +++ b/pint/delegates/formatter/_format_helpers.py @@ -161,6 +161,7 @@ def formatter( power_fmt: str = "{} ** {}", parentheses_fmt: str = "({0})", exp_call: FORMATTER = "{:n}".format, + empty_numerator_fmt = "1", ) -> str: """Format a list of (name, exponent) pairs. @@ -183,6 +184,8 @@ def formatter( the format used for parenthesis. (Default value = "({0})") exp_call : callable (Default value = lambda x: f"{x:n}") + empty_numerator_fmt : str + the format used for an empty numerator. (Default value = "1") Returns ------- @@ -218,7 +221,7 @@ def formatter( return join_u(product_fmt, pos_terms + neg_terms) # Show as Ratio: positive terms / negative terms - pos_ret = join_u(product_fmt, pos_terms) or "1" + pos_ret = join_u(product_fmt, pos_terms) or empty_numerator_fmt if not neg_terms: return pos_ret diff --git a/pint/delegates/formatter/full.py b/pint/delegates/formatter/full.py index 92609b713..c3bf49ef4 100644 --- a/pint/delegates/formatter/full.py +++ b/pint/delegates/formatter/full.py @@ -131,10 +131,14 @@ def format_unit( ) -> str: uspec = uspec or self.default_format sort_func = sort_func or self.default_sort_func + empty_numerator_fmt = "1" if isinstance(unit._units, NonReducingUnitsContainer): sort_func = _sort_func + empty_numerator_fmt = "" return self.get_formatter(uspec).format_unit( - unit, uspec, sort_func=sort_func, **babel_kwds + unit, uspec, sort_func=sort_func, + empty_numerator_fmt = empty_numerator_fmt, +**babel_kwds ) def format_quantity( diff --git a/pint/delegates/formatter/plain.py b/pint/delegates/formatter/plain.py index d40ec1ae0..d03c00959 100644 --- a/pint/delegates/formatter/plain.py +++ b/pint/delegates/formatter/plain.py @@ -81,6 +81,7 @@ def format_unit( unit: PlainUnit | Iterable[tuple[str, Any]], uspec: str = "", sort_func: SortFunc | None = None, + empty_numerator_fmt = "1", **babel_kwds: Unpack[BabelKwds], ) -> str: """Format a unit (can be compound) into string @@ -203,6 +204,7 @@ def format_unit( unit: PlainUnit | Iterable[tuple[str, Any]], uspec: str = "", sort_func: SortFunc | None = None, + empty_numerator_fmt = "1", **babel_kwds: Unpack[BabelKwds], ) -> str: numerator, denominator = prepare_compount_unit( @@ -225,6 +227,7 @@ def format_unit( division_fmt=division_fmt, power_fmt="{}**{}", parentheses_fmt=r"({})", + empty_numerator_fmt=empty_numerator_fmt, ) def format_quantity( @@ -313,12 +316,16 @@ def format_unit( unit: PlainUnit | Iterable[tuple[str, Any]], uspec: str = "", sort_func: SortFunc | None = None, + empty_numerator_fmt="1", + **babel_kwds: Unpack[BabelKwds], + ) -> str: numerator, denominator = prepare_compount_unit( unit, uspec, sort_func=sort_func, + **babel_kwds, registry=self._registry, ) @@ -339,6 +346,8 @@ def format_unit( power_fmt="{}{}", parentheses_fmt="({})", exp_call=pretty_fmt_exponent, + empty_numerator_fmt=empty_numerator_fmt, + ) def format_quantity(