快速谷歌搜索返回我这个从Appache
Hadoop项目。从那里复制:(Apache许可证,版本2.0):
private static DecimalFormat oneDecimal = new DecimalFormat("0.0"); public static String humanReadableInt(long number) { long absNumber = Math.abs(number); double result = number; String suffix = ""; if (absNumber < 1024) { // nothing } else if (absNumber < 1024 * 1024) { result = number / 1024.0; suffix = "k"; } else if (absNumber < 1024 * 1024 * 1024) { result = number / (1024.0 * 1024); suffix = "m"; } else { result = number / (1024.0 * 1024 * 1024); suffix = "g"; } return oneDecimal.format(result) + suffix; }它使用1K = 1024,但是您可以根据需要进行调整。您还需要使用其他DecimalFormat处理<1024情况。



