Here you can find the source of toFileSize(final long longSize, final int decimalPos)
public static String toFileSize(final long longSize, final int decimalPos)
//package com.java2s; //License from project: Open Source License import java.text.NumberFormat; public class Main { public static String toFileSize(final long longSize, final int decimalPos) { final NumberFormat fmt = NumberFormat.getNumberInstance(); if (decimalPos >= 0) { fmt.setMaximumFractionDigits(decimalPos); }//from w w w . j a v a 2 s .co m final double size = longSize; double val = size / (1024 * 1024); if (val > 1) { return fmt.format(val).concat(" MB"); } val = size / 1024; if (val > 10) { return fmt.format(val).concat(" KB"); } return fmt.format(val).concat(" bytes"); } }