package com.util
import java.text.DecimalFormat;
import java.math.BigDecimal;
public class DoubleUtil{
public static final double THRESHOLD=0.000001;
//按照阀值的方式比较相等
public static boolean equalsThreshold(double d1,double d2){
if(Math.abs(d1-d2)<THRESHOLD) return true;
return false;
}
//按照BigDecimal的方式比较相等
public static boolean equalsBigDecimal(double d1,double d2){
BigDecimal bigDecimal1=new BigDecimal(d1);
BigDecimal bigDecimal2=new BigDecimal(d2);
if(bigDecimal1.compareTo(bigDecimal2)==0) return true;
return false;
}
//格式化double
public static double formatDoubleDigit(double _dboule, int digit) {
StringBuffer buff = new StringBuffer("#.");
for(int i = 0; i< digit; i++){
buff.append("0");
}
DecimalFormat df = new DecimalFormat(buff.toString());
return Double.valueOf(df.format(_dboule));
}
}