Skip to content

Commit

Permalink
[FIX] web: remove human_number limitations
Browse files Browse the repository at this point in the history
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 #31140
  • Loading branch information
Polymorphe57 committed Feb 15, 2019
1 parent 03e8065 commit 24b0f09
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
12 changes: 11 additions & 1 deletion addons/web/static/src/js/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 21 additions & 1 deletion addons/web/static/tests/core/util_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

});

});
});

0 comments on commit 24b0f09

Please sign in to comment.