统计大小

This commit is contained in:
2023-06-04 16:05:22 +02:00
parent 157ebda24d
commit d4226132c5
6 changed files with 54 additions and 0 deletions

View 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();
}
}
}