From be396dcb91c407b029cd21cc66a4c777970fd0e6 Mon Sep 17 00:00:00 2001 From: Andrew Hosgood Date: Tue, 3 Mar 2026 10:32:20 +0000 Subject: [PATCH 1/3] Apply suggested fix to tna_utilities/number.py from Copilot Autofix Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> --- tna_utilities/number.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tna_utilities/number.py b/tna_utilities/number.py index 5eeb903..d50671f 100644 --- a/tna_utilities/number.py +++ b/tna_utilities/number.py @@ -51,12 +51,12 @@ def numberish( def pretty_file_size(bytes, simplify=True): if not isinstance(bytes, int): raise ValueError("file_size must be an integer") - bits_unit = 1000 + byte_unit = 1000 suffixes = ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"] i = 0 prettified_file_size = bytes - while prettified_file_size >= bits_unit and i < len(suffixes) - 1: - prettified_file_size /= bits_unit + while prettified_file_size >= byte_unit and i < len(suffixes) - 1: + prettified_file_size /= byte_unit i += 1 if prettified_file_size == 0: return "0B" From af035adf32804b9d059c2d272779c1bad610af7a Mon Sep 17 00:00:00 2001 From: Andrew Hosgood Date: Tue, 3 Mar 2026 10:32:21 +0000 Subject: [PATCH 2/3] Apply suggested fix to tna_utilities/number.py from Copilot Autofix Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> --- tna_utilities/number.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/tna_utilities/number.py b/tna_utilities/number.py index d50671f..992e7d0 100644 --- a/tna_utilities/number.py +++ b/tna_utilities/number.py @@ -3,6 +3,22 @@ from typing import Union +def round_to_significant_figures(value: float, sig_figs: int = 1) -> float: + """ + Round a positive float to the given number of significant figures. + + This computes the appropriate number of decimal places for ``round`` based + on the order of magnitude of ``value``. For example, with ``sig_figs=1``, + values are rounded to one significant figure (e.g. 1234 -> 1000, 0.0123 -> 0.01). + """ + if value == 0: + return 0.0 + # Determine how many decimal places are needed so that ``round`` keeps + # ``sig_figs`` significant digits. + decimal_places = -int(math.floor(math.log10(abs(value)))) + (sig_figs - 1) + return round(value, decimal_places) + + def numberish( value: Union[float, int], simple_units: bool = False, @@ -27,10 +43,9 @@ def numberish( base_value = value / threshold if base_value.is_integer(): return f"{int(base_value)}{unit}" - base_value_rounded = round( - base_value, - -int(math.floor(math.log10(abs(base_value)))) + 1, - ) + # Round to one significant figure to produce a human-friendly value, + # e.g. 1234 -> 1000, 1.23 -> 1, 0.0123 -> 0.01. + base_value_rounded = round_to_significant_figures(base_value, 1) if base_value_rounded == base_value: prefix_text = "" elif isinstance(prefix_text, tuple): From e6339b4402d872dda2c826081e01770cde97ca1f Mon Sep 17 00:00:00 2001 From: Andrew Hosgood Date: Mon, 9 Mar 2026 10:18:08 +0000 Subject: [PATCH 3/3] Apply suggested fix to tna_utilities/number.py from Copilot Autofix Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> --- tna_utilities/number.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tna_utilities/number.py b/tna_utilities/number.py index d50671f..4a08812 100644 --- a/tna_utilities/number.py +++ b/tna_utilities/number.py @@ -50,7 +50,7 @@ def numberish( def pretty_file_size(bytes, simplify=True): if not isinstance(bytes, int): - raise ValueError("file_size must be an integer") + raise ValueError("bytes must be an integer") byte_unit = 1000 suffixes = ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"] i = 0