日期:2014-05-20 浏览次数:20826 次
import java.math.BigDecimal;
public class TestDouble {
public static void main(String[] args) {
double d1 = div(20130708154752d,1000000000000d);
double d2 = 16000d;
System.out.println(d1);
System.out.println(sum(d1,d2));
}
public static double sum(double d1,double d2){
BigDecimal bd1 = new BigDecimal(Double.toString(d1));
BigDecimal bd2 = new BigDecimal(Double.toString(d2));
return bd1.add(bd2).doubleValue();
}
public static double div(double v1,double v2){
return div(v1,v2,12);
}
public static double div(double v1,double v2,int scale){
if(scale<0){
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.divide(b2,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
}
}