public class DoublePlusDouble { 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(); } public static double addList(double d1,double d2){ List<Double> list =new ArrayList<Double>(); list.add(d1); list.add(d2); double init = 0; BigDecimal initB = new BigDecimal(Double.toString(init)); for(int i=0,len=list.size() ; i<len ; i++){ BigDecimal param =new BigDecimal(Double.toString(list.get(i))); initB= initB.add(param); } init = Double.parseDouble(initB.toString()); return init; } public static void main(String[] args) { double param1 = 0.0001; double param2 = 13.2351; System.out.println(add(param1, param2)); System.out.println(addList(param1, param2)); } }
时间: 2024-10-19 13:03:23