[FIX] numbers formatting to correctly apply user locales

bzr revid: xmo@openerp.com-20131022085436-5845r3q51g9pl2ct
This commit is contained in:
Xavier Morel 2013-10-22 10:54:36 +02:00
parent b9dfef2fab
commit fc8ab0df05
2 changed files with 19 additions and 3 deletions

View File

@ -587,12 +587,19 @@ class FloatConverter(osv.AbstractModel):
def value_to_html(self, cr, uid, value, column, options=None, context=None):
precision = self.precision(cr, uid, column, options=options, context=context)
fmt = '%s' if precision is None else '%.{precision}f'
fmt = '%f' if precision is None else '%.{precision}f'
lang = self.user_lang(cr, uid, context)
return werkzeug.utils.escape(
lang.format(fmt.format(precision=precision), value, grouping=True))
formatted = lang.format(fmt.format(precision=precision),
value, grouping=True)
# %f does not strip trailing zeroes. %g does but its precision causes
# it to switch to scientific notation starting at a million *and* to
# strip decimals. So use %f and if no precision was specified manually
# strip trailing 0.
if not precision:
formatted = re.sub(r'(?:(0|\d+?)0+)$', r'\1', formatted)
return werkzeug.utils.escape(formatted)
class DateConverter(osv.AbstractModel):
_name = 'ir.qweb.field.date'

View File

@ -53,6 +53,12 @@ class TestIntegerExport(TestBasicExport):
self.assertEqual(value, "42")
class TestFloatExport(TestBasicExport):
def setUp(self):
super(TestFloatExport, self).setUp()
self.registry('res.lang').write(self.cr, self.uid, [1], {
'grouping': '[3,0]'
})
def test_float(self):
converter = self.get_converter('float')
@ -65,6 +71,9 @@ class TestFloatExport(TestBasicExport):
value = converter(42.01234)
self.assertEqual(value, "42.01234")
value = converter(1234567.89)
self.assertEqual(value, '1,234,567.89')
def test_numeric(self):
converter = self.get_converter('numeric')