工具类篇【二】BigDecimal计算_bigdecimal计算工具类

3 阅读1分钟

工具��篇大全

工具类篇【一】String字符串

工具类篇【二】BigDecimal计算

工具类篇【三】日期Date转换

工具类篇【四】日志脱敏

工具类篇【五】Random随机生成字符串

工具类篇【六】克隆对象的2种常用方法

 

前言

BigDecimal作为Java中常用的金额计算类,由于金额计算涉及小数点甚至四舍五入计数法等。其他数据类型如:Double、Float、Integer、Long等都不能作为一个企业的标准金额计算工具。经常有人用除BigDecimal以外的其他类型计算金额,导致计算结果有误,要么少了小数点,要么多了小数点,要么报错等等。因此,作为一名合格的程序员,强调用BigDecimal作为基本金额计算类显得尤为重要。

一、BigDecimal、Double、String互转

入参为Double,返回小数点后指定位数的数值

public static Double setScaleUp(Double value, int scale) {

		BigDecimal bd = BigDecimal.valueOf(value.doubleValue());
		return Double.valueOf(bd.setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue());
	}

获取指定小数点位数的值,并四舍五入

	/** 截取小数点位
	 *  @author 
	 *	@DateTime 2018年10月16日 上午9:58:52
	 *
	 *  @param value
	 *  @param scale
	 *  @return
	 */
	public static BigDecimal setScaleByBigDecimalDown(BigDecimal value, int scale) {
		return value.setScale(scale, BigDecimal.ROUND_DOWN);
	}

Double转Bigdecimal

	public static BigDecimal convertDoubleToBigdecimal(Double d1) {
		return BigDecimal.valueOf(d1 == null ? 0 : d1);
	}

BigDecimal转Double

	/**
	 * 转换BigDecimal 到double
	 * @author 
	 * @DateTime 2017年11月11日 上午10:14:44
	 *
	 * @param bigDecimal
	 * @return
	 */
	public static Double convertBigDecimalToDouble(BigDecimal bigDecimal) {
		return bigDecimal == null ? null : bigDecimal.doubleValue();
	}

String转BigDecimal

public static BigDecimal converStringToBigdecimal(String st, int scale) {
		if (StringUtils.isEmpty(st)) {
			return BigDecimal.ZERO;
		} else {
			return FcsNumberUtils.setScaleOfBigDecimal(new BigDecimal(st), scale);
		}
	}

BigDecimal转String

	public static String bigdecimalToString(BigDecimal b1) {

		if (b1 == null) {
			return "0";
		} else {
			return b1.toString();
		}
	}

Double转String

	public static String doubleToString(Double b1) {

		if (b1 == null) {
			return "0";
		} else {
			return b1.toString();
		}
	}

BigDecimal转Long

    public static Long convertBigDecimalToLong(BigDecimal bigDecimal) {
        return bigDecimal == null ? null : bigDecimal.longValue();
    }

二、常用计算方法

BigDecimal比较大小

    /**
	 * @author 
	 * @DateTime 2018年1月16日 上午10:37:55
	 *
	 * @param b1
	 * @param b2
	 * @return
	 */
	public static boolean compareToOnBigDecimal(BigDecimal b1, BigDecimal b2) {

		if (b1 == null) {
			b1 = BigDecimal.ZERO;
		}
		if (b2 == null) {
			b2 = BigDecimal.ZERO;
		}
		if (b1.compareTo(b2) == 0) {
			return true;
		} else {
			return false;
		}
	}

BigDecimal加法计算

	public static BigDecimal addOfBigDecimal(BigDecimal d1, BigDecimal d2, int scale) {

		if (d1 == null) {
			d1 = BigDecimal.ZERO;
		}
		if (d2 == null) {
			d2 = BigDecimal.ZERO;
		}
		return FcsNumberUtils.addOfBigDecimal(d1, d2, scale);
	}

BigDecimal减法计算

	/**
	 *  @author  
	 *	@DateTime 2018年3月23日 下午5:30:06
	 *
	 *  @param d1
	 *  @param d2
	 *  @param scale
	 *  @return
	 */
	public static BigDecimal subtractOfBigDecimal(BigDecimal d1, BigDecimal d2, int scale) {

		if (d1 == null) {
			d1 = BigDecimal.ZERO;
		}
		if (d2 == null) {
			d2 = BigDecimal.ZERO;
		}
		if (scale 

既然都看完了整篇文章,相信对你一定有所帮助。原创不易,勿做伸手党。
 

点击下方【打赏】小编,或者关注公众号给予支持,你们的每一份鼓励都将是小编伟大的动力。
 

 
 
  
  
   同名原创公众号:   
   **程序大视界**