Here you can find the source of bytesToUnits(long size)
public static String bytesToUnits(long size)
//package com.java2s; //License from project: Apache License import java.text.DecimalFormat; public class Main { public static String bytesToUnits(long size) { String hrSize = null;/*from www . j a v a 2 s . c o m*/ double b = size; double k = size / 1024.0; double m = ((size / 1024.0) / 1024.0); double g = (((size / 1024.0) / 1024.0) / 1024.0); double t = ((((size / 1024.0) / 1024.0) / 1024.0) / 1024.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 { hrSize = dec.format(b).concat(" Bytes"); } return hrSize; } }