统计大小
This commit is contained in:
33
Converters/SizeConverter.cs
Normal file
33
Converters/SizeConverter.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Windows.Data;
|
||||
|
||||
namespace HashCalculator.GUI.Converters
|
||||
{
|
||||
internal class SizeConverter : IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
var size = (int)value;
|
||||
var suffixes = new[]
|
||||
{
|
||||
(1024, 1.0, ""),
|
||||
(1024 * 1024, 1024.0, "K"),
|
||||
(1024 * 1024 * 1024, 1024 * 1024.0, "M"),
|
||||
};
|
||||
foreach (var (limit, divisor, suffix) in suffixes)
|
||||
{
|
||||
if (size < limit)
|
||||
{
|
||||
return $"{size / divisor:0.00}{suffix}B";
|
||||
}
|
||||
}
|
||||
return $"{size / (1024.0 * 1024.0 * 1024.0):0.00}GB";
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user