Formatting file sizes should be performed by a separate function (in tools.py, for example) that maps an integer value to an appropriate tuple of strings, e.g. 12899 → ("12.6", "KiB"). Separating amount and unit is necessary since different languages might have different places or orders in which amount and unit must appear in different situations. Also, the way amount and unit are formatted might be locale dependent. For example, some languages use a decimal comma instead of a decimal point, and there might be different words for "byte".
An example where this kind of formatting is performed is the Properties dialog since a0f5a7cc6c6dc6c81675e9e84c62343337e7382b.
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
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.
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