我从这个例子中找到了这段代码:
private static final String[] Q = new String[]{"", "K", "M", "G", "T", "P", "E"};
public String getAsString(long bytes)
{
for (int i = 6; i > 0; i--)
{
double step = Math.pow(1024, i);
if (bytes > step) return String.format("%3.1f %s", bytes / step, Q[i]);
}
return Long.toString(bytes);
}
我实现了这段代码,但结果看起来很可疑。我试图从 JVM 获得多少可用内存,但我只得到 80 MB。您能否确认此代码正确地将字节转换为兆字节或千兆字节。