diff options
| -rw-r--r-- | addons/web/static/src/js/fields/field_utils.js | 9 | ||||
| -rw-r--r-- | addons/web/static/tests/fields/field_utils_tests.js | 5 |
2 files changed, 11 insertions, 3 deletions
diff --git a/addons/web/static/src/js/fields/field_utils.js b/addons/web/static/src/js/fields/field_utils.js index 750cf055cdf..9d40c88019d 100644 --- a/addons/web/static/src/js/fields/field_utils.js +++ b/addons/web/static/src/js/fields/field_utils.js @@ -354,11 +354,16 @@ function formatMonetary(value, field, options) { * @param {number | false} value * @param {Object} [field] * @param {Object} [options] + * @param {function} [options.humanReadable] if returns true, parsing is avoided * @returns {string} */ function formatPercentage(value, field, options) { - value = formatFloat(value * 100, field, options) || '0'; - return parseFloat(value) + "%"; + options = options || {}; + var result = formatFloat(value * 100, field, options) || '0'; + if (options.humanReadable && options.humanReadable(value * 100)) { + return result + "%"; + } + return parseFloat(result) + "%"; } /** * Returns a string representing the value of the selection. diff --git a/addons/web/static/tests/fields/field_utils_tests.js b/addons/web/static/tests/fields/field_utils_tests.js index 0947bfbbd4b..84e1ae95bd6 100644 --- a/addons/web/static/tests/fields/field_utils_tests.js +++ b/addons/web/static/tests/fields/field_utils_tests.js @@ -135,7 +135,7 @@ QUnit.test('format binary', function (assert) { }); QUnit.test('format percentage', function (assert) { - assert.expect(8); + assert.expect(9); assert.strictEqual(fieldUtils.format.percentage(0), '0%'); assert.strictEqual(fieldUtils.format.percentage(0.5), '50%'); @@ -148,6 +148,9 @@ QUnit.test('format percentage', function (assert) { assert.strictEqual(fieldUtils.format.percentage(0.666666), '66.67%'); assert.strictEqual(fieldUtils.format.percentage(false), '0%'); + assert.strictEqual(fieldUtils.format.percentage(50, null, + {humanReadable: function (val) {return true;}}), '5k%' + ); }); QUnit.test('parse float', function(assert) { |
