Reputation: 35
I need to verify if a String displayed is numeric. The format of the value is 123.345.678,99. I.e., the grouping separators are dots and the decimal separator is a comma.
I've tried the DecimalFormatter where I set the separators:
DecimalFormat formatter = (DecimalFormat) NumberFormat.getCurrencyInstance();
DecimalFormatSymbols symbols = formatter.getDecimalFormatSymbols();
symbols.setGroupingSeparator('.');
symbols.setDecimalSeparator(',');
formatter.setDecimalFormatSymbols(symbols);
formatter.setGroupingUsed(true);
System.out.println(formatter.format(incoming));
However, in this case, I'm getting
java.lang.IllegalArgumentException: Cannot format given Object as Number
I've tried also to add
formatter.applyPattern("###,###,###.##");
but it didn't help.
Any ideas?
I could also use regexp I guess but wanted first to make use of this DecimalFormatter
if possible.
Upvotes: 2
Views: 1073
Reputation: 311518
I'd lose the currency, and just explicily create a new DecimalFormat
with the relevant symbols. Once you have that, you should use it to parse the incoming string, not to format it:
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setDecimalSeparator(',');
dfs.setGroupingSeparator('.');
DecimalFormat df = new DecimalFormat();
df.setDecimalFormatSymbols(dfs);
System.out.println(df.parse(stringToTest));
Upvotes: 1