0

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;
 }
}
goe
  • 1,153
  • 2
  • 12
  • 24

2 Answers2

0

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.

goe
  • 1,153
  • 2
  • 12
  • 24
-1

Maybe you should consider using Regex for your problem.

You can replace , for . and add 000 at the end.

String str = "10,1";
str = str.replaceAll(",", ".");
System.out.println(str);

This will print "10.1"

EDIT:

String str = "1.000,";
str = str.replaceAll(",", "");
str = str.replaceAll(".", "");
int bigDecimal = Integer.valueOf(str);
//first part is done
System.out.println(str);
String regex = "(\\d)(?=(\\d{3})+$)";
String result = bigDecimal.toString();
result = result.replaceAll(regex,".");
result = result + ",";

Is this what you are looking for?

Supamiu
  • 8,501
  • 7
  • 42
  • 76