package org.gdpe.common.util;
import java.math.BigDecimal;
import java.text.DecimalFormat;
public class CalculateUtil {
private static final int DEF_DIV_SCALE = 4; //默认精确到小数点后DEF_DIV_SCALE位,须精确到其它位数的,请调用相应的传参方法
private CalculateUtil(){}
/**
* 提供精确的加法运算。
* @param oj1 被加数
* @param oj2 加数
* @return 两个参数的和
*/
public static Double add(Object oj1,Object oj2){
BigDecimal b1 = new BigDecimal(oj1.toString());
BigDecimal b2 = new BigDecimal(oj2.toString());
return b1.add(b2).doubleValue();
}
/**
* 提供精确的减法运算。
* @param oj1 被减数
* @param oj2 减数
* @return 两个参数的差
*/
public static Double subtract(Object oj1,Object oj2){
BigDecimal b1 = new BigDecimal(oj1.toString());
BigDecimal b2 = new BigDecimal(oj2.toString());
return b1.subtract(b2).doubleValue();
}
/**
* 提供精确的乘法运算。精确到小数点以后DEF_DIV_SCALE位,以后的数字四舍五入。
* @param oj1 被乘数
* @param oj2 乘数
* @return 两个参数的积
*/
public static Double multiply(Object oj1,Object oj2){
return multiply(oj1,oj2,1,DEF_DIV_SCALE);
}
/**
* 提供精确的乘法运算。精确到小数点以后scale位,以后的数字四舍五入。
* @param oj1 被乘数
* @param oj2 乘数
* @param oj3 乘数
* @param scale 表示表示需要精确到小数点以后几位。
* @return 参数的积
*/
public static Double multiply(Object oj1,Object oj2,Object oj3,int scale){
BigDecimal b1 = new BigDecimal(oj1.toString());
BigDecimal b2 = new BigDecimal(oj2.toString());
BigDecimal b3 = new BigDecimal(oj3.toString());
BigDecimal one = new BigDecimal("1");
return (b1.multiply(b2)).multiply(b3).divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}
/**
* 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到
* 小数点以后DEF_DIV_SCALE位,以后的数字四舍五入。
* @param oj1 被除数
* @param oj2 除数
* @return 两个参数的商
*/
public static Double divide(Object oj1,Object oj2){
return divide(oj1,oj2,DEF_DIV_SCALE);
}
/**
* 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指
* 定精度,以后的数字四舍五入。
* @param oj1 被除数
* @param oj2 除数
* @param scale 表示表示需要精确到小数点以后几位。
* @return 两个参数的商
*/
public static Double divide(Object oj1,Object oj2,int scale){
if(scale<0){
throw new IllegalArgumentException("The scale must be a positive integer or zero");
}
BigDecimal b1 = new BigDecimal(oj1.toString());
BigDecimal b2 = new BigDecimal(oj2.toString());
return b1.divide(b2,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
}
/**
* 提供精确的小数位四舍五入处理。
* @param v 需要四舍五入的数字
* @param scale 小数点后保留几位
* @return 四舍五入后的结果
*/
public static Double round(Object v,int scale){
if(scale<0){
throw new IllegalArgumentException("The scale must be a positive integer or zero");
}
BigDecimal b = new BigDecimal(v.toString());
BigDecimal one = new BigDecimal("1");
return b.divide(one,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
}
/**格式化数字########0.0#
* @param v
* @return
*/
public static String numberFormat2(Object v){
Double d=round(v,2);
DecimalFormat numformat = new DecimalFormat("########0.0#");
return numformat.format(d);
}
/**格式化数字为########0.0##
* @param v
* @return
*/
public static String numberFormat3(Object v){
Double d=round(v,3);
DecimalFormat numformat = new DecimalFormat("########0.0##");
return numformat.format(d);
}
/**格式化数字为########0.0###
* @param v
* @return
*/
public static String numberFormat4(Object v){
Double d=round(v,4);
DecimalFormat numformat = new DecimalFormat("########0.0###");
return numformat.format(d);
}
public static void main(String[] args) {
System.out.println(CalculateUtil.round(0.9950000000000001, 4));
}
}
import java.math.BigDecimal;
import java.text.DecimalFormat;
public class CalculateUtil {
private static final int DEF_DIV_SCALE = 4; //默认精确到小数点后DEF_DIV_SCALE位,须精确到其它位数的,请调用相应的传参方法
private CalculateUtil(){}
/**
* 提供精确的加法运算。
* @param oj1 被加数
* @param oj2 加数
* @return 两个参数的和
*/
public static Double add(Object oj1,Object oj2){
BigDecimal b1 = new BigDecimal(oj1.toString());
BigDecimal b2 = new BigDecimal(oj2.toString());
return b1.add(b2).doubleValue();
}
/**
* 提供精确的减法运算。
* @param oj1 被减数
* @param oj2 减数
* @return 两个参数的差
*/
public static Double subtract(Object oj1,Object oj2){
BigDecimal b1 = new BigDecimal(oj1.toString());
BigDecimal b2 = new BigDecimal(oj2.toString());
return b1.subtract(b2).doubleValue();
}
/**
* 提供精确的乘法运算。精确到小数点以后DEF_DIV_SCALE位,以后的数字四舍五入。
* @param oj1 被乘数
* @param oj2 乘数
* @return 两个参数的积
*/
public static Double multiply(Object oj1,Object oj2){
return multiply(oj1,oj2,1,DEF_DIV_SCALE);
}
/**
* 提供精确的乘法运算。精确到小数点以后scale位,以后的数字四舍五入。
* @param oj1 被乘数
* @param oj2 乘数
* @param oj3 乘数
* @param scale 表示表示需要精确到小数点以后几位。
* @return 参数的积
*/
public static Double multiply(Object oj1,Object oj2,Object oj3,int scale){
BigDecimal b1 = new BigDecimal(oj1.toString());
BigDecimal b2 = new BigDecimal(oj2.toString());
BigDecimal b3 = new BigDecimal(oj3.toString());
BigDecimal one = new BigDecimal("1");
return (b1.multiply(b2)).multiply(b3).divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}
/**
* 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到
* 小数点以后DEF_DIV_SCALE位,以后的数字四舍五入。
* @param oj1 被除数
* @param oj2 除数
* @return 两个参数的商
*/
public static Double divide(Object oj1,Object oj2){
return divide(oj1,oj2,DEF_DIV_SCALE);
}
/**
* 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指
* 定精度,以后的数字四舍五入。
* @param oj1 被除数
* @param oj2 除数
* @param scale 表示表示需要精确到小数点以后几位。
* @return 两个参数的商
*/
public static Double divide(Object oj1,Object oj2,int scale){
if(scale<0){
throw new IllegalArgumentException("The scale must be a positive integer or zero");
}
BigDecimal b1 = new BigDecimal(oj1.toString());
BigDecimal b2 = new BigDecimal(oj2.toString());
return b1.divide(b2,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
}
/**
* 提供精确的小数位四舍五入处理。
* @param v 需要四舍五入的数字
* @param scale 小数点后保留几位
* @return 四舍五入后的结果
*/
public static Double round(Object v,int scale){
if(scale<0){
throw new IllegalArgumentException("The scale must be a positive integer or zero");
}
BigDecimal b = new BigDecimal(v.toString());
BigDecimal one = new BigDecimal("1");
return b.divide(one,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
}
/**格式化数字########0.0#
* @param v
* @return
*/
public static String numberFormat2(Object v){
Double d=round(v,2);
DecimalFormat numformat = new DecimalFormat("########0.0#");
return numformat.format(d);
}
/**格式化数字为########0.0##
* @param v
* @return
*/
public static String numberFormat3(Object v){
Double d=round(v,3);
DecimalFormat numformat = new DecimalFormat("########0.0##");
return numformat.format(d);
}
/**格式化数字为########0.0###
* @param v
* @return
*/
public static String numberFormat4(Object v){
Double d=round(v,4);
DecimalFormat numformat = new DecimalFormat("########0.0###");
return numformat.format(d);
}
public static void main(String[] args) {
System.out.println(CalculateUtil.round(0.9950000000000001, 4));
}
}