Java tutorial
//package com.java2s; //License from project: Apache License public class Main { public static String getSize(long size) { if (size < 0) return null; String result = null; if (size > 1024 * 1024 * 1024) { float f = (float) size / (1024 * 1024 * 1024); String s = String.valueOf(f); if (s.length() - s.indexOf(".") > 2) result = s.substring(0, s.indexOf(".") + 3); else result = s; return result + "GB"; } else if (size > 1024 * 1024) { float f = (float) size / (1024 * 1024); String s = String.valueOf(f); if (s.length() - s.indexOf(".") > 2) result = s.substring(0, s.indexOf(".") + 3); else result = s; return result + "MB"; } else if (size > 1024) { float f = (float) size / 1024; String s = String.valueOf(f); if (s.length() - s.indexOf(".") > 2) result = s.substring(0, s.indexOf(".") + 3); else result = s; return result + "KB"; } else if (size < 1024) { return String.valueOf(size) + "B"; } else return null; } }