字节数,转换成人类可读性强的字符串的方法
Size byte to human readable string
Java版:
public static String humanReadableByteCount(long bytes, boolean si){
int unit = si ? 1000 : 1024;
if (bytes < unit) return bytes + " B";
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "i" : "");
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
}
C# 版:
public static string SizeToReadable(Int64 bytes, bool si = false)
{
int unit = si ? 1000 : 1024;
if (bytes < unit) return bytes + " B";
int exp = (int)(Math.Log(bytes) / Math.Log(unit));
String pre = (si ? "kMGTPE" : "KMGTPE")[exp - 1] + (si ? "i" : "");
return String.Format("{0:0.00} {1}B", bytes / Math.Pow(unit, exp), pre);
}