1 /// <summary> 2 /// 计算文件大小函数(保留两位小数),Size为字节大小 3 /// </summary> 4 /// <param name="size">初始文件大小</param> 5 /// <returns></returns> 6 public static string GetFileSize(long size) 7 { 8 var num = 1024.00; //byte 9 10 if (size < num) 11 return size + "B"; 12 if (size < Math.Pow(num, 2)) 13 return (size / num).ToString("f2") + "K"; //kb 14 if (size < Math.Pow(num, 3)) 15 return (size / Math.Pow(num, 2)).ToString("f2") + "M"; //M 16 if (size < Math.Pow(num, 4)) 17 return (size / Math.Pow(num, 3)).ToString("f2") + "G"; //G 18 19 return (size / Math.Pow(num, 4)).ToString("f2") + "T"; //T 20 }
【原文出处】 http://www.51aras.com/?id=10