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.
This commit is contained in:
parent
b00b374e64
commit
8f87118725
1 changed files with 17 additions and 4 deletions
|
|
@ -2623,6 +2623,16 @@ def scourUnitlessLength(length, needsRendererWorkaround=False): # length is of
|
||||||
if not isinstance(length, Decimal):
|
if not isinstance(length, Decimal):
|
||||||
length = getcontext().create_decimal(str(length))
|
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
|
# reduce numeric precision
|
||||||
# plus() corresponds to the unary prefix plus operator and applies context precision and rounding
|
# plus() corresponds to the unary prefix plus operator and applies context precision and rounding
|
||||||
length = scouringContext.plus(length)
|
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
|
nonsci = nonsci[1:] # remove the 0, leave the dot
|
||||||
elif len(nonsci) > 3 and nonsci[:3] == '-0.':
|
elif len(nonsci) > 3 and nonsci[:3] == '-0.':
|
||||||
nonsci = '-' + nonsci[2:] # remove the 0, leave the minus and dot
|
nonsci = '-' + nonsci[2:] # remove the 0, leave the minus and dot
|
||||||
|
return_value = nonsci
|
||||||
|
|
||||||
# Gather the scientific notation version of the coordinate which
|
# 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).
|
# 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)
|
sci = six.text_type(length) + 'e' + six.text_type(exponent)
|
||||||
|
|
||||||
if len(sci) < len(nonsci):
|
if len(sci) < len(nonsci):
|
||||||
return sci
|
return_value = sci
|
||||||
else:
|
|
||||||
return nonsci
|
# 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:
|
else:
|
||||||
return nonsci
|
return initial_value
|
||||||
|
|
||||||
|
|
||||||
def reducePrecision(element):
|
def reducePrecision(element):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue