带逗号的String类型金额转为BigDecimal

最近项目发现一个问题,有关于Excel中的金额,返回的时候有的银行是带逗号格式,导致后台转换报错了,直接new BigDecimal(cell.getValue())是不行的,必须用以下的方法。

public static BigDecimal revertB(String str) {
    DecimalFormat format = new DecimalFormat();
    format.setParseBigDecimal(true);
    ParsePosition position = new ParsePosition(0);
    BigDecimal parse = (BigDecimal) format.parse(str, position);

    if (str.length() == position.getIndex()) {
        return parse;
    }
    return null;
}

切记。。。。。。

猜你喜欢

转载自blog.csdn.net/airyearth/article/details/108488997
今日推荐