Parsing numbers with pattern in Java without DecimalFormat

Is there a Java parsing library similar to DecimalFormat? I am unable to do what I want with DecimalFormat and regular expressions.

I need to be able to:

  • Parse only an integer (DecimalFormat needs more settings than just pattern)
  • Parse decimal (precise no rounding)
  • Parse numbers like 123,456.789. (DecimalFormat for ###,###.### accepts "123456789" as well)

2 Answers

  1. Keith- Reply


    You could try treating it like a String and tokenizing it.

    str.split("\\.") will split out the decimal side and then substring it to your precision, then on the number side you could split(",")

    This is quite a bit of manual parsing work though...

  2. Ken- Reply


    I would use Regexp and NumberFormat:

    Map<Pattern,NumberFormat> POSSIBLE_FORMATS = ...;
    String input = ...;
    Number value = null;
    for (Map.Entry<Pattern,NumberFormat> possible : POSSIBLE_FORMATS.entrySet()) {
      if (possible.getKey().matcher(input).matches()) {
        value = possible.getKeValue().parse(input);
    if (value == null) {
      throw new IllegalArgumentExcption("no valid input format: " + input);

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>