Java tutorial
//package com.java2s; import java.text.DecimalFormat; public class Main { public static String calculateSizeToString(long size) { String str = ""; DecimalFormat df = new DecimalFormat("#.00"); if (size < 1024) { str = df.format(size) + "B"; } else if (size < 1024 * 1024) { str = df.format(size / 1024) + "KB"; } else if (size < 1024 * 1024 * 1024) { str = df.format(size / 1024 / 1024) + "M"; } else { str = df.format(size / 1024 / 1024 / 1024) + "G"; } return str; } }