好吧,我的 pg 类遇到了这个问题,我应该将一个数字(代表字节数)作为输入并将其转换为千兆字节、兆字节、千字节和字节。做了一些研究,找不到解决这个问题的方法。顺便说一句,我正在使用 Java。谢谢指教!
Here is an example:
Input : 3434901120028
Output : 3199 GB + 1001 KB + 28 bytes
最佳答案
字节数除以 1024 就是千字节数。千字节数除以 1024 就是兆字节数。除以 1024 就是 GB 数。比如,
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
关于java - gb mb kb 和 X 字节中的字节数,java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26563272/