Skip to content

Commit

Permalink
Merge pull request #132 from e10v/dev
Browse files Browse the repository at this point in the history
Set upper boundary for fixed point number formatting…
  • Loading branch information
e10v authored Jan 10, 2025
2 parents 39f7988 + 205b7ca commit 9897cc5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/tea_tasting/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def format_num(
pct: bool = False,
nan: str = "-",
inf: str = "∞",
fixed_point_limit: float = 0.001,
fixed_point_range: tuple[float | None, float | None] = (0.001, 10_000_000),
thousands_sep: str | None = None,
decimal_point: str | None = None,
) -> str:
Expand All @@ -138,7 +138,10 @@ def format_num(
pct: If `True`, format as a percentage.
nan: Replacement for `None` and `nan` values.
inf: Replacement for infinite values.
fixed_point_limit: Limit, below which number is formatted as exponential.
fixed_point_range: The range within which the number is formatted
as fixed point.
Number outside of the range is formatted as exponential.
`None` means no boundary.
thousands_sep: Thousands separator. If `None`, the value from locales is used.
decimal_point: Decimal point symbol. If `None`, the value from locales is used.
Expand All @@ -154,7 +157,10 @@ def format_num(
if pct:
val = val * 100

if abs(val) < fixed_point_limit:
if (
(fixed_point_range[0] is not None and abs(val) < fixed_point_range[0]) or
(fixed_point_range[1] is not None and abs(val) >= fixed_point_range[1])
):
precision = max(0, sig - 1)
typ = "e" if val != 0 else "f"
else:
Expand Down Expand Up @@ -191,7 +197,8 @@ def get_and_format_num(data: dict[str, Any], key: str) -> str:
a percentage value. Round percentage values to 2 significant digits,
multiply by `100` and add `"%"`.
- Round other values to 3 significant values.
- If value is less than `0.001`, format it in exponential presentation.
- If value is less than `0.001` or is greater than or equal to `10_000_000`,
format it in exponential presentation.
- If a name ends with `"_ci"`, consider it a confidence interval.
Look up for attributes `"{name}_lower"` and `"{name}_upper"`,
and format the interval as `"[{lower_bound}, {lower_bound}]"`.
Expand Down Expand Up @@ -225,7 +232,8 @@ class DictsReprMixin(abc.ABC):
a percentage value. Round percentage values to 2 significant digits,
multiply by `100` and add `"%"`.
- Round other values to 3 significant values.
- If value is less than `0.001`, format it in exponential presentation.
- If value is less than `0.001` or is greater than or equal to `10_000_000`,
format it in exponential presentation.
- If a name ends with `"_ci"`, consider it a confidence interval.
Look up for attributes `"{name}_lower"` and `"{name}_upper"`,
and format the interval as `"[{lower_bound}, {lower_bound}]"`.
Expand Down Expand Up @@ -263,7 +271,8 @@ def to_pretty_dicts(
a percentage value. Round percentage values to 2 significant digits,
multiply by `100` and add `"%"`.
- Round other values to 3 significant values.
- If value is less than `0.001`, format it in exponential presentation.
- If value is less than `0.001` or is greater than or equal to `10_000_000`,
format it in exponential presentation.
- If a name ends with `"_ci"`, consider it a confidence interval.
Look up for attributes `"{name}_lower"` and `"{name}_upper"`,
and format the interval as `"[{lower_bound}, {lower_bound}]"`.
Expand Down Expand Up @@ -295,7 +304,8 @@ def to_string(
a percentage value. Round percentage values to 2 significant digits,
multiply by `100` and add `"%"`.
- Round other values to 3 significant values.
- If value is less than `0.001`, format it in exponential presentation.
- If value is less than `0.001` or is greater than or equal to `10_000_000`,
format it in exponential presentation.
- If a name ends with `"_ci"`, consider it a confidence interval.
Look up for attributes `"{name}_lower"` and `"{name}_upper"`,
and format the interval as `"[{lower_bound}, {lower_bound}]"`.
Expand Down Expand Up @@ -346,7 +356,8 @@ def to_html(
a percentage value. Round percentage values to 2 significant digits,
multiply by `100` and add `"%"`.
- Round other values to 3 significant values.
- If value is less than `0.001`, format it in exponential presentation.
- If value is less than `0.001` or is greater than or equal to `10_000_000`,
format it in exponential presentation.
- If a name ends with `"_ci"`, consider it a confidence interval.
Look up for attributes `"{name}_lower"` and `"{name}_upper"`,
and format the interval as `"[{lower_bound}, {lower_bound}]"`.
Expand Down
2 changes: 2 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ def test_format_num():
assert tea_tasting.utils.format_num(float("-inf")) == "-∞"
assert tea_tasting.utils.format_num(0.00012345) == "1.23e-04"
assert tea_tasting.utils.format_num(0.00099999) == "1.00e-03"
assert tea_tasting.utils.format_num(9_999_999) == "9999999"
assert tea_tasting.utils.format_num(10_000_000) == "1.00e+07"
assert tea_tasting.utils.format_num(12345, thousands_sep=" ") == "12 345"
assert tea_tasting.utils.format_num(1.2345, decimal_point=",") == "1,23"
assert tea_tasting.utils.format_num(0) == "0.00"
Expand Down

0 comments on commit 9897cc5

Please sign in to comment.