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" />
  • 相关阅读:
    servlet
    过滤器
    拦截器
    logback
    hibernate(1)
    函数的关键字参数
    函数的不定长参数
    打印星形三角
    九九乘法表
    udp客户端收发数据流程
  • 原文地址:https://www.cnblogs.com/RedSky/p/10156952.html
Copyright © 2011-2022 走看看