网站首页 博客 C#格式化显示文件大小
函数定义
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);