subtract two double - Java java.lang

Java examples for java.lang:double

Description

subtract two double

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;
        System.out.println(sub(d1, d2));
    }//  w ww.  ja v  a2  s  . c  o m

    public static double sub(double d1, double d2) {
        BigDecimal bd1 = new BigDecimal(Double.toString(d1));
        BigDecimal bd2 = new BigDecimal(Double.toString(d2));
        return bd1.subtract(bd2).doubleValue();
    }
}

Related Tutorials