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).doublevalue(); } /** * 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到 * 小数点以后10位,以后的数字四舍五入。 * @param v1 被除数 * @param v2 除数 * @return 两个参数的商 */ public static double div(double v1,double v2){ return div(v1,v2,def_div_scale); } /** * 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指 * 定精度,以后的数字四舍五入。 * @param v1 被除数 * @param v2 除数 * @param scale 表示表示需要精确到小数点以后几位。 * @return 两个参数的商 */ 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(); } /** * 提供精确的小数位四舍五入处理。 * @param v 需要四舍五入的数字 * @param scale 小数点后保留几位 * @return 四舍五入后的结果 */ 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).doublevalue(); } } |