FeRD - 2024-11-18

This was done in commit f813e027, however GLib has a function GLib.format_size (docs.gtk.org) which is likely a much better way to do it. For starters:

It's locale-aware

>>> import locale
>>> import gi
>>> from gi.repository import GLib
>>> locale.setlocale(locale.LC_ALL, 'en_US')  # US English
>>> print(GLib.format_size(312665))
312.7 kB
>>> locale.setlocale(locale.LC_ALL, 'de_DE')  # German German
>>> print(GLib.format_size(312665))
312,7 KB
>>> locale.setlocale(locale.LC_ALL, 'he_IL')  # Israeli Hebrew
>>> print(GLib.format_size(312665))
312.7 ק״ב

It uses SI units (multiples of 10) by default

The current ad-hoc code uses IEC powers-of-2 scaling. Binary scaling is only recommended for things like RAM sizes, that really are locked in to 1024-byte progressions.

>>> import mcomix.tools
>>> print(mcomix.tools.format_byte_size(312665))
305.3 KiB

The output is protected from line-wrapping.

The space between the value and units is actually a Unicode U+00A0 NO-BREAK SPACE.

 

Last edit: FeRD 2024-11-18