Convert size in byte to human readable string using Java

public static String byteToHumanReadableSize(long size) { String hrSize = ""; long b = size; double k = size / 1024.0; double m = size / 1048576.0; double g = size / 1073741824.0; double t = size / 1099511627776.0; DecimalFormat dec = new DecimalFormat("0.00"); if (t > 1){ hrSize = dec.format(t).concat("TB"); } else if (g > 1) { hrSize = dec.format(g).concat("GB"); } else if (m > 1) { hrSize = dec.format(m).concat("MB"); } else if (k > 1) { hrSize = dec.format(k).concat("KB"); } else if (b > 1) { hrSize = dec.format(b).concat("B"); } return hrSize; }
Simple snippet that converts size in byte to human readable string using Java.

#byte #size #terabyte #gigabyte #megabyte #kilobyte #DecimalFormat

#cesarnog

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.