I have this String: 10,692,467,440,017.120 (it's an amount).
I want to parse it to a BigDecimal. The problem is that I have tried both DecimalFormat and NumbeFormat in vain.
The doubleValue() method of Java BigDecimal class is used to convert the BigDecimal value into a double type. If BigDecimal has very big value represented as a double, it will be converted to Double. NEGATIVE_INFINITY or Double. POSITIVE_INFINITY as appropriate.
Try this
// Create a DecimalFormat that fits your requirements DecimalFormatSymbols symbols = new DecimalFormatSymbols(); symbols.setGroupingSeparator(','); symbols.setDecimalSeparator('.'); String pattern = "#,##0.0#"; DecimalFormat decimalFormat = new DecimalFormat(pattern, symbols); decimalFormat.setParseBigDecimal(true); // parse the string BigDecimal 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With