Fix display of size values in web interface (#2703)

* Fix display of size values in web interface:

The web interface rounded large number values down using Math.floor(),
causing the web interface to display faulty torrent/download sizes.
A torrent of size 1,92 GB (915 pieces @ 2,1 MB) was shown as "1GB".
After about half the download was done, the UI showed "1GB of 1GB (55%)", which is rather irritating.

The fix removes the `Math.floor()` and adds fractions to the larger size values.
This commit is contained in:
Frank Aurich 2022-02-24 20:27:32 +01:00 committed by GitHub
parent abfd47a4a8
commit cc7d1c5f01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 8 deletions

File diff suppressed because one or more lines are too long

View File

@ -9,19 +9,45 @@ const number_format = new Intl.NumberFormat(current_locale);
const kilo = 1000;
const mem_formatters = [
new Intl.NumberFormat(current_locale, { style: 'unit', unit: 'byte' }),
new Intl.NumberFormat(current_locale, { style: 'unit', unit: 'kilobyte' }),
new Intl.NumberFormat(current_locale, { style: 'unit', unit: 'megabyte' }),
new Intl.NumberFormat(current_locale, { style: 'unit', unit: 'gigabyte' }),
new Intl.NumberFormat(current_locale, { style: 'unit', unit: 'terabyte' }),
new Intl.NumberFormat(current_locale, { style: 'unit', unit: 'petabyte' }),
new Intl.NumberFormat(current_locale, {
maximumFractionDigits: 0,
style: 'unit',
unit: 'byte',
}),
new Intl.NumberFormat(current_locale, {
maximumFractionDigits: 0,
style: 'unit',
unit: 'kilobyte',
}),
new Intl.NumberFormat(current_locale, {
maximumFractionDigits: 0,
style: 'unit',
unit: 'megabyte',
}),
new Intl.NumberFormat(current_locale, {
maximumFractionDigits: 2,
style: 'unit',
unit: 'gigabyte',
}),
new Intl.NumberFormat(current_locale, {
maximumFractionDigits: 2,
style: 'unit',
unit: 'terabyte',
}),
new Intl.NumberFormat(current_locale, {
maximumFractionDigits: 2,
style: 'unit',
unit: 'petabyte',
}),
];
const fmt_kBps = new Intl.NumberFormat(current_locale, {
maximumFractionDigits: 2,
style: 'unit',
unit: 'kilobyte-per-second',
});
const fmt_MBps = new Intl.NumberFormat(current_locale, {
maximumFractionDigits: 2,
style: 'unit',
unit: 'megabyte-per-second',
});
@ -51,7 +77,7 @@ export const Formatter = {
let size = bytes;
for (const nf of mem_formatters) {
if (size < kilo) {
return nf.format(Math.floor(size));
return nf.format(size);
}
size /= kilo;
}