代码片段-计算文件字节大小,以合适的单位显示

 1     /// <summary>
 2     /// 返回文件体积大小描述
 3     /// </summary>
 4     /// <param name="bytesCount">字节数量</param>
 5     /// <returns></returns>
 6     private string Calc(long bytesCount)
 7     {
 8       var units = new[] { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", "BB" };
 9 
10       int index = 0;
11       double k = bytesCount;
12 
13       if (bytesCount >= 1024)
14       {
15         while (true)
16         {
17           k = k / 1024;
18 
19           index++;
20           if (k < 1024) break;
21         }
22       }
23 
24       return $"{k:f2}{units[index]}";
25     }
26   }

 

上一篇:postcss.config.js 配置(vue移动端自适应)


下一篇:[FE] Canvas 转图片并下载的方式