add two double value via BigDecimal - Java java.math

Java examples for java.math:BigDecimal Calculation

Description

add 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(add(v1, v2));
    }//  w  w w.  j  a v  a 2  s  .com

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

Related Tutorials