code_review: PR #1537

- The highest unit of file size is `GB`.
- Always display the original number of bytes.

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo
2025-07-07 13:35:09 +08:00
parent 622fa265ee
commit c1ec6c9823

View File

@@ -6,17 +6,20 @@ namespace SourceGit.Converters
{
public static readonly FuncValueConverter<long, string> ToFileSize = new(bytes =>
{
var suffixes = new[] { "", "ki", "Mi", "Gi", "Ti", "Pi", "Ei" };
double dbl = bytes;
var i = 0;
if (bytes < KB)
return $"{bytes} B";
while (dbl > 1024 && i < suffixes.Length - 1)
{
dbl /= 1024;
i++;
}
if (bytes < MB)
return $"{(bytes / KB):F3} KB ({bytes} B)";
return $"{dbl:0.#} {suffixes[i]}B";
if (bytes < GB)
return $"{(bytes / MB):F3} MB ({bytes} B)";
return $"{(bytes / GB):F3} GB ({bytes} B)";
});
private const double KB = 1024;
private const double MB = 1024 * 1024;
private const double GB = 1024 * 1024 * 1024;
}
}