Here you can find the source of getSizeText(long size)
public static String getSizeText(long size)
//package com.java2s; /*/*from w ww .j av a 2 s . c o m*/ * Copyright 2014 Alibaba.com All right reserved. This software is the * confidential and proprietary information of Alibaba.com ("Confidential * Information"). You shall not disclose such Confidential Information and shall * use it only in accordance with the terms of the license agreement you entered * into with Alibaba.com. */ import java.text.DecimalFormat; public class Main { public static String getSizeText(long size) { DecimalFormat df = new DecimalFormat("0.00"); if (size >= 0L && size < 1024) { return size + "B"; } else if (size >= 1024 && size < 1048576) { double fileSize = (double) size / 1024; return df.format(fileSize) + "KB"; } else { double fileSize = (double) size / 1048576; return df.format(fileSize) + "MB"; } } }