BigDecimal num = new BigDecimal("100.000");
BigDecimal numNoEndZero = num.stripTrailingZeros(); //numNoEndZero :1E+2
System.out.println(numNoEndZero.toString());
按上面的方法输出结果,会显示科学计数法,所以需要处理一下,解决方法:
String numNoEndZeroStr = new BigDecimal("100.000").stripTrailingZeros().toPlainString(); //numNoEndZeroStr :100
System.out.println(numNoEndZeroStr);
通过该方法处理BigDecimal类型数据后面的0的方法实现:
* @Title: clearNoUseZeroForBigDecimal
* @Description: 去掉BigDecimal尾部多余的0,通过stripTrailingZeros().toPlainString()实现
*
@param
num
*
@return
BigDecimal
public
static
BigDecimal clearNoUseZeroForBigDecimal(BigDecimal num) {
BigDecimal returnNum
=
null
;
String numStr
=
num.stripTrailingZeros().toPlainString();
if
(numStr.indexOf(".") == -1
) {
//
如果num 不含有小数点,使用stripTrailingZeros()处理时,变成了科学计数法
returnNum =
new
BigDecimal(numStr);
}
else
{
if
(num.compareTo(BigDecimal.ZERO) == 0
) {
returnNum
=
BigDecimal.ZERO;
}
else
{
returnNum
=
num.stripTrailingZeros();
return
returnNum;
不通过该方法处理BigDecimal类型数据实现
* @Title: removeAmtLastZero
* @Description: 金额处理,去掉BigDecimal尾部多余的0
*
@param
num
*
@return
BigDecimal
public
static
BigDecimal removeAmtLastZero(BigDecimal num) {
String strNum
=
num.toString();
if
(strNum.indexOf('.') != -1
) {
String[] arr
= strNum.split("\\."
);
String strDecimals
= arr[1
];
List
<String> list =
new
ArrayList<String>
();
boolean
isCanAdd =
false
;
for
(
int
i = strDecimals.length() - 1; i > -1; i--
) {
String ss
=
String.valueOf(strDecimals.charAt(i));
if
(!ss.equals("0"
)) {
isCanAdd
=
true
;
//
从最后的字符开始算起,遇到第一个不是0的字符开始都是需要保留的字符
if
(!ss.equals("0") ||
isCanAdd) {
list.add(ss);
StringBuffer strZero
=
new
StringBuffer();
for
(
int
i = list.size() - 1; i > -1; i--
) {
strZero.append(list.get(i));
strNum
= String.format("%s.%s", arr[0
], strZero.toString());
return
new
BigDecimal(strNum);