码迷,mamicode.com
首页 > Windows程序 > 详细

C# 获取人类可识别的文件大小转换显示

时间:2017-06-15 18:52:53      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:byte   stat   int   returns   四舍六入   获取   转换   小数点   file   

     /// <summary>
        /// 人类可识别的文件大小显示格式
        /// </summary>
        /// <param name="size">文件大小(Byte为单位)</param>
        /// <returns></returns>
        public static string HumanReadableFileSize(double size)
        {
            string[] units = new string[] { "B", "KB", "MB", "GB", "TB", "PB" };
            double mod = 1024.0;
            int i = 0;
            while (size >= mod)
            {
                size /= mod;
                i++;
            }

            //四舍六入
            //return Math.Round(size) + units[i];

            //取小数点后一位
            return size.ToString("0.0");
        }

 

C# 获取人类可识别的文件大小转换显示

标签:byte   stat   int   returns   四舍六入   获取   转换   小数点   file   

原文地址:http://www.cnblogs.com/ChenRihe/p/CSharpHumanReadableFileSize.html

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