divide two double with scale - Java java.lang

Java examples for java.lang:double

Description

divide two double with scale

Demo Code


//package com.java2s;
import java.math.BigDecimal;

public class Main {
    public static void main(String[] argv) throws Exception {
        double d1 = 2.45678;
        double d2 = 2.45678;
        int scale = 2;
        System.out.println(div(d1, d2, scale));
    }//from w ww.ja v  a  2  s  .  co m

    public static double div(double d1, double d2, int scale) {


        BigDecimal bd1 = new BigDecimal(Double.toString(d1));
        BigDecimal bd2 = new BigDecimal(Double.toString(d2));
        return bd1.divide(bd2, scale, BigDecimal.ROUND_HALF_UP)
                .doubleValue();
    }
}

Related Tutorials