File size: 1,416 Bytes
598316f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | diff --git a/src/humanize/filesize.py b/src/humanize/filesize.py
index fb675fd..cf09f1e 100644
--- a/src/humanize/filesize.py
+++ b/src/humanize/filesize.py
@@ -42,6 +42,8 @@ def naturalsize(
binary: bool = False,
gnu: bool = False,
format: str = "%.1f",
+ *,
+ rollover: bool = True,
) -> str:
"""Format a number of bytes like a human-readable filesize (e.g. 10 kB).
@@ -77,6 +79,7 @@ def naturalsize(
gnu (bool): If `True`, the binary argument is ignored and GNU-style
(`ls -sh` style) prefixes are used (K, M) with the 2**10 definition.
format (str): Custom formatter.
+ rollover (bool): Promote a rounded mantissa to the next suffix when available.
Returns:
str: Human readable representation of a filesize.
@@ -103,7 +106,11 @@ def naturalsize(
# mantissa afterward; rounding can push it up to `base` (e.g. 999999 is
# 999.999 kB, which formats to "1000.0 kB"). When that happens and a larger
# suffix is available, step up one suffix so the result reads "1.0 MB".
- if exp < len(suffix) and abs(float(format % (abs_bytes / (base**exp)))) >= base:
+ if (
+ rollover
+ and exp < len(suffix)
+ and abs(float(format % (abs_bytes / (base**exp)))) >= base
+ ):
exp += 1
space = "" if gnu else " "
ret: str = format % (bytes_ / (base**exp)) + space + _(suffix[exp - 1])
|