WebUI: replace rounding function from MooTools

The `round()` returning floating point number is not a good idea. This is due to floating point
representation is imprecise and sometimes it cannot faithfully represent a number, for example
`0.09 + 0.01 !== 0.1 `. Therefore, it should be avoided and/or utilize other function
to achieve the goal.

Also, improve `window.qBittorrent.Misc.toFixedPointString()` and add test cases.

PR #22281.
This commit is contained in:
Chocobo1
2025-02-17 15:11:55 +08:00
committed by GitHub
parent 8da43a4054
commit 955688c125
8 changed files with 134 additions and 47 deletions

View File

@@ -383,25 +383,18 @@ window.qBittorrent.PropFiles ??= (() => {
is_seed = (files.length > 0) ? files[0].is_seed : true;
const rows = files.map((file, index) => {
let progress = (file.progress * 100).round(1);
if ((progress === 100) && (file.progress < 1))
progress = 99.9;
const ignore = (file.priority === FilePriority.Ignored);
const checked = (ignore ? TriState.Unchecked : TriState.Checked);
const remaining = (ignore ? 0 : (file.size * (1.0 - file.progress)));
const row = {
fileId: index,
checked: checked,
checked: (ignore ? TriState.Unchecked : TriState.Checked),
fileName: file.name,
name: window.qBittorrent.Filesystem.fileName(file.name),
size: file.size,
progress: progress,
progress: window.qBittorrent.Misc.toFixedPointString((file.progress * 100), 1),
priority: normalizePriority(file.priority),
remaining: remaining,
remaining: (ignore ? 0 : (file.size * (1 - file.progress))),
availability: file.availability
};
return row;
});