Here you can find the source of convertSize(Long size)
public static String convertSize(Long size)
//package com.java2s; //License from project: Open Source License import java.math.BigDecimal; public class Main { public static String convertSize(Long size) { String retStr = ""; for (int i = 3; i > 0; i--) { double compSize = Math.pow(1024, i); double remainder = size / compSize; if (remainder > 1) { if (i == 3) { retStr = round(remainder, 2, BigDecimal.ROUND_HALF_EVEN) + "G"; break; } else if (i == 2) { retStr = round(remainder, 2, BigDecimal.ROUND_HALF_EVEN) + "M"; break; } else { retStr = round(remainder, 2, BigDecimal.ROUND_HALF_EVEN) + "K"; break; }// w ww.j a v a2 s.c om } retStr = round(remainder, 2, BigDecimal.ROUND_HALF_EVEN) + "K"; } return retStr; } public static double round(double value, int scale, int roundingMode) { BigDecimal bd = new BigDecimal(value); bd = bd.setScale(scale, roundingMode); double retValue = bd.doubleValue(); bd = null; return retValue; } }