From c1ec6c9823b82c6d810940c7688df3f612626e1b Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 7 Jul 2025 13:35:09 +0800 Subject: [PATCH] code_review: PR #1537 - The highest unit of file size is `GB`. - Always display the original number of bytes. Signed-off-by: leo --- src/Converters/LongConverters.cs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/Converters/LongConverters.cs b/src/Converters/LongConverters.cs index d7309363..bdbb7882 100644 --- a/src/Converters/LongConverters.cs +++ b/src/Converters/LongConverters.cs @@ -6,17 +6,20 @@ namespace SourceGit.Converters { public static readonly FuncValueConverter 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; } }