C#/.NET不使用判断语句实现Bytes到KB,MB,GB,TB单位智能转换的静态扩展方法

public static string ConvertBytes(this long len)
{
      string[] sizes = { "Bytes", "KB", "MB", "GB", "TB" };
      int order = 0;
      while (len >= 1024 && order + 1 < sizes.Length)
      {
        order++;
        len = len / 1024;
      }
      return String.Format("{0:0.##} {1}", len, sizes[order]);
}

原文地址:https://codedefault.com/p/csharp-dot-net-dont-use-if-or-switch-to-convert-bytes-to-kb-mb-gb-tb


猜你喜欢

转载自blog.csdn.net/weixin_42930928/article/details/84322929