Java tutorial
//package com.java2s; import java.text.DecimalFormat; public class Main { public static String setFileSize(long size) { DecimalFormat df = new DecimalFormat("###.##"); float f = ((float) size / (float) (1024 * 1024)); if (f < 1.0) { float f2 = ((float) size / (float) (1024)); return df.format(new Float(f2).doubleValue()) + "KB"; } else { return df.format(new Float(f).doubleValue()) + "M"; } } }