Convert Bit to KB - Java java.lang

Java examples for java.lang:int Binary

Description

Convert Bit to KB

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(bitToKB(d,scale));
    }//from  ww w.  j  ava  2  s  .c o m
    /**
     * Convert Bit to KB
     * 
     * @param d
     * @param scale
     * @return
     */
    public static double bitToKB(double d, int scale) {
        return CommonUtil.format(d / 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