Here you can find the source of convertSizeToMB(long sizeInBytes)
public static String convertSizeToMB(long sizeInBytes)
//package com.java2s; /******************************************************************************* * Copyright (c) 2006 Bruno G. Braga.//www . ja va 2 s .c o m * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Bruno G. Braga - initial API and implementation *******************************************************************************/ import java.math.BigDecimal; public class Main { public static String convertSizeToMB(long sizeInBytes) { double size = sizeInBytes / 1024; String m = " Kb"; if (size > 1024) { size = size / 1024; m = " MB"; } BigDecimal bd = new BigDecimal(size); bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP); return String.valueOf(bd.doubleValue()) + m; } }