Java Utililty Methods Byte Value Format

List of utility methods to do Byte Value Format

Description

The list of methods to do Byte Value Format are organized into topic(s).

Method

floatbytesToGB(long bytes)
bytes To GB
return (float) bytes / ONE_GB;
StringbytesToHuman(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);
StringbytesToHuman(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));
...
StringbytesToHumanReadable(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]);
...
longbytesToKBytes(long bytes)
bytes To K Bytes
long kBytes = bytes / 1024;
long remainder = bytes % 1024;
if (remainder > 0)
    kBytes += 1;
return kBytes;
intbytesToMagaBytes(long bytes)
bytes To Maga Bytes
return (int) (((float) bytes) / 1024.0f / 1024.0f);
longBytesToMB(long kb)
Yes, I'm that lazy
return KB(kb) / 1024;
longbytesToMB(long sizeInBytes)
bytes To MB
return sizeInBytes / (1024 * 1024);
StringbytesToMBString(long bytes)
bytes To MB String
return bytesToMB(bytes) + " MB";
longbytesToMegaBytes(long bytes)
Convert bytes to MegaBytes
return bytes / MEGABYTE;