Java tutorial
//package com.java2s; //License from project: Apache License import java.text.DecimalFormat; public class Main { public static String formatFileSizeToString(long fileLen) { DecimalFormat df = new DecimalFormat("#.00"); String fileSizeString = ""; if (fileLen < 1024) { fileSizeString = df.format((double) fileLen) + "B"; } else if (fileLen < 1048576) { fileSizeString = df.format((double) fileLen / 1024) + "K"; } else if (fileLen < 1073741824) { fileSizeString = df.format((double) fileLen / 1048576) + "M"; } else { fileSizeString = df.format((double) fileLen / 1073741824) + "G"; } return fileSizeString; } }