Here you can find the source of readableSize(long size, DecimalFormat format)
public static String readableSize(long size, DecimalFormat format)
//package com.java2s; /*//from w w w . j av a2 s . c o m * Copyright (c) 2011 - 2012. Elega9t Ltd. All rights reserved. * ELEGA9T PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.Copyright (c) 2011 - 2012. Elega9t Ltd. All rights reserved. */ import java.text.DecimalFormat; public class Main { private static final DecimalFormat FILE_SIZE_FORMAT = new DecimalFormat("#,##0.#"); public static String readableSize(long size, DecimalFormat format) { if (size <= 0) return "0"; final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" }; int digitGroups = (int) (Math.log10(size) / Math.log10(1024)); return format.format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups]; } public static String readableSize(long size) { return readableSize(size, FILE_SIZE_FORMAT); } }