BigDecimalUtil calculation tool class
Addition,
subtraction,
multiplication,
division,
rounding, keeping two decimal
places, discarding decimals, and increasing when greater than 0,
discarding decimals, and discarding directly
package com.rely.base.utils;
import java.math.BigDecimal;
/**
* TODO BigDecimalUtil 小数处理工具类, 所有方法默认四舍五入,
* <p>
* BigDecimalUtil.MONEY_POINT 小数位数默认保留2,暂不支持自定义
* <p>
* BigDecimalUtil.mc ==> 四舍五入
* BigDecimalUtil.mcDown ==> 舍弃 ===> 直接舍弃指定位数后的小数,如:1.9 = 1
* BigDecimalUtil.mcUp ==> 上升 ===> 舍弃指定位数后的小数,舍弃后小数的最后一位只要大于0就加1 --> 如: 1.1 = 2
*
* @author ws
* @mail [email protected]
* @date 2020/4/24 0024 9:51
* @return
*/
public class BigDecimalUtil {
// /**
// * TODO 货币保留两位小数
// */
// public static final int MONEY_POINT = 2;
// // 四舍五入( HALF_UP 5向上取,HALF_DOWN 5向下取)
// public static MathContext mc = new MathContext(MONEY_POINT + 1, RoundingMode.HALF_UP);
// // 舍弃 ===> 直接舍弃指定位数后的小数(FLOOR = 地板)
// public static MathContext mcDown = new MathContext(MONEY_POINT + 1, RoundingMode.FLOOR);
// // 上升 ===> 舍弃指定位数后的小数, 舍弃后小数的最后一位只要 >0 就加1 (FLOOR = 天花板)
// public static MathContext mcUp = new MathContext(MONEY_POINT + 1, RoundingMode.CEILING);
//==========================================================================
//============================== 具体方法 ===================================
//==========================================================================
// TODO BigDecimal 相加
public static BigDecimal add(BigDecimal v1, BigDecimal v2) {
if (v1 == null) {
v1 = new BigDecimal("0");
}
if (v2 == null) {
v2 = new BigDecimal("0");
}
return v1.add(v2);
}
// TODO BigDecimal 相减
public static BigDecimal subtract(BigDecimal v1, BigDecimal v2) {
if (v1 == null) {
v1 = new BigDecimal("0");
}
if (v2 == null) {
v2 = new BigDecimal("0");
}
return v1.subtract(v2);
}
// TODO BigDecimal 相乘
public static BigDecimal multiply(BigDecimal v1, BigDecimal v2) {
if (v1 == null) {
v1 = new BigDecimal("0");
}
if (v2 == null) {
v2 = new BigDecimal("0");
}
return v1.multiply(v2);
}
// TODO BigDecimal 相除
public static BigDecimal divide(BigDecimal v1, BigDecimal v2) {
if (v1 == null) {
v1 = new BigDecimal("0");
}
if (v2 == null) {
v2 = new BigDecimal("0");
}
return v1.divide(v2,2, BigDecimal.ROUND_HALF_UP);
}
/**
* TODO 四舍五入保留两位小数 ( HALF_UP 5向上取,HALF_DOWN 5向下取)
*/
public static BigDecimal parse(BigDecimal bg) {
return bg.setScale(2, BigDecimal.ROUND_HALF_UP);
}
/**
* TODO 上升 ===> 舍弃指定位数后的小数, ( CEILING= 天花板) ==> 舍弃后小数的最后一位只要 >0 就加1
*/
public static BigDecimal parseUP(BigDecimal bg) {
return bg.setScale(2, BigDecimal.ROUND_CEILING);
}
/**
* TODO 舍弃 ===> 直接舍弃指定位数后的小数 (FLOOR = 地板) ==> 直接舍弃指定位数后的小数
*/
public static BigDecimal parseDown(BigDecimal bg) {
return bg.setScale(2, BigDecimal.ROUND_FLOOR);
}
/**
* TODO 测试方法
*
* @author ws
* @mail [email protected]
* @date 2020/4/24 0024 17:17
*/
public static void main(String[] args) {
//============================================== 加法 ==============================================================
// 加法
System.out.println("四舍五入 1.50 + 1.504 = " + parse(add(new BigDecimal("1.50"), new BigDecimal("1.504"))));
System.out.println("四舍五入 1.50 + 1.505 = " + parse(add(new BigDecimal("1.50"), new BigDecimal("1.505"))));
System.out.println("天花板 1.50 + 1.501 = " + parseUP(add(new BigDecimal("1.50"), new BigDecimal("1.501"))));
System.out.println("地板 1.50 + 1.509 = " + parseDown(add(new BigDecimal("1.50"), new BigDecimal("1.509"))));
//============================================== 减法 ==============================================================
// 减法
System.out.println("减法 =" + parse(subtract(new BigDecimal("100.09999999999998"), new BigDecimal("0.81"))));
}
}
Personal open source project (universal back-end management system) –> https://gitee.com/wslxm/spring-boot-plus2 , if you like, you can read this article. This is the end of this article. If you find it useful, please give a
thumbs up or pay attention to it. , Will continue to update more content from time to time... Thank you for watching!