Here you can find the source of bytesToString(long sizeInBytes)
public static String bytesToString(long sizeInBytes)
//package com.java2s; //License from project: Open Source License import java.text.DecimalFormat; import java.text.NumberFormat; public class Main { public static String bytesToString(long sizeInBytes) { final double SPACE_KB = 1024; final double SPACE_MB = 1024 * SPACE_KB; final double SPACE_GB = 1024 * SPACE_MB; final double SPACE_TB = 1024 * SPACE_GB; NumberFormat nf = new DecimalFormat(); nf.setMaximumFractionDigits(2);/*from w ww . ja v a2s . c o m*/ try { if (sizeInBytes < SPACE_KB) { return nf.format(sizeInBytes) + " Byte(s) (" + sizeInBytes + ")"; } else if (sizeInBytes < SPACE_MB) { return nf.format(sizeInBytes / SPACE_KB) + " KB (" + sizeInBytes + ")"; } else if (sizeInBytes < SPACE_GB) { return nf.format(sizeInBytes / SPACE_MB) + " MB (" + sizeInBytes + ")"; } else if (sizeInBytes < SPACE_TB) { return nf.format(sizeInBytes / SPACE_GB) + " GB (" + sizeInBytes + ")"; } else { return nf.format(sizeInBytes / SPACE_TB) + " TB (" + sizeInBytes + ")"; } } catch (Exception e) { return sizeInBytes + " Byte(s)"; } } }