I have to convert a German locale formatted String
to a BigDecimal
. However, I\'m struggling with the best solution.
The following code sho
DecimalFormat
has a method called setParseBigDecimal that causes parse()
to return a BigDecimal
. You just need to cast the returned Number
.
String numberString = "2.105,88";
NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
if (nf instanceof DecimalFormat) {
DecimalFormat df = (DecimalFormat) nf;
df.setParseBigDecimal(true);
BigDecimal parsed = (BigDecimal) df.parse(numberString);
System.out.println(parsed);
}
Output:
2105.88
setParseBigDecimal
was introduced in Java 1.5.