Java tutorial
//package com.java2s; //License from project: Open Source License public class Main { public static String formatFromSize(long size) { if (size == -1) return ""; String suffix = " B"; if (size >= 1024) { suffix = " KB"; size /= 1024; if (size >= 1024) { suffix = " MB"; size /= 1024; } } StringBuilder resultBuffer = new StringBuilder(Long.toString(size)); int commaOffset = resultBuffer.length() - 3; while (commaOffset > 0) { resultBuffer.insert(commaOffset, ','); commaOffset -= 3; } if (suffix != null) resultBuffer.append(suffix); return resultBuffer.toString(); } }