Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/humanize/filesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from humanize.i18n import _gettext as _
from humanize.i18n import _gettext_noop as N_
from humanize.i18n import decimal_separator

suffixes = {
"decimal": (
Expand Down Expand Up @@ -107,5 +108,10 @@ def naturalsize(
if 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])
mantissa: str = format % (bytes_ / (base**exp))
decimal_sep = decimal_separator()
if decimal_sep != ".":
mantissa = mantissa.replace(".", decimal_sep, 1)

ret: str = mantissa + space + _(suffix[exp - 1])
return ret
10 changes: 10 additions & 0 deletions tests/test_filesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,13 @@ def test_naturalsize(test_args: list[int] | list[int | bool], expected: str) ->
test_args[0] = f"-{test_args[0]}"

assert humanize.naturalsize(*test_args) == "-" + expected


def test_naturalsize_uses_locale_decimal_separator() -> None:
humanize.i18n.activate("fr_FR")
try:
assert humanize.naturalsize(1_500_000) == "1,5 Mo"
finally:
humanize.i18n.deactivate()

assert humanize.naturalsize(1_500_000) == "1.5 MB"
10 changes: 5 additions & 5 deletions tests/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,10 @@ def test_intword_i18n(locale: str, number: int, expected_result: str) -> None:
("si_LK", 42, "බයිට් 42"),
("bn_BD", 1, "1 বাইট"),
("bn_BD", 42, "42 বাইট"),
("fr_FR", 42_000, "42.0 Ko"),
("fr_FR", 42_000_000, "42.0 Mo"),
("fr_FR", 42_000_000_000, "42.0 Go"),
("fr_FR", -42_000, "-42.0 Ko"),
("fr_FR", 42_000, "42,0 Ko"),
("fr_FR", 42_000_000, "42,0 Mo"),
("fr_FR", 42_000_000_000, "42,0 Go"),
("fr_FR", -42_000, "-42,0 Ko"),
],
)
def test_naturalsize_i18n(locale: str, value: float, expected_result: str) -> None:
Expand All @@ -327,7 +327,7 @@ def test_naturalsize_i18n_binary() -> None:
except FileNotFoundError:
pytest.skip("Generate .mo with tox -e compile-translations")
else:
assert humanize.naturalsize(3000, binary=True) == "2.9 Kio"
assert humanize.naturalsize(3000, binary=True) == "2,9 Kio"
finally:
humanize.i18n.deactivate()

Expand Down