Here you can find the source of format(long mem)
private static String format(long mem)
//package com.java2s; //License from project: Apache License public class Main { private static String format(long mem) { double dMem = mem; String[] units = new String[] { "", "KB", "MB", "GB" }; for (String unit : units) { if (dMem < 1024) return String.format("%.1f %s", dMem, unit); dMem /= 1024;//from w w w . ja v a2 s . co m } return String.format("%.1f %s", dMem * 1024, units[units.length - 1]); } }