分类: Java
2010-08-23 13:10:48
import java.math.BigDecimal;
public class Arith {
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).doubleval_rue();
}
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).doubleval_rue();
}
public static double mul(double v1,double v2){
BigDecimal b1=new BigDecimal(Double.toString(v1));
BigDecimal b2=new BigDecimal(Double.toString(v2));
return b1.multiply(b2).doubleval_rue();
}
public static double div(double v1,double v2){
BigDecimal b1=new BigDecimal(Double.toString(v1));
BigDecimal b2=new BigDecimal(Double.toString(v2));
return b1.divide(b2,10,BigDecimal.ROUND_HALF_UP).doubleval_rue();
}
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).doubleval_rue();
}
public static double round(double v,int scale){
if(scale<0){
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b=new BigDecimal(Double.toString(v));
BigDecimal one=new BigDecimal(1);
return b.divide(one,scale,BigDecimal.ROUND_HALF_UP).doubleval_rue();
}
public static void main(String [] args){
Arith arith=new Arith();
System.out.println(arith.div(13,3));
}
}