问题描述
对接项目中,遇到对面返回金额栏位超过了千位,自带“,”-千位符的情况,原设计中未考虑此情况,直接使用
new BigDecimal(str);
的方式处理,此时系统就呵呵哒了:
解决办法
1.很简单,而又不高大上的处理方式:
将字符",“转换成”"
try {
String replaced = str.replace(",", "");
BigDecimal result = new BigDecimal(replaced);
return result;
} catch (NumberFormatException e) {
return null;
}
2、使用DecimalFormat 处理
DecimalFormat format = new DecimalFormat();
format.setParseBigDecimal(true);
ParsePosition position = new ParsePosition(0);
BigDecimal parse = (BigDecimal) format.parse(str, position);