网站首页 博客 C#格式化显示文件大小
C#格式化显示文件大小
 2017-07-24 10:44:51  管理员  310

函数定义

public String FormatFileSize(double fileSize)
{
    if (fileSize <= 0)
    {
        return "0 KB";
    }
    else if (fileSize >= 1024 * 1024 * 1024)
    {
        return string.Format("{0:########0.00} GB", ((Double)fileSize) / (1024 * 1024 * 1024));
    }
    else if (fileSize >= 1024 * 1024)
    {
        return string.Format("{0:####0.00} MB", ((Double)fileSize) / (1024 * 1024));
    }
    else if (fileSize >= 1024)
    {
        return string.Format("{0:####0.00} KB", ((Double)fileSize) / 1024);
    }
    else
    {
        return string.Format("{0} bytes", fileSize);
    }
}

使用方法

FormatFileSize(123456);


来说两句吧
最新评论