summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMathieu Duckerts-Antoine <dam@odoo.com>2019-02-14 16:04:18 +0000
committerMathieu Duckerts-Antoine <dam@odoo.com>2019-02-15 10:51:01 +0000
commit24b0f092b4f5ac7043430f106486fa2ab8829c21 (patch)
tree898ca1208bdbc7e7aac3909acace72bb119930ed
parent03e80650c7bf40a5fdac7019a1306e5e1db6565b (diff)
[FIX] web: remove human_number limitations
The formatting of very big numbers (like 1.0045e+22) was not handled adequately by the function 'human_number' in web.utils. For those numbers, it seems more appropriate to use the scientific format. The only difference introduced here is the following. Beyond a magnitude of e+21, we represent the numbers by themselves but we keep the number of decimals provided as parameter (or zero by default) and we remove useless zero decimals like in the other cases. For instance, 1.0045e+22 is replaced by 1e+22 instead of 1.00e+22 if ask 2 decimals of precision (and by 1.005e+22 for 3 decimals). We have also fixed a small bug occuring in the previous version of the function: the symbol 'E' was never used. So by the past the above number would have be represented as 10045000P and, even worse, 1.01e+45 by 1.012e+28P. closes odoo/odoo#31140
-rw-r--r--addons/web/static/src/js/core/utils.js12
-rw-r--r--addons/web/static/tests/core/util_tests.js22
2 files changed, 32 insertions, 2 deletions
diff --git a/addons/web/static/src/js/core/utils.js b/addons/web/static/src/js/core/utils.js
index f4031fccec9..a21c4241864 100644
--- a/addons/web/static/src/js/core/utils.js
+++ b/addons/web/static/src/js/core/utils.js
@@ -112,7 +112,17 @@ var utils = {
var d2 = Math.pow(10, decimals);
var val = _t('kMGTPE');
var symbol = '';
- for (var i = val.length - 1 ; i > 0 ; i--) {
+ var numberMagnitude = number.toExponential().split('e')[1];
+ // the case numberMagnitude >= 21 corresponds to a number
+ // better expressed in the scientific format.
+ if (numberMagnitude >= 21) {
+ // we do not use number.toExponential(decimals) because we want to
+ // avoid the possible useless O decimals: 1e.+24 prefered to 1.0e+24
+ number = Math.round(number * Math.pow(10, decimals - numberMagnitude)) / d2;
+ // formatterCallback seems useless here.
+ return number + 'e' + numberMagnitude;
+ }
+ for (var i = val.length; i > 0 ; i--) {
var s = Math.pow(10, i * 3);
if (s <= number / Math.pow(10, minDigits - 1)) {
number = Math.round(number * d2 / s) / d2;
diff --git a/addons/web/static/tests/core/util_tests.js b/addons/web/static/tests/core/util_tests.js
index c7158083dcb..f81dea7ce4e 100644
--- a/addons/web/static/tests/core/util_tests.js
+++ b/addons/web/static/tests/core/util_tests.js
@@ -55,6 +55,26 @@ QUnit.module('core', {}, function () {
assert.strictEqual(is_bin_size('64.2 Кб'), true);
});
+ QUnit.test('human_number', function (assert) {
+ assert.expect(13);
+
+ var human_number = utils.human_number;
+
+ assert.strictEqual(human_number(1020, 2, 1), '1.02k');
+ assert.strictEqual(human_number(1020000, 2, 2), '1020k');
+ assert.strictEqual(human_number(10200000, 2, 2), '10.2M');
+ assert.strictEqual(human_number(1020, 2, 1), '1.02k');
+ assert.strictEqual(human_number(1002, 2, 1), '1k');
+ assert.strictEqual(human_number(101, 2, 1), '101');
+ assert.strictEqual(human_number(64.2, 2, 1), '64');
+ assert.strictEqual(human_number(1e+18), '1E');
+ assert.strictEqual(human_number(1e+21, 2, 1), '1e+21');
+ assert.strictEqual(human_number(1.0045e+22, 2, 1), '1e+22');
+ assert.strictEqual(human_number(1.0045e+22, 3, 1), '1.005e+22');
+ assert.strictEqual(human_number(1.012e+43, 2, 1), '1.01e+43');
+ assert.strictEqual(human_number(1.012e+43, 2, 2), '1.01e+43');
+ });
+
});
-}); \ No newline at end of file
+});