From 8f87118725a70329801b3f42b8eed2cc8c8e0dc1 Mon Sep 17 00:00:00 2001 From: Eduard Braun Date: Sat, 18 Feb 2017 19:01:26 +0100 Subject: [PATCH] Only use number representation with reduced precision if it is shorter than the initial representation. Before it could happen that "123" was replaced with "1e3" if precision was set to 1 which is obviously not desirable. --- scour/scour.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/scour/scour.py b/scour/scour.py index 24bfaa6..7d2e320 100644 --- a/scour/scour.py +++ b/scour/scour.py @@ -2623,6 +2623,16 @@ def scourUnitlessLength(length, needsRendererWorkaround=False): # length is of if not isinstance(length, Decimal): length = getcontext().create_decimal(str(length)) + # remove trailing zeroes as we do not care for significance + intLength = length.to_integral_value() + if length == intLength: + length = Decimal(intLength) + else: + length = length.normalize() + + # Gather the initial non-scientific notation version of the coordinate (we want to compare with it later) + initial_value = '{0:f}'.format(length) + # reduce numeric precision # plus() corresponds to the unary prefix plus operator and applies context precision and rounding length = scouringContext.plus(length) @@ -2641,6 +2651,7 @@ def scourUnitlessLength(length, needsRendererWorkaround=False): # length is of nonsci = nonsci[1:] # remove the 0, leave the dot elif len(nonsci) > 3 and nonsci[:3] == '-0.': nonsci = '-' + nonsci[2:] # remove the 0, leave the minus and dot + return_value = nonsci # Gather the scientific notation version of the coordinate which # can only be shorter if the length of the number is at least 4 characters (e.g. 1000 = 1e3). @@ -2653,11 +2664,13 @@ def scourUnitlessLength(length, needsRendererWorkaround=False): # length is of sci = six.text_type(length) + 'e' + six.text_type(exponent) if len(sci) < len(nonsci): - return sci - else: - return nonsci + return_value = sci + + # Return the shortest representation (if they are equal prefer the original as it still has the full precision) + if len(return_value) < len(initial_value): + return return_value else: - return nonsci + return initial_value def reducePrecision(element):