Utils: SI: fix rounding problems (#895)

* Utils: SI: fix rounding problems

999.999 would give 1000.00 instead of 1.00k

* Tests: add Utils: SI tests
This commit is contained in:
black-sliver
2022-08-12 00:46:11 +02:00
committed by GitHub
parent adc16fdd3d
commit b8ca41b45f
3 changed files with 47 additions and 2 deletions

View File

@@ -504,11 +504,12 @@ def chaining_prefix(index: int, labels: typing.Tuple[str]) -> str:
# noinspection PyPep8Naming
def format_SI_prefix(value, power=1000, power_labels=('', 'k', 'M', 'G', 'T', "P", "E", "Z", "Y")) -> str:
def format_SI_prefix(value, power=1000, power_labels=("", "k", "M", "G", "T", "P", "E", "Z", "Y")) -> str:
"""Formats a value into a value + metric/si prefix. More info at https://en.wikipedia.org/wiki/Metric_prefix"""
n = 0
value = decimal.Decimal(value)
while value >= power:
limit = power - decimal.Decimal("0.005")
while value >= limit:
value /= power
n += 1