Here you can find the source of getDisplayable(long numBytes)
public static String getDisplayable(long numBytes)
//package com.java2s; import java.util.Formatter; public class Main { private static final int BYTES_SCALING_FACTOR = 1024; public static String getDisplayable(long numBytes) { if (numBytes < BYTES_SCALING_FACTOR) { return numBytes + " bytes"; }//from w ww .j ava 2s .c o m String label; double size = ((double) numBytes) / BYTES_SCALING_FACTOR; if (size < BYTES_SCALING_FACTOR) { label = "Kb"; } else { size = size / BYTES_SCALING_FACTOR; if (size < BYTES_SCALING_FACTOR) { label = "Mb"; } else { size = size / BYTES_SCALING_FACTOR; label = "Gb"; } } StringBuilder sb = new StringBuilder(); Formatter formatter = new Formatter(sb); formatter.format("%.2f %s", size, label); return sb.toString(); } }