C# 常用公共方法

/// <summary>
/// 文件大小单位格式(GB/MB/KB/B)
/// </summary>
/// <param name="b"></param>
/// <returns></returns>
public static string FileUnitToString(long b)
{
    const double GB = 1024 * 1024 * 1024;
    const double MB = 1024 * 1024;
    const double KB = 1024;

    if (b / GB >= 1)
    {
        return Math.Round(b / (double)GB, 1) + "GB";
    }

    if (b / MB >= 1)
    {
        return Math.Round(b / (double)MB, 1) + "MB";
    }

    if (b / KB >= 1)
    {
        return Math.Round(b / (double)KB, 1) + "KB";
    }

    return b + "B";
}

  

posted @ 2021-05-08 09:57  microsoft-zhcn  阅读(86)  评论(0编辑  收藏  举报