Skip to content

Commit

Permalink
Cockpit: limit number of shown decimal places when formatting numbers
Browse files Browse the repository at this point in the history
Fixes #2181
Closes #2182
  • Loading branch information
dperpeet committed Apr 21, 2015
1 parent 7c84786 commit 672b2a3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
32 changes: 17 additions & 15 deletions pkg/base1/cockpit.js
Expand Up @@ -2131,7 +2131,6 @@ function full_scope(cockpit, $, po) {
};

function format_units(number, suffixes, factor, separate) {
var divided = false;
var quotient;
var suffix = null;

Expand All @@ -2147,7 +2146,6 @@ function full_scope(cockpit, $, po) {
if (factor == suffixes[keys[y]][x]) {
number = number / Math.pow(keys[y], x);
suffix = factor;
divided = x > 0;
break;
}
}
Expand All @@ -2163,33 +2161,37 @@ function full_scope(cockpit, $, po) {
if (quotient < factor) {
number = quotient;
suffix = suffixes[factor][i];
divided = divisor > 1;
break;
}
divisor *= factor;
}
}

var ret;

if (!suffix) {
ret = [number.toString()];
if (!separate)
ret = ret.join(" ");
return ret;
}

/* non-zero values should never appear zero */
if (number > 0 && number < 0.1)
number = 0.1;
else if (number < 0 && number > -0.1)
number = -0.1;

var ret;

/* TODO: Make the decimal separator translatable */
if (number === 0 || !divided)
ret = [number.toString(), suffix];
var string_representation;

/* only show as integer if we have a natural number */
if (number % 1 === 0)
string_representation = number.toString();
else
string_representation = number.toFixed(1);

if (suffix)
ret = [string_representation, suffix];
else
ret = [number.toFixed(1), suffix];
ret = [string_representation];

if (!separate)
ret = ret.join(" ");

return ret;
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/base1/test-format.html
Expand Up @@ -58,10 +58,10 @@ <h2 id="qunit-userAgent"></h2><ol id="qunit-tests"></ol>
[ 2000, 1024, "2.0 KB" ],
[ 1999, 1000, "2.0 KB" ],
[ 1999, 1024, "2.0 KB" ],
[ 1000000, 1000, "1.0 MB" ],
[ 1000000, 1000, "1 MB" ],
[ 1000000, 1024, "976.6 KB" ],
[ 2000000, 1024, "1.9 MB" ],
[ 2000000, 1000, "2.0 MB" ],
[ 2000000, 1000, "2 MB" ],
[ 2000000, "MB", "1.9 MB" ],
[ 2000000, "KB", "1953.1 KB" ],
[ 1, "KB", "0.1 KB" ],
Expand Down Expand Up @@ -97,6 +97,8 @@ <h2 id="qunit-userAgent"></h2><ol id="qunit-tests"></ol>
test("format_bits_per_sec", function() {
var checks = [
[ 555, "555 bps" ],
[ 555.23456789, "555.2 bps" ],
[ 555.98765432, "556.0 bps" ],
[ 2555, "2.6 Kbps" ]
];

Expand Down
2 changes: 1 addition & 1 deletion test/check-storage
Expand Up @@ -837,7 +837,7 @@ class TestStorage(MachineCase):
b.click("#format-disk-format")
b.wait_popdown("storage_format_disk_dialog")

b.wait_in_text("#storage_detail_content", "50.0 MB Free Space")
b.wait_in_text("#storage_detail_content", "50 MB Free Space")

def testHidden(self):
m = self.machine
Expand Down

0 comments on commit 672b2a3

Please sign in to comment.