Java tutorial
//package com.java2s; //License from project: Open Source License import java.text.DecimalFormat; public class Main { public static String getFriendlyFileSize(long fileSize) { DecimalFormat df = new DecimalFormat("#.00"); String fileSizeString = ""; if (fileSize < 1024) { fileSizeString = df.format((double) fileSize) + "B"; } else if (fileSize < 1048576) { fileSizeString = df.format((double) fileSize / 1024) + "KB"; } else if (fileSize < 1073741824) { fileSizeString = df.format((double) fileSize / 1048576) + "MB"; } else { fileSizeString = df.format((double) fileSize / 1073741824) + "GB"; } return fileSizeString; } }