zoukankan      html  css  js  c++  java
  • BytesConverter

        public class BytesConverter : IValueConverter
        {
            public bool IsSpeed { get; set; }
    
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                double kb = 1024.00;
                double mb = 1024.00 * 1024;
                double gb = 1024.00 * 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;
            }
        }

     食用方法:

    <converter:BytesConverter x:Key="BytesConverter" IsSpeed="False" />
  • 相关阅读:
    对象拷贝-深拷贝
    eclipse安装桌面快捷方式
    ajax 分页
    单例模式
    过滤器
    ajax参数详解
    json
    反射
    jdbc02
    jdbc --例子7
  • 原文地址:https://www.cnblogs.com/RedSky/p/10156952.html
Copyright © 2011-2022 走看看