Java examples for java.lang:double
Provide accurate addition among two double values
//package com.java2s; 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)); }/*from w w w .j ava2 s. co m*/ /** * Provide accurate addition. * * @param v1 * @param v2 * @return */ public static double add(double v1, double v2) { java.math.BigDecimal b1 = new java.math.BigDecimal( Double.toString(v1)); java.math.BigDecimal b2 = new java.math.BigDecimal( Double.toString(v2)); return b1.add(b2).doubleValue(); } /** * Provide accurate addition. * * @param v1 * @param v2 * @return */ public static double add(String v1, String v2) { java.math.BigDecimal b1 = new java.math.BigDecimal(v1); java.math.BigDecimal b2 = new java.math.BigDecimal(v2); return b1.add(b2).doubleValue(); } }