Commit 64c1fd42 authored by Julien Muchembled's avatar Julien Muchembled

Formulator: clean up FloatWidget.format_value

parent ccdcbe59
...@@ -1807,38 +1807,30 @@ class FloatWidget(TextWidget): ...@@ -1807,38 +1807,30 @@ class FloatWidget(TextWidget):
if value not in (None,''): if value not in (None,''):
precision = field.get_value('precision') precision = field.get_value('precision')
input_style = field.get_value('input_style') input_style = field.get_value('input_style')
percent = 0 percent = '%' in input_style
original_value = value try:
if input_style.find('%')>=0:
percent=1
try:
value = float(value) * 100
except ValueError:
return value
try :
float_value = float(value) float_value = float(value)
if percent:
float_value *= 100
if precision not in (None, ''): if precision not in (None, ''):
# if we have a precision, then use it now # if we have a precision, then use it now
value = ('%%0.%sf' % precision) % float_value value = ('%%0.%sf' % precision) % float_value
else: else:
value = str(float_value) value = str(float_value)
# if this number is displayed in scientific notification,
# just return it as is
if 'e' in value:
return value
value, fpart = value.split('.')
except ValueError: except ValueError:
return value return value
# if this number displayed in scientific notification, just return it as
# is
if 'e' in value:
return value
value_list = value.split('.')
integer = value_list[0]
decimal_separator = '' decimal_separator = ''
decimal_point = '.' decimal_point = '.'
if input_style == "-1234.5": if input_style == "-1234.5":
decimal_point = '.' pass
elif input_style == '-1 234.5': elif input_style == '-1 234.5':
decimal_separator = ' ' decimal_separator = ' '
decimal_point = '.'
elif input_style == '-1 234,5': elif input_style == '-1 234,5':
decimal_separator = ' ' decimal_separator = ' '
decimal_point = ',' decimal_point = ','
...@@ -1847,37 +1839,28 @@ class FloatWidget(TextWidget): ...@@ -1847,37 +1839,28 @@ class FloatWidget(TextWidget):
decimal_point = ',' decimal_point = ','
elif input_style == '-1,234.5': elif input_style == '-1,234.5':
decimal_separator = ',' decimal_separator = ','
decimal_point = '.'
if input_style.find(decimal_separator) >= 0: if decimal_separator in input_style:
integer = value_list[0] if value.startswith('-'):
sign = ''
if integer.startswith('-'):
sign = '-' sign = '-'
integer = integer[1:]
i = len(integer) % 3
value = integer[:i]
while i != len(integer):
value += decimal_separator + integer[i:i+3]
i += 3
if value[0] == decimal_separator:
value = value[1:] value = value[1:]
value = '%s%s' % (sign, value) else:
else: sign = ''
value = value_list[0] i = len(value) % 3 or 3
integer = value[:i]
while i < len(value):
integer += decimal_separator + value[i:i+3]
i += 3
value = sign + integer
if precision != 0: if precision != 0:
value += decimal_point value += decimal_point
if precision not in (None, ''): if precision:
for i in range(0, precision): value += fpart[:precision].ljust(precision, '0')
if i < len(value_list[1]): else:
value += value_list[1][i] value += fpart
else:
value += '0'
else:
value += value_list[1]
if percent: if percent:
value += '%' value += '%'
return value.strip() return value
return '' return ''
def render(self, field, key, value, REQUEST, render_prefix=None): def render(self, field, key, value, REQUEST, render_prefix=None):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment