Java Number Convert convertSizeToMB(long sizeInBytes)

Here you can find the source of convertSizeToMB(long sizeInBytes)

Description

convert Size To MB

License

Open Source License

Declaration

public static String convertSizeToMB(long sizeInBytes) 

Method Source Code

//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;
    }
}

Related

  1. convertNanoDiff(long startNanos, long stopNanos, TimeUnit timeUnit)
  2. convertNullToOne(final Object value)
  3. convertScientificToStandard(double value)
  4. convertSize(Long size)
  5. convertSizeG1DetailsToSizeG1(final String size, final char units)
  6. convertsToLong(double v)
  7. convertStringToObjectWrapper(String className, String value)
  8. convertToDecimal(Number n)
  9. convertToHours(long workingDuration)