mirror of
https://github.com/chylex/Discord-History-Tracker.git
synced 2025-06-07 19:03:03 +03:00
Add one decimal place to MB/GB/TB in the table in the Attachments tab
This commit is contained in:
parent
d463b407f4
commit
739e87c5ab
@ -4,12 +4,26 @@ using Avalonia.Data.Converters;
|
|||||||
|
|
||||||
namespace DHT.Desktop.Common {
|
namespace DHT.Desktop.Common {
|
||||||
sealed class BytesValueConverter : IValueConverter {
|
sealed class BytesValueConverter : IValueConverter {
|
||||||
private static readonly string[] Units = {
|
private sealed class Unit {
|
||||||
"B",
|
private readonly string label;
|
||||||
"kB",
|
private readonly string numberFormat;
|
||||||
"MB",
|
|
||||||
"GB",
|
public Unit(string label, int decimalPlaces) {
|
||||||
"TB"
|
this.label = label;
|
||||||
|
this.numberFormat = "{0:n" + decimalPlaces + "}";
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Format(double size) {
|
||||||
|
return string.Format(Program.Culture, numberFormat, size) + " " + label;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static readonly Unit[] Units = {
|
||||||
|
new ("B", decimalPlaces: 0),
|
||||||
|
new ("kB", decimalPlaces: 0),
|
||||||
|
new ("MB", decimalPlaces: 1),
|
||||||
|
new ("GB", decimalPlaces: 1),
|
||||||
|
new ("TB", decimalPlaces: 1)
|
||||||
};
|
};
|
||||||
|
|
||||||
private const int Scale = 1000;
|
private const int Scale = 1000;
|
||||||
@ -17,13 +31,7 @@ namespace DHT.Desktop.Common {
|
|||||||
private static string Convert(ulong size) {
|
private static string Convert(ulong size) {
|
||||||
int power = size == 0L ? 0 : (int) Math.Log(size, Scale);
|
int power = size == 0L ? 0 : (int) Math.Log(size, Scale);
|
||||||
int unit = power >= Units.Length ? Units.Length - 1 : power;
|
int unit = power >= Units.Length ? Units.Length - 1 : power;
|
||||||
if (unit == 0) {
|
return Units[unit].Format(unit == 0 ? size : size / Math.Pow(Scale, unit));
|
||||||
return string.Format(Program.Culture, "{0:n0}", size) + " " + Units[unit];
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
double humanReadableSize = size / Math.Pow(Scale, unit);
|
|
||||||
return string.Format(Program.Culture, "{0:n0}", humanReadableSize) + " " + Units[unit];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) {
|
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user