javacsvapache-camelbigdecimalbindy

How to define a grouping separator for parsing BigDecimal numbers in Camel Bindy?


I have a CSV file that contains numbers in this format: 1,234.56 with , being the grouping separator and . being the decimal separator. I'd like to parse that number as a BigDecimal using Camel Bindy. This is the part of the model class for this number:

@DataField(pos = 6, required = true, precision = 2, pattern = "#,###.##")
public BigDecimal value;

The problem right now is, that this will cause a java.lang.NumberFormatException even though the correct pattern has been applied. If I remove that pattern from the annotation and the grouping separator from the number in the data file, then everything works fine. Or if I use Double type instead of BigDecimal.

The Camel Bindy Documentation mentions that the grouping separater for BigDecimal can be set, but the table beneath that information says that the pattern is only supported for Decimals not the BigDecimal type.

Also the source code of the used FormatFactory class shows that the pattern won't be used for BigDecimal types.

So my question is: how can I set and use a grouping separator for a BigDecimal type, like the documentation mentioned? Or is this currently not supported and I have to use Double instead?

P.S: the locale is currently set to en_US.


Solution

  • This feature has been added to Camel Bindy and can be used with the following annotation types:

    @DataField(pos = x, precision = 2, pattern = "#,###.##")
    private BigDecimal number;
    

    or:

    @DataField(pos = x, precision = 2, pattern = "#,###.##", groupingSeparator = ",", decimalSeparator = ".")
    private BigDecimal number;
    

    The first version uses the currently set Locale value. If this is not set it is possible to use the second version to specify own grouping and decimal separators.

    Both version can parse numbers like these:

    1,000.00
    -100
    123,456,789.00
    0.00

    I guess the first release containing this feature will be version 2.14.0.