From 1c1443c86295d48c2ea4961cf8ac449c812812a6 Mon Sep 17 00:00:00 2001 From: DarkPhoenix Date: Mon, 26 Aug 2019 02:56:11 +0300 Subject: [PATCH] Move calculation of normalization shift to separate function --- gui/utils/numberFormatter.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/gui/utils/numberFormatter.py b/gui/utils/numberFormatter.py index 12d53a564..d37db9b64 100644 --- a/gui/utils/numberFormatter.py +++ b/gui/utils/numberFormatter.py @@ -104,9 +104,7 @@ def roundToPrec(val, prec): # Also make sure that we do not ask to calculate logarithm of zero if int(val) == val: return int(val) - # Find round factor, taking into consideration that we want to keep at least prec - # positions for fractions with zero integer part (e.g. 0.0000354 for prec=3) - roundFactor = int(prec - math.ceil(math.log10(abs(val)))) + roundFactor = int(prec - getNormalizationShift(val) - 1) # But we don't want to round integers if roundFactor < 0: roundFactor = 0 @@ -118,6 +116,16 @@ def roundToPrec(val, prec): return val +def getNormalizationShift(val): + """ + Given a value, return how many positions to the left we should shift + decimal separator to get number in range of [1, 10) + """ + if val == 0: + return None + return math.floor(math.log10(abs(val))) + + def roundDec(val, prec): if int(val) == val: return int(val)