码迷,mamicode.com
首页 > 其他好文 > 详细

BytesConverter

时间:2018-12-21 17:38:39      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:convert   bin   col   lse   windows   color   not   speed   back   

    public class BytesConverter : IValueConverter
    {
        public bool IsSpeed { get; set; }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double kb = 1024.00 * 1024;
            double mb = 1024.00 * 1024 * 1024;
            double gb = 1024.00 * 1024 * 1024 * 1024;
            double bytes = value == null ? 0 : (long)(value);
            string result = null;

            if (bytes < kb)
                result = (bytes / 1024.00).ToString("N2") + "B";
            else if (bytes >= kb && bytes < mb)
                result = (bytes / kb).ToString("N2") + "KB";
            else if (bytes >= mb && bytes < gb)
                result = (bytes / mb).ToString("N2") + "MB";
            else
                result = bytes / gb + "GB";
            if (IsSpeed)
                result += "/s";
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return System.Windows.Data.Binding.DoNothing;
        }
    }

 

BytesConverter

标签:convert   bin   col   lse   windows   color   not   speed   back   

原文地址:https://www.cnblogs.com/RedSky/p/10156952.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!