String转BigDecimal问题之千位符字符串

问题描述

对接项目中,遇到对面返回金额栏位超过了千位,自带“,”-千位符的情况,原设计中未考虑此情况,直接使用
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);
发布了42 篇原创文章 · 获赞 0 · 访问量 1428

猜你喜欢

转载自blog.csdn.net/tcctcszhanghao/article/details/103630263