Java tutorial
//package com.java2s; /* * Copyright (c) 2014-2015 Luis M. Gallardo D. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Lesser General Public License v3.0 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/lgpl.html * */ public class Main { public static String calculateSize(String value) { long bytes = Long.parseLong(value); int unit = 1024; if (bytes < unit) return bytes + " B"; int exp = (int) (Math.log(bytes) / Math.log(unit)); String pre = "KMGTPE".charAt(exp - 1) + "i"; return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre).replace(",", "."); } }