0

Here's what I have now:

String text = "-9,23";
Character character = '.';

DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator(character);
symbols.setGroupingSeparator(character.equals('.') ? ',' : '.');

DecimalFormat format = new DecimalFormat();
format.setDecimalFormatSymbols(symbols);
format.setParseBigDecimal(true);

ParsePosition parsePosition = new ParsePosition(0);
Object object = format.parse(text, parsePosition);

The result of this is -923 rather then some sort of java.lang.NumberFormatException which I would expect to get in this scenario. I need my code to fail hard when the incoming string based number doesn't match the predefined format standard (either US or EU) which comes from the db layer. What's the best way of doing that?

goe
  • 1,153
  • 2
  • 12
  • 24
  • works fine, it looks like the grouping characters get stripped out and then the string is evaluated, so if your decimal character is '.', your grouping character becomes a ',' which then gets stripped out, and your string becomes "-923" which is what you say its returning. – Dave Sep 16 '14 at 20:49
  • right, but I want it to throw an exception since -9,23 shouldn't really match x,xxx.xx format and shouldn't result in a BigDecimal object of -923. – goe Sep 16 '14 at 20:55
  • its not gonna throw an exception, because it is being coded for one of the other, it just will ignore commas or treat them like decimal points.. you need to look into a regex possibly to see if your number isUK or isUs.. – Dave Sep 16 '14 at 20:58
  • @goeagile:I think you need to look into locale:http://stackoverflow.com/questions/12836647/best-way-to-convert-locale-specific-string-to-bigdecimal – Cratylus Sep 16 '14 at 21:44

0 Answers0