>>101554267
Stop trying to be clever. Write simple code that can be reasoned about as locally as possible.
from math import log
SI_PREFIX = ('', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
def prefix(n: int, /, *, si_units: bool = True) -> tuple[str, float]:
if n == 0:
return '', 0.0
base = 1000 if si_units else 1024
m = min(
int(log(abs(n), base)),
len(SI_PREFIX) - 1,
)
n /= base ** m
unit = SI_PREFIX[m]
if m and not si_units:
# upper() handles case difference between "KiB" and "kB".
unit = unit.upper() + 'i'
return unit, n
Also, zetta is smaller than yotta.