Convert Bit to MB - Java java.lang

Java examples for java.lang:int Binary

Description

Convert Bit to MB

Demo Code


import java.io.Closeable;
import java.io.IOException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main{
    public static void main(String[] argv) throws Exception{
        double d = 2.45678;
        int scale = 2;
        System.out.println(bitToMB(d,scale));
    }/* w ww  . j  a  v a 2s .co  m*/
    /**
     * Convert Bit to MB
     * 
     * @param d
     * @param scale
     * @return
     */
    public static double bitToMB(double d, int scale) {
        return CommonUtil.format(d / (1024.0 * 1024.0), scale);
    }
    /**
     * Format double value with given scale
     * 
     * @param d
     * @param scale
     * @return
     */
    public static double format(double d, int scale) {
        return new BigDecimal(d).setScale(scale, RoundingMode.HALF_UP)
                .doubleValue();
    }
}

Related Tutorials