From c1607a1f792c8fde90a900d4f6713d603a6b1ed1 Mon Sep 17 00:00:00 2001 From: Seth Underwood Date: Sun, 21 Jun 2026 23:23:03 -0400 Subject: [PATCH] fix: prevent scientific notation for exact powers of 10 --- src/fsize/__init__.py | 2 +- tests/test_fsize_init.py | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/fsize/__init__.py b/src/fsize/__init__.py index be419c8..21df579 100644 --- a/src/fsize/__init__.py +++ b/src/fsize/__init__.py @@ -200,7 +200,7 @@ def __format__(self, format_spec: str) -> str: raise AssertionError(f"unhandled unit: {unit!r}") n = self.real / self._convert ** _UNIT_POWERS[unit] - log_digits = math.ceil(math.log10(n)) if n > 0 else 0 + log_digits = math.floor(math.log10(n)) + 1 if n > 0 else 0 out_format_spec = ( f"{fill}{align}{width}{grouping}" + "." diff --git a/tests/test_fsize_init.py b/tests/test_fsize_init.py index c2646d1..6379031 100755 --- a/tests/test_fsize_init.py +++ b/tests/test_fsize_init.py @@ -442,6 +442,13 @@ def test_format_empty_spec(): assert format(z, "") == str(z) +def test_format_no_scientific_notation(): + """Test that exact powers of 10 do not produce scientific notation.""" + assert format(FSize(10 * 1024), "K") == "10" + assert format(FSize(100 * 1024), "K") == "100" + assert format(FSize(1000 * 1024), "K") == "1000" + + def test_format_all_units(): """Test that every unit in _UNIT_POWERS works via format.""" # Binary FSize (1 EiB)