source: trunk/CrypPlugins/KeySearcher/Converter/LongToSizeMetricString.cs @ 1704

Last change on this file since 1704 was 1704, checked in by Paul Lelgemann, 12 years ago

+ KeySearcher: display of aggregated linkmanager statistics
o KeySearcher: job identifier extended with sample decryption data to preserve decryption algorithm settings

File size: 981 bytes
Line 
1using System;
2using System.Globalization;
3using System.Windows.Data;
4
5namespace KeySearcherConverter
6{
7    class LongToSizeMetricString : IValueConverter
8    {
9        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
10        {
11            var bytes = (long)value;
12            const long scale = 1024;
13            var orders = new[] { "EiB", "PiB", "TiB", "GiB", "MiB", "KiB", "Bytes" };
14            var max = (long)Math.Pow(scale, (orders.Length - 1));
15
16            foreach (var order in orders)
17            {
18                if (bytes > max)
19                    return string.Format("{0:##.##} {1}", Decimal.Divide(bytes, max), order);
20                max /= scale;
21            }
22
23            return "0 Bytes";
24        }
25
26        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
27        {
28            throw new NotImplementedException();
29        }
30    }
31}
Note: See TracBrowser for help on using the repository browser.