From 01897c8aa6b946492eeaba1595e7e925837527b7 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Fri, 21 Oct 2011 14:07:41 +0200 Subject: [PATCH] [FIX] completely broken float formatting when leading 0 Formatting converted the decimal part into an integer, dropping all leading 0s and failing to reinsert them. The conversion also lead to compounded errors, casting 0.1100 into 0.10999 (something like that). Replace this by a straight float formatting via _.sprintf and the slicing of the decimal part from the resulting string (Math.abs and %1 clamp the value to format to the [0, 1[ range, ensuring we just need to slice off the first two characters). Also add some tests. lp bug: https://launchpad.net/bugs/879355 fixed bzr revid: xmo@openerp.com-20111021120741-813im5bzup7vho1f --- addons/web/static/src/js/formats.js | 11 +++++++---- addons/web/static/test/formats.js | 12 ++++++++++++ addons/web/static/test/test.html | 3 --- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/addons/web/static/src/js/formats.js b/addons/web/static/src/js/formats.js index 94b2c7085db..3ab972f7a86 100644 --- a/addons/web/static/src/js/formats.js +++ b/addons/web/static/src/js/formats.js @@ -28,10 +28,13 @@ openerp.web.format_value = function (value, descriptor, value_if_empty) { case 'float': var precision = descriptor.digits ? descriptor.digits[1] : 2; var int_part = Math.floor(value); - var dec_part = Math.abs(Math.floor((value % 1) * Math.pow(10, precision))); - return _.sprintf('%d%s%d', - int_part, - openerp.web._t.database.parameters.decimal_point, dec_part); + var dec_part = _.sprintf( + '%.' + precision + 'f', + Math.abs(value) % 1).substring(2); + return _.sprintf('%d%s%s', + int_part, + openerp.web._t.database.parameters.decimal_point, + dec_part); case 'float_time': return _.sprintf("%02d:%02d", Math.floor(value), diff --git a/addons/web/static/test/formats.js b/addons/web/static/test/formats.js index 57ffd981ed8..3c3c6b06e72 100644 --- a/addons/web/static/test/formats.js +++ b/addons/web/static/test/formats.js @@ -27,6 +27,18 @@ $(document).ready(function () { var fl = 12.1234; var str = openerp.web.format_value(fl, {type:"float"}); equal(str, "12.12"); + equal(openerp.web.format_value(12.02, {type: 'float'}), + '12.02'); + equal(openerp.web.format_value(0.0002, {type: 'float', digits: [1, 3]}), + '0.000'); + equal(openerp.web.format_value(0.0002, {type: 'float', digits: [1, 4]}), + '0.0002'); + equal(openerp.web.format_value(0.0002, {type: 'float', digits: [1, 6]}), + '0.000200'); + equal(openerp.web.format_value(1, {type: 'float', digits: [1, 6]}), + '1.000000'); + equal(openerp.web.format_value(1, {type: 'float'}), + '1.00'); }); test("parse_datetime", function () { var val = openerp.web.str_to_datetime("2009-05-04 12:34:23"); diff --git a/addons/web/static/test/test.html b/addons/web/static/test/test.html index e45b982fa9e..f6248e705c0 100644 --- a/addons/web/static/test/test.html +++ b/addons/web/static/test/test.html @@ -33,9 +33,6 @@ -

OpenERP web Test Suite