[
https://issues.apache.org/jira/browse/CAMEL-7742?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14108862#comment-14108862
]
Charles Moulliard commented on CAMEL-7742:
------------------------------------------
Remark : We can't use together the pattern provided as property by @DataField
annotation with DecimalFormat as a comparison is done automtically with the
pattern of the LOCALE and we could generate a Malformed pattern exception
{code}
FRENCH LOCALE
java.lang.IllegalArgumentException: Malformed pattern "###.##0,00"
at java.text.DecimalFormat.applyPattern(DecimalFormat.java:2610)
at java.text.DecimalFormat.applyPattern(DecimalFormat.java:2304)
Locale.setDefault(locale);
NumberFormat f = NumberFormat.getInstance(locale);
if (f instanceof DecimalFormat) {
DecimalFormat df = ((DecimalFormat) f);
df.setParseBigDecimal(true);
df.applyPattern(pattern); // ERROR RAISED HERE
DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale);
symbols.setDecimalSeparator(decimalSeparator);
symbols.setGroupingSeparator(groupingSeparator);
df.setDecimalFormatSymbols(symbols);
}
// parse the string
BigDecimal bigDecimal = (BigDecimal) f.parse(formattedString);
bigDecimal.setScale(precision,RoundingMode.HALF_DOWN);
System.out.println(bigDecimal);
Locale.getDefault();
where we use as parameter
locale = Locale.FRENCH;
pattern = "###.##0,00";
groupingSeparator = ',';
decimalSeparator = '.';
formatedString = "123,000.45";
{code}
> Support grouping separtor for Camel Bindy (BigDecimal)
> ------------------------------------------------------
>
> Key: CAMEL-7742
> URL: https://issues.apache.org/jira/browse/CAMEL-7742
> Project: Camel
> Issue Type: Improvement
> Components: camel-bindy
> Affects Versions: 2.13.2
> Reporter: Charles Moulliard
> Assignee: Charles Moulliard
>
> Issue
> {code}
> I currently have a problem concerning Camel Bindy. I have a csv file that
> should be parsed with Camel Bindy using a given model class. This csv file
> contains a number that should be parsed as a BigDecimal. The problem is that
> this number contains a comma as a grouping separator and a point as the
> decimal separator.
> If I start the camel route the unmarshal process will throw a
> NumberFormatException mentioning the invalid format of that number. So I
> tried to add a pattern to the DataField annotation, but the error stays.
> After digging throw the documentation (http://camel.apache.org/bindy.html)
> and the source code of FormatFactory
> (http://grepcode.com/file_/repo1.maven.org/maven2/org.apache.camel/camel-bindy/2.13.1/org/apache/camel/dataformat/bindy/FormatFactory.java/?v=source)
> I'm a little bit confused, because the documentation mentions the ability to
> specify a grouping separator, but the source code shows that the pattern is
> ignored for BigDecimal data types.
> Does anyone know how to specify that grouping separator? Or is this not
> implemented yet?
> {code}
> Solution
> {code}
> public void parsePattern(String formattedString,
> Locale locale,
> String pattern,
> char groupingSeparator,
> char decimalSeparator) throws ParseException {
> DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale);
> symbols.setGroupingSeparator(groupingSeparator);
> symbols.setDecimalSeparator(decimalSeparator);
> DecimalFormat decimalFormat = new DecimalFormat(pattern, symbols);
> decimalFormat.setParseBigDecimal(true);
> // parse the string
> BigDecimal bigDecimal = (BigDecimal)
> decimalFormat.parse(formattedString);
> bigDecimal.setScale(precision);
> System.out.println(bigDecimal);
> }
> {code}
--
This message was sent by Atlassian JIRA
(v6.2#6252)