mirror of
https://github.com/BeyondDimension/SteamTools.git
synced 2026-10-25 11:39:56 +08:00
SizeConverter
This commit is contained in:
@@ -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}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user