C# 字节转换成"B", "KB", "MB", "GB", "TB", "PB"实例
/// <summary>
/// 转换方法
/// </summary>
/// <param name="size">字节值</param>
/// <returns></returns>
private String HumanReadableFilesize(double size)
{
String[] units = new String[] { "B", "KB", "MB", "GB", "TB", "PB" };
double mod = 1024.0;
int i = 0;
while (size >= mod)
{
size /= mod;
i++;
}
return Math.Round(size) + units[i];
}
此处参考:C# 字节转换成"B", "KB", "MB", "GB", "TB", "PB"实例
注:此处并没有将所得的值进行四舍五入,而是直接取整数了;
补充代码:
return Math.Round(size,2) + units[i];