how to parse a String to BigDecimal

private BigDecimal parsePrice(String price) {
        DecimalFormatSymbols symbols = new DecimalFormatSymbols();
        //symbols.setGroupingSeparator(',');
        symbols.setDecimalSeparator('.');
        String pattern = "#.##";
        DecimalFormat decimalFormat = new DecimalFormat(pattern, symbols);
        decimalFormat.setParseBigDecimal(true);

        // parse the string
        BigDecimal bigDecimal = null;
        try {
            bigDecimal = (BigDecimal) decimalFormat.parse(price);
        }
        catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(bigDecimal);
        return bigDecimal;
    }

猜你喜欢

转载自sunxboy.iteye.com/blog/2094370
今日推荐