How can I parse a String to BigDecimal? [duplicate] How can I parse a String to BigDecimal? [duplicate] java java

How can I parse a String to BigDecimal? [duplicate]


Try this

// Create a DecimalFormat that fits your requirementsDecimalFormatSymbols symbols = new DecimalFormatSymbols();symbols.setGroupingSeparator(',');symbols.setDecimalSeparator('.');String pattern = "#,##0.0#";DecimalFormat decimalFormat = new DecimalFormat(pattern, symbols);decimalFormat.setParseBigDecimal(true);// parse the stringBigDecimal bigDecimal = (BigDecimal) decimalFormat.parse("10,692,467,440,017.120");System.out.println(bigDecimal);

If you are building an application with I18N support you should use DecimalFormatSymbols(Locale)

Also keep in mind that decimalFormat.parse can throw a ParseException so you need to handle it (with try/catch) or throw it and let another part of your program handle it


Try this

 String str="10,692,467,440,017.120".replaceAll(",",""); BigDecimal bd=new BigDecimal(str);


Try the correct constructorhttp://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#BigDecimal(java.lang.String)

You can directly instanciate the BigDecimal with the String ;)

Example:

BigDecimal bigDecimalValue= new BigDecimal("0.5");