I need to be able to be able to parse a "1.000,"
string into a BigDecimal with value 1000
and then parse that same BigDecimal back into a "1.000,"
string.
Both should happen using the same exact DecimalFormat
formatter.
Why? Because I need to compare at the end of each stage whether the two strings are equal, meaning whether the parsing was correct or not.
So far I tried to use these two patterns: DecimalFormat("###.##0,")
and DecimalFormat("0,")
but they don't produce the exact "1.000," at the end.
Here's my code:
List<NumberFormat> formats = new ArrayList<NumberFormat>();
formats.add(DecimalFormat("###.##0,"));
formats.add(DecimalFormat("0,"));
for(NumberFormat format : formats) {
Number number = format.parse(text);
format.setParseIntegerOnly(false);
if (number != null && format(format, number).equals(text)) {
return true;
}
}
I actually figured this out. I had to define the patterns as
DecimalFormat("###,##0.", new DecimalFormatSymbols(Locale.GERMAN))
and
DecimalFormat("0.", new DecimalFormatSymbols(Locale.GERMAN))
and I had to add this to the NumberFormat object:
format.setParseIntegerOnly(false);
to make it work for EU numbers.