subtract two double value via BigDecimal - Java java.math

Java examples for java.math:BigDecimal Calculation

Description

subtract two double value via BigDecimal

Demo Code


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

public class Main {
    public static void main(String[] argv) throws Exception {
        double v1 = 2.45678;
        double v2 = 2.45678;
        System.out.println(sub(v1, v2));
    }// w ww .jav a  2 s  .  c  o m

    public static double sub(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.subtract(b2).doubleValue();
    }
}

Related Tutorials