List of utility methods to do Byte Value Format
float | bytesToGB(long bytes) bytes To GB return (float) bytes / ONE_GB; |
String | bytesToHuman(long bytes) Converts a given number of bytes to a human readable format. if (bytes < 1024) { return bytes + " B"; int bitsUsed = 63 - Long.numberOfLeadingZeros(bytes); double significand = (double) bytes / (1L << (bitsUsed - bitsUsed % 10)); char prefix = "kMGTPE".charAt(bitsUsed / 10 - 1); return String.format("%.1f %sB", significand, prefix); |
String | bytesToHuman(long size) bytes To Human if (size < 1024L) { return size + "bytes"; } else if (size < 1024L * 1024L) { return String.format("%.2fkb", (double) size / 1024L); } else if (size < 1024L * 1024L * 1024L) { return String.format("%.2fmb", (double) size / (1024L * 1024L)); } else if (size < 1024L * 1024L * 1024L * 1024L) { return String.format("%.2fgb", (double) size / (1024L * 1024L * 1024L)); ... |
String | bytesToHumanReadable(int bytes) bytes To Human Readable float fbytes = (float) bytes; String[] mags = new String[] { "", "k", "m", "g", "t" }; int magIndex = 0; while (fbytes >= 1024) { fbytes /= 1024; magIndex++; return String.format("%.2f%sb", fbytes, mags[magIndex]); ... |
long | bytesToKBytes(long bytes) bytes To K Bytes long kBytes = bytes / 1024; long remainder = bytes % 1024; if (remainder > 0) kBytes += 1; return kBytes; |
int | bytesToMagaBytes(long bytes) bytes To Maga Bytes return (int) (((float) bytes) / 1024.0f / 1024.0f); |
long | BytesToMB(long kb) Yes, I'm that lazy return KB(kb) / 1024;
|
long | bytesToMB(long sizeInBytes) bytes To MB return sizeInBytes / (1024 * 1024);
|
String | bytesToMBString(long bytes) bytes To MB String return bytesToMB(bytes) + " MB"; |
long | bytesToMegaBytes(long bytes) Convert bytes to MegaBytes return bytes / MEGABYTE;
|