Here you can find the source of getDataSize(long size)
public static String getDataSize(long size)
//package com.java2s; //License from project: Apache License import java.text.DecimalFormat; public class Main { private static DecimalFormat formater = new DecimalFormat("#,##0.00"); public static String getDataSize(long size) { if (size < 1024) { return size + "bytes"; } else if (size < 1024L * 1024) { double kbsize = size / 1024D; return formater.format(kbsize) + "KB"; } else if (size < 1024L * 1024L * 1024) { double mbsize = size / 1024D / 1024D; return formater.format(mbsize) + "MB"; } else if (size < 1024L * 1024L * 1024L * 1024) { double gbsize = size / 1024D / 1024D / 1024D; return formater.format(gbsize) + "GB"; } else {/*from w w w . j a va 2 s . co m*/ double tbsize = size / 1024D / 1024D / 1024D / 1024D; return formater.format(tbsize) + "TB"; } } }