From afcbb475d01287dab22b42e7ab1933728f76c75e Mon Sep 17 00:00:00 2001 From: Aigio Liu Date: Thu, 12 Aug 2021 15:05:14 +0800 Subject: [PATCH] SizeConverter --- src/Common.CoreLib/IOPath.cs | 26 ++++++-- .../Application/Converters/SizeConverter.cs | 60 ++++++++++--------- 2 files changed, 51 insertions(+), 35 deletions(-) diff --git a/src/Common.CoreLib/IOPath.cs b/src/Common.CoreLib/IOPath.cs index fe8076d8b..30eeb341f 100644 --- a/src/Common.CoreLib/IOPath.cs +++ b/src/Common.CoreLib/IOPath.cs @@ -397,14 +397,28 @@ namespace System const double unit = 1024d; static readonly string[] units = new[] { "B", "KB", "MB", "GB", "TB" }; + + public static (double length, string unit) GetSize(double length) + { + if (length > 0d) + { + for (int i = 0; i < units.Length; i++) + { + if (i > 0) length /= unit; + if (length < unit) return (length, units[i]); + } + return (length, units.Last()); + } + else + { + return (0, units.First()); + } + } + public static string GetSizeString(double length) { - for (int i = 0; i < units.Length; i++) - { - if (i > 0) length /= unit; - if (length < unit) return $"{length:0.00} {units[i]}"; - } - return $"{length:0.00} {units.Last()}"; + (length, string unit) = GetSize(length); + return $"{length:0.00} {unit}"; } } } \ No newline at end of file diff --git a/src/ST.Client.Desktop.Avalonia/Application/Converters/SizeConverter.cs b/src/ST.Client.Desktop.Avalonia/Application/Converters/SizeConverter.cs index 9776a36d7..fc8b3135b 100644 --- a/src/ST.Client.Desktop.Avalonia/Application/Converters/SizeConverter.cs +++ b/src/ST.Client.Desktop.Avalonia/Application/Converters/SizeConverter.cs @@ -5,40 +5,42 @@ namespace System.Application.Converters { public class SizeConverter : IValueConverter { - private const int SizeUnit = 1024; + //private const int SizeUnit = 1024; public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (double.TryParse(value.ToString(), out double b)) { - if (b > 0) - { - if (b > SizeUnit) - { - var kb = b / SizeUnit; - if (kb > SizeUnit) - { - var mb = kb / SizeUnit; - if (mb > SizeUnit) - { - var gb = mb / SizeUnit; - if (gb > SizeUnit) - { - var tb = gb / SizeUnit; - return tb.ToString("###,###.##") + " TB"; - } - return gb.ToString("###,###.##") + " GB"; - } - return mb.ToString("###,###.##") + " MB"; - } - return kb.ToString("###,###.##") + " KB"; - } - return b.ToString("###,###.##") + " B"; - } - else - { - return "0 B"; - } + (var length, string unit) = IOPath.GetSize(b); + return $"{length:###,###.##} {unit}"; + //if (b > 0) + //{ + // if (b > SizeUnit) + // { + // var kb = b / SizeUnit; + // if (kb > SizeUnit) + // { + // var mb = kb / SizeUnit; + // if (mb > SizeUnit) + // { + // var gb = mb / SizeUnit; + // if (gb > SizeUnit) + // { + // var tb = gb / SizeUnit; + // return tb.ToString("###,###.##") + " TB"; + // } + // return gb.ToString("###,###.##") + " GB"; + // } + // return mb.ToString("###,###.##") + " MB"; + // } + // return kb.ToString("###,###.##") + " KB"; + // } + // return b.ToString("###,###.##") + " B"; + //} + //else + //{ + // return "0 B"; + //} } return value; }