好的,所以我的pg类有这个问题,我应该把一个数字(代表一些字节)作为输入,并将其转换为千兆字节,兆字节,千字节和字节。做了一点研究,无法找到解决这个问题的方法。我顺便使用Java。谢谢你的建议!
以下是一个例子: // 3434901120028 - 输入 // 3199 GB + 1001 KB + 28字节 - 输出
答案 0 :(得分:1)
字节数除以1024是千字节数。千字节数除以1024是兆字节数。除以1024是千兆字节的数量。像,
long bytes = 3434901120028l;
long gb = bytes / (1024 * 1024 * 1024);
bytes -= gb * 1024 * 1024 * 1024;
long mb = bytes / (1024 * 1024);
bytes -= mb * 1024 * 1024;
long kb = bytes / 1024;
bytes -= kb * 1024;
if (gb > 0) {
System.out.printf("%d gigabytes ", gb);
}
if (mb > 0) {
System.out.printf("%d megabytes ", mb);
}
if (kb > 0) {
System.out.printf("%d kilobytes ", kb);
}
if (bytes > 0) {
System.out.printf("%d bytes ", bytes);
}
输出
3199 gigabytes 1001 kilobytes 28 bytes