Files
ThreadPilot/Converters/BytesToStringConverter.cs
T
2026-06-24 17:52:45 +02:00

46 lines
1.2 KiB
C#

namespace ThreadPilot.Converters
{
using System;
using System.Globalization;
using System.Windows.Data;
public class BytesToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is long bytes)
{
return FormatBytes(bytes);
}
if (value is int intBytes)
{
return FormatBytes(intBytes);
}
return "0 B";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
private static string FormatBytes(long bytes)
{
string[] suffixes = { "B", "KB", "MB", "GB", "TB" };
int counter = 0;
decimal number = bytes;
while (Math.Round(number / 1024) >= 1)
{
number /= 1024;
counter++;
}
return $"{number:n1} {suffixes[counter]}";
}
}
}