Here you can find the source of getFormatSize(double size)
private static String getFormatSize(double size)
//package com.java2s; //License from project: Apache License import java.text.NumberFormat; public class Main { public static final String[] FILE_SIZE_UNIT = { "Byte", "KB", "MB", "GB", "TB", "PB" }; private static String getFormatSize(double size) { NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(2);// w w w . j av a 2s. c om int i = 0; String[] unit = FILE_SIZE_UNIT; for (i = 0; i < unit.length; i++) { if ((long) (size / 1024) > 0) { size /= 1024; } else { break; } } return nf.format(size) + unit[i]; } }