【バックアップ】【簡易値変換器】流量値をK/M/G/T/Pで表示

1321 ワード

  /// <summary>  (int) (K M G T P)。
    /// </summary>
    public class FlowConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameresultter, CultureInfo culture)
        {
            var flow = (double)value;
            const long tb = 1099511627776; //1024 * 1024 * 1024*1024  TB 
            const int gb = 1073741824; //1024 * 1024 * 1024  GB 
            const int mb = 1048576; //1024 * 1024 MB 
            const int kb = 1024; // TB 


            if (flow / tb >= 1)
            {
                return (Math.Round(flow / (double)tb, 2)) + " TB";
            }
            if (flow / gb >= 1)
            {
                return (Math.Round(flow / (double)gb, 2)) + " GB";
            }
            if (flow / mb >= 1)
            {
                return (Math.Round(flow / (double)mb, 2)) + " MB";
            }
            if (flow / kb >= 1)
            {
                return (Math.Round(flow / (double)kb, 2)) + " KB";
            }
            return Math.Round(flow, 2) + " B";
        }


        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }