diff --git a/StabilityMatrix.Avalonia/Converters/KiloFormatter.cs b/StabilityMatrix.Avalonia/Converters/KiloFormatter.cs new file mode 100644 index 00000000..32bd2241 --- /dev/null +++ b/StabilityMatrix.Avalonia/Converters/KiloFormatter.cs @@ -0,0 +1,50 @@ +using System; + +namespace StabilityMatrix.Avalonia.Converters; + +public class KiloFormatter : ICustomFormatter, IFormatProvider +{ + public object? GetFormat(Type? formatType) + { + return formatType == typeof(ICustomFormatter) ? this : null; + } + + public string Format(string? format, object? arg, IFormatProvider? formatProvider) + { + if (format == null || !format.Trim().StartsWith('K')) + { + if (arg is IFormattable formatArg) + { + return formatArg.ToString(format, formatProvider); + } + + return arg?.ToString() ?? string.Empty; + } + + var value = Convert.ToInt64(arg); + + return FormatNumber(value); + } + + private static string FormatNumber(long num) + { + if (num >= 100000000) + { + return (num / 1000000D).ToString("0.#M"); + } + if (num >= 1000000) + { + return (num / 1000000D).ToString("0.##M"); + } + if (num >= 100000) + { + return (num / 1000D).ToString("0.#K"); + } + if (num >= 10000) + { + return (num / 1000D).ToString("0.##K"); + } + + return num.ToString("#,0"); + } +} diff --git a/StabilityMatrix.Avalonia/Converters/KiloFormatterStringConverter.cs b/StabilityMatrix.Avalonia/Converters/KiloFormatterStringConverter.cs new file mode 100644 index 00000000..eaa58eea --- /dev/null +++ b/StabilityMatrix.Avalonia/Converters/KiloFormatterStringConverter.cs @@ -0,0 +1,25 @@ +using System; +using System.Globalization; +using Avalonia.Data.Converters; + +namespace StabilityMatrix.Avalonia.Converters; + +public class KiloFormatterStringConverter : IValueConverter +{ + /// + public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) + { + return value is null ? null : string.Format(new KiloFormatter(), "{0:K}", value); + } + + /// + public object? ConvertBack( + object? value, + Type targetType, + object? parameter, + CultureInfo culture + ) + { + return value is null ? null : throw new NotImplementedException(); + } +} diff --git a/StabilityMatrix.Avalonia/Views/CheckpointBrowserPage.axaml b/StabilityMatrix.Avalonia/Views/CheckpointBrowserPage.axaml index 688ac03f..2bba66bf 100644 --- a/StabilityMatrix.Avalonia/Views/CheckpointBrowserPage.axaml +++ b/StabilityMatrix.Avalonia/Views/CheckpointBrowserPage.axaml @@ -1,20 +1,24 @@ - + - - - + +