Here you can find the source of getSizeString(long size)
public static String getSizeString(long size)
//package com.java2s; //License from project: Apache License import java.text.DecimalFormat; public class Main { private static final long KSIZE = 1024L; private static final long MSIZE = KSIZE * 1024L; private static final long GSIZE = MSIZE * 1024L; private static final long TSIZE = GSIZE * 1024L; public static String getSizeString(long size) { if (size < MSIZE) { DecimalFormat format = new DecimalFormat("0.0KB"); return format.format((double) size / KSIZE); }//from w ww. j a v a 2 s . co m if (size < GSIZE) { DecimalFormat format = new DecimalFormat("0.0MB"); return format.format((double) size / MSIZE); } if (size < TSIZE) { DecimalFormat format = new DecimalFormat("0.0GB"); return format.format((double) size / GSIZE); } DecimalFormat format = new DecimalFormat("0.0TB"); return format.format((double) size / TSIZE); } }