NumberUtils.java

时间:2023-03-09 09:06:49
NumberUtils.java

package com.vcredit.ddcash.batch.util;

import java.math.BigDecimal;

public class NumberUtils {

/**
* 以指定的基数位进行四舍五入
*
* @param original 原值
* @param roundBase 基数,例:以百位为舍入基础位,基数为100
* @return
* @throws RuntimeException
*/
public static BigDecimal roundByBase(BigDecimal original, RoundBase roundBase) throws RuntimeException {
if (original == null || roundBase == null) {
throw new RuntimeException("参数不能为空");
}
BigDecimal base = roundBase.getValue();// 基数
return original.divide(base).setScale(0, BigDecimal.ROUND_HALF_UP).multiply(base);
}

/**
* 四舍五入的基数枚举
*
* @author xutao
*/
public enum RoundBase {
GE(new BigDecimal(1), "个位"),
SHI(new BigDecimal(10), "十位"),
BAI(new BigDecimal(100), "百位"),
QIAN(new BigDecimal(1000), "千位"),
WAN(new BigDecimal(10000), "万位"),
SHIWAN(new BigDecimal(100000), "十万位"),
BAIWAN(new BigDecimal(1000000), "百万位");

RoundBase(BigDecimal value, String desc) {
this.value = value;
this.desc = desc;
}

/**
* 值
*/
private BigDecimal value;
/**
* 描述
*/
private String desc;

// setter and getter
public BigDecimal getValue() {
return value;
}

public void setValue(BigDecimal value) {
this.value = value;
}

public String getDesc() {
return desc;
}

public void setDesc(String desc) {
this.desc = desc;
}
}

}