base: Show numbers with more precision

Now we use 3 digits of precision (for non-integers), instead of 1
digit after the comma.

This should help especially when showing usage numbers for
filesystems, where we might show a small number (used bytes) with a
unit that is appropriate for a big number (total bytes).

Fixes #8361
Closes #8382
This commit is contained in:
Marius Vollmer 2018-01-09 14:45:38 +02:00 committed by Martin Pitt
parent 386d312c2b
commit 0218d776ca
3 changed files with 41 additions and 39 deletions

View File

@ -1446,21 +1446,24 @@ function factory() {
};
cockpit.format_number = function format_number(number) {
/* 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;
/* TODO: Make the decimal separator translatable */
/* only show as integer if we have a natural number */
/* We show 3 digits of precison but avoid scientific notation.
* We also show integers without digits after the comma.
*/
if (!number && number !== 0)
return "";
else if (number % 1 === 0)
return number.toString();
else if (number > 0 && number <= 0.001)
return "0.001";
else if (number < 0 && number >= -0.001)
return "-0.001";
else if (number > 999 || number < -999)
return number.toFixed(0);
else
return number.toFixed(1);
return number.toPrecision(3);
};
function format_units(number, suffixes, factor, separate) {

View File

@ -17,22 +17,22 @@ QUnit.test("format", function() {
QUnit.test("format_number", function () {
var checks = [
[ 123.4, "123.4" ],
[ 123.45, "123.5" ],
[ 123.44, "123.4" ],
[ 23.4, "23.4" ],
[ 23.46, "23.5" ],
[ 23.44, "23.4" ],
[ -123.4, "-123.4" ],
[ -123.45, "-123.5" ],
[ -123.44, "-123.4" ],
[ -23.4, "-23.4" ],
[ -23.46, "-23.5" ],
[ -23.44, "-23.4" ],
[ 0, "0" ],
[ 0.01, "0.1" ],
[ -0.01, "-0.1" ],
[ 0.001, "0.001" ],
[ -0.001, "-0.001" ],
[ 123.0, "123" ],
[ 123.01, "123.0" ],
[ 123.01, "123" ],
[ -123.0, "-123" ],
[ -123.01, "-123.0" ],
[ -123.01, "-123" ],
[ null, "" ],
[ undefined, "" ],
];
@ -47,22 +47,22 @@ QUnit.test("format_number", function () {
QUnit.test("format_bytes", function() {
var checks = [
[ 999, 1000, "999" ],
[ 1934, undefined, "1.9 KiB" ],
[ 1934, 1000, "1.9 KB" ],
[ 2000, 1024, "2.0 KiB" ],
[ 1999, 1000, "2.0 KB" ],
[ 1999, 1024, "2.0 KiB" ],
[ 1934, undefined, "1.89 KiB" ],
[ 1934, 1000, "1.93 KB" ],
[ 2000, 1024, "1.95 KiB" ],
[ 1999, 1000, "2.00 KB" ],
[ 1999, 1024, "1.95 KiB" ],
[ 1000000, 1000, "1 MB" ],
[ 1000001, 1000, "1.0 MB" ],
[ 1000000, 1024, "976.6 KiB" ],
[ 2000000, 1024, "1.9 MiB" ],
[ 1000001, 1000, "1.00 MB" ],
[ 1000000, 1024, "977 KiB" ],
[ 2000000, 1024, "1.91 MiB" ],
[ 2000000, 1000, "2 MB" ],
[ 2000001, 1000, "2.0 MB" ],
[ 2000001, 1000, "2.00 MB" ],
[ 2000000, "MB", "2 MB" ],
[ 2000000, "MiB", "1.9 MiB" ],
[ 2000000, "MiB", "1.91 MiB" ],
[ 2000000, "KB", "2000 KB" ],
[ 2000000, "KiB", "1953.1 KiB" ],
[ 1, "KB", "0.1 KB" ],
[ 2000000, "KiB", "1953 KiB" ],
[ 1, "KB", "0.001 KB" ],
[ 0, "KB", "0 KB" ],
[ undefined, "KB", "" ],
[ null, "KB", "" ],
@ -116,7 +116,7 @@ QUnit.test("get_byte_units", function() {
QUnit.test("format_bytes_per_sec", function() {
var checks = [
[ 2555, "2.5 KiB/s" ]
[ 2555, "2.50 KiB/s" ]
];
assert.expect(checks.length);
@ -128,13 +128,12 @@ QUnit.test("format_bytes_per_sec", function() {
QUnit.test("format_bits_per_sec", function() {
var checks = [
[ 555, "555 bps" ],
[ 555.23456789, "555.2 bps" ],
[ 555.98765432, "556.0 bps" ],
[ 555, "555 bps" ],
[ 2555, "2.6 Kbps" ],
[ 55, "55 bps" ],
[ 55.23456789, "55.2 bps" ],
[ 55.98765432, "56.0 bps" ],
[ 2555, "2.56 Kbps" ],
[ 2000, "2 Kbps" ],
[ 2003, "2.0 Kbps" ]
[ 2003, "2.00 Kbps" ]
];
assert.expect(checks.length);

View File

@ -72,7 +72,7 @@ class TestStorage(StorageCase):
m.execute("mkdir /run/data && mount /dev/mapper/vdo0 /run/data")
self.content_row_wait_in_col(1, 1, "xfs File System")
self.content_tab_wait_in_info(1, 1, "Mounted At", "/run/data")
self.content_tab_wait_in_info(1, 1, "Used", "of 5.0 GiB")
self.content_tab_wait_in_info(1, 1, "Used", "of 4.99 GiB")
# Grow physical
@ -88,7 +88,7 @@ class TestStorage(StorageCase):
b.click(detail(4) + " button:contains(Grow)")
self.dialog({ "lsize": 10*1024 })
b.wait_in_text(detail(4), "used of 10 GiB")
self.content_tab_wait_in_info(1, 1, "Used", "of 10.0 GiB")
self.content_tab_wait_in_info(1, 1, "Used", "of 9.99 GiB")
# Stop