C# 内存单位计算 源码

来源:互联网 发布:下周主要财经数据 编辑:程序博客网 时间:2024/05/28 18:45

知识备忘

public static string FormatBytesSize(long bytes)

{
int unit = 1024;
int exp = (int)Math.Log(bytes,unit);
return String.Format("{0:F2} {1}", bytes / Math.Pow(unit, exp), new string[] { "B", "KB", "MB", "GB", "TB", "PB", "EB" }[exp]);

}

string unit = FormatBytesSize(1024);// unit = 1.00 KB

string unit = FormatBytesSize(1024 * 1024);// unit = 1.00 MB

string unit = FormatBytesSize(1024 * 1024 * 1024);// unit = 1.00 GB

原创粉丝点击