duble计算

来源:互联网 发布:58 程序员兼职 编辑:程序博客网 时间:2024/06/02 07:09
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 用于duble计算
 * 
 * @Time 2017年5月24日 下午3:40:51
 */
public class ArithUtil {


private static final Logger logger = LoggerFactory.getLogger(ArithUtil.class);


/**
* 兩個double相加

* @author 01368435
* @Time 2017年5月24日 下午3:41:20
* @param v1
* @param v2
* @return
*/
public static Double add(Double v1, Double v2) {
try {
BigDecimal b1 = new BigDecimal(v1.toString());
BigDecimal b2 = new BigDecimal(v2.toString());
return new Double(b1.add(b2).doubleValue());
} catch (Exception e) {
logger.error("add faild.s1:" + v1 + ",s2:" + v2 + ",Exception:{}", e);
}
return 0d;
}


public static double add(String s1, String s2) {
try {
BigDecimal b1 = new BigDecimal(s1);
BigDecimal b2 = new BigDecimal(s2);
return new Double(b1.add(b2).doubleValue());
} catch (Exception e) {
logger.error("add faild.s1:" + s1 + ",s2:" + s2 + ",Exception:{}", e);
}
return 0d;
}


}