diff --git a/addons/web/static/src/js/formats.js b/addons/web/static/src/js/formats.js index 385b1925454..c4af2d0cf6b 100644 --- a/addons/web/static/src/js/formats.js +++ b/addons/web/static/src/js/formats.js @@ -48,6 +48,19 @@ openerp.web.intersperse = function (str, indices, separator) { if (s) { result.push(s); } return result.reverse().join(separator); }; +/** + * Insert "thousands" separators in the provided number (which is actually + * a string) + * + * @param {String} num + * @returns {String} + */ +openerp.web.insert_thousand_seps = function (num) { + var negative = num[0] === '-'; + num = (negative ? num.slice(1) : num); + return (negative ? '-' : '') + openerp.web.intersperse( + num, _t.database.parameters.grouping, _t.database.parameters.thousands_sep); +}; /** * Formats a single atomic value based on a field descriptor * @@ -72,12 +85,13 @@ openerp.web.format_value = function (value, descriptor, value_if_empty) { var l10n = _t.database.parameters; switch (descriptor.widget || descriptor.type) { case 'integer': - return openerp.web.intersperse( - _.sprintf('%d', value), l10n.grouping, l10n.thousands_sep); + return openerp.web.insert_thousand_seps( + _.sprintf('%d', value)); case 'float': var precision = descriptor.digits ? descriptor.digits[1] : 2; - return _.sprintf('%.' + precision + 'f', value) - .replace('.', l10n.decimal_point); + var formatted = _.sprintf('%.' + precision + 'f', value).split('.'); + formatted[0] = openerp.web.insert_thousand_seps(formatted[0]); + return formatted.join(l10n.decimal_point); 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 189a1bb4475..d530571583c 100644 --- a/addons/web/static/test/formats.js +++ b/addons/web/static/test/formats.js @@ -41,6 +41,12 @@ $(document).ready(function () { '1.00'); equal(openerp.web.format_value(-11.25, {type: 'float'}), "-11.25"); + openerp.web._t.database.parameters.grouping = [1, 2, -1]; + equal(openerp.web.format_value(1111111.25, {type: 'float'}), + "1111,11,1.25"); + openerp.web._t.database.parameters.grouping = [1, 0]; + equal(openerp.web.format_value(-11.25, {type: 'float'}), + "-1,1.25"); }); // test("parse_datetime", function () { // var val = openerp.web.str_to_datetime("2009-05-04 12:34:23");