Java tutorial
//package com.java2s; import java.text.DecimalFormat; public class Main { public static String converSizeToString(long size) { String ext[] = { "B", "KB", "MB", "G", "T", "P" }; int i = 0; long duration = size; while (duration >= 1024) { System.out.println("duration=" + duration + " i=" + i); duration = duration >> 10; i++; System.out.println("duration=" + duration + " i=" + i); } double d = Math.pow(2, 10 * (i)); java.text.DecimalFormat df = new DecimalFormat(); df.setMaximumFractionDigits(2); df.setMinimumFractionDigits(2); return df.format(size / d) + ext[i]; } }