首页 > 其他 > 详细

BytesConverter

时间:2018-12-21 17:27:05      阅读:148      评论:0      收藏:0      [点我收藏+]
    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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!