View Code
 1  /// <summary>
 2         /// 格式化文件大小
 3         /// </summary>
 4         /// <param name="size">大小</param>
 5         /// <returns>式化大小</returns>
 6         public string FormatSize(long size)
 7         {
 8             try
 9             {
10                 string strReturn = "";
11                 double tempSize = Math.Abs(size);
12                 if (tempSize < 1024)
13                 {
14                     strReturn += tempSize.ToString() + "B";
15                 }
16                 else if (tempSize < 1024 * 1024)
17                 {
18                     tempSize = tempSize / 1024;
19                     strReturn += tempSize.ToString("0.##") + "K";
20                 }
21                 else if (tempSize < 1024 * 1024 * 1024)
22                 {
23                     tempSize = tempSize / 1024 / 1024;
24                     strReturn += tempSize.ToString("0.##") + "M";
25                 }
26                 else
27                 {
28                     tempSize = tempSize / 1024 / 1024 / 1024;
29                     strReturn += tempSize.ToString("0.##") + "G";
30                 }
31 
32                 if (size < 0)
33                 {
34                     strReturn = "-" + strReturn;
35                 }
36 
37                 return strReturn;
38             }
39             catch (Exception e)
40             {
41                 throw e;
42             }
43             
44         }

 

posted on 2012-07-12 15:16  捣乃忒  阅读(260)  评论(0)    收藏  举报