Skip to content
Snippets Groups Projects
Commit 3c093a81 authored by Mathieu Duckerts-Antoine's avatar Mathieu Duckerts-Antoine
Browse files

[FIX] web: human numbers and percentages

A recent modification of the float and integer
formatters allowing the rendering to be human readable
causes a bug in the percentage formatter for large values.
The reason is that the latter formatter uses formatFloat (wich can
now produce 'numbers' like 3k) and then parse the result. In the case
of 3k an error is raised since 3k is not recognized as a true number.

This fixes avoid to parse the number in case it has been formatted
to be 'readable'. Note that the parsing was essentially used
to produces better percentages like 10% instead of 10.00%.
This is not necessary in our case since utils.human_number takes care
of the unwanted (not all) decimals for us (the original number is always
rounded by utils.human_number).
parent 49695b00
No related branches found
No related tags found
No related merge requests found
......@@ -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.
......
......@@ -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) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment