From 80ff5a18b1d4ffdf71c5d974f03016ef183a79ca Mon Sep 17 00:00:00 2001 From: Fabian Dill Date: Tue, 21 Jun 2022 20:50:40 +0200 Subject: [PATCH] remove limit of 1000 Yotta-Joule in EnergyLink (#689) --- Utils.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/Utils.py b/Utils.py index a8d2f112..a19f66bf 100644 --- a/Utils.py +++ b/Utils.py @@ -12,6 +12,7 @@ import io import collections import importlib import logging +import decimal if typing.TYPE_CHECKING: from tkinter import Tk @@ -494,17 +495,25 @@ class VersionException(Exception): pass +def chaining_prefix(index: int, labels: typing.Tuple[str]) -> str: + text = "" + max_label = len(labels) - 1 + while index > max_label: + text += labels[-1] + index -= max_label + return labels[index] + text + + # noinspection PyPep8Naming 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 - - while value > power: + value = decimal.Decimal(value) + while value >= power: value /= power n += 1 - if type(value) == int: - return f"{value} {power_labels[n]}" - else: - return f"{value:0.3f} {power_labels[n]}" + + return f"{value.quantize(decimal.Decimal('1.00'))} {chaining_prefix(n, power_labels)}" def get_fuzzy_ratio(word1: str, word2: str) -> float: