Java tutorial
//package com.java2s; public class Main { public static String longSizeToStr(long contentLength) { float length = contentLength; String strLen; if (length >= 1024 * 1024 * 1024) { length /= 1024 * 1024 * 1024; String s = String.valueOf(length); strLen = s.substring(0, s.length() > 4 ? 4 : s.length()) + "GB"; while (strLen.length() < 6) { strLen = strLen.replace("GB", "0GB"); } } else if (length >= 1024 * 1024) { length /= 1024 * 1024; String s = String.valueOf(length); strLen = s.substring(0, s.length() > 4 ? 4 : s.length()) + "MB"; while (strLen.length() < 6) { strLen = strLen.replace("MB", "0MB"); } } else if (length >= 1024) { length /= 1024; String s = String.valueOf(length); strLen = s.substring(0, s.length() > 4 ? 4 : s.length()) + "KB"; while (strLen.length() < 6) { strLen = strLen.replace("KB", "0KB"); } } else { strLen = (int) (length) + "B"; } for (int i = 0; i < strLen.length(); i++) { if (strLen.charAt(i) == '.' && i == strLen.length() - 3) { strLen = strLen.substring(0, i) + strLen.substring(i + 1); break; } } return strLen; } }