Skip to content

Commit c1ec6c9

Browse files
committed
code_review: PR #1537
- The highest unit of file size is `GB`. - Always display the original number of bytes. Signed-off-by: leo <[email protected]>
1 parent 622fa26 commit c1ec6c9

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/Converters/LongConverters.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@ public static class LongConverters
66
{
77
public static readonly FuncValueConverter<long, string> ToFileSize = new(bytes =>
88
{
9-
var suffixes = new[] { "", "ki", "Mi", "Gi", "Ti", "Pi", "Ei" };
10-
double dbl = bytes;
11-
var i = 0;
9+
if (bytes < KB)
10+
return $"{bytes} B";
1211

13-
while (dbl > 1024 && i < suffixes.Length - 1)
14-
{
15-
dbl /= 1024;
16-
i++;
17-
}
12+
if (bytes < MB)
13+
return $"{(bytes / KB):F3} KB ({bytes} B)";
1814

19-
return $"{dbl:0.#} {suffixes[i]}B";
15+
if (bytes < GB)
16+
return $"{(bytes / MB):F3} MB ({bytes} B)";
17+
18+
return $"{(bytes / GB):F3} GB ({bytes} B)";
2019
});
20+
21+
private const double KB = 1024;
22+
private const double MB = 1024 * 1024;
23+
private const double GB = 1024 * 1024 * 1024;
2124
}
2225
}

0 commit comments

Comments
 (0)