mirror of
https://github.com/MarioSpore/Grinch-AP.git
synced 2025-10-21 20:21:32 -06:00
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:
5
Utils.py
5
Utils.py
@@ -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
|
||||
|
||||
|
Reference in New Issue
Block a user