我发现了该程序的示例,该程序将转换输入并根据该值打印输出。但是我注意到,如果我增加更大的价值,我会得到错误

integer number too large: 9394343298


有没有什么办法解决这一问题?

String readableFileSize = readableFileSize(939343298);

System.out.println("Result " + readableFileSize);

public static String readableFileSize(long size)
{
    if (size <= 0)
        return "0";
    final String[] units = new String[] {"B", "kB", "MB", "GB", "TB"};
    int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
    return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
}

最佳答案

只需在数字末尾添加L即可将其标记为长值:

String readableFileSize = readableFileSize(9394343298L);

关于java - 增加转换大小,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29692472/

10-09 01:55