Java tutorial
//package com.java2s; import java.text.DecimalFormat; public class Main { private static final long KILO = 1000; private static final long MEGA = KILO * 1000; private static final long GIGA = MEGA * 1000; private static final DecimalFormat mFmt = new DecimalFormat("###,###.##"); public static String formatSize(long fileSize) { if (fileSize > GIGA) { return mFmt.format((double) fileSize / GIGA) + "GB"; } else if (fileSize > MEGA) { return mFmt.format((double) fileSize / MEGA) + "MB"; } else if (fileSize > KILO) { return mFmt.format((double) fileSize / KILO) + "KB"; } else { return Long.toString(fileSize) + "B"; } } }