fyzhou / beanio

Automatically exported from code.google.com/p/beanio
Apache License 2.0
0 stars 0 forks source link

DecimalFormat always uses default locale #83

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
When using DecimalFormat, it is common to override the default locale with the 
US locale to force the period sign to be the fraction separator. It would be 
nice if I could set the locale so BeanIO uses dots as separator as they are 
frequently used (not comma as in my default locale).

As a workaround, I'm extending BigDecimalTypeHandler to create BigDecimal 
instance using the US locale.

Original issue reported on code.google.com by fbar...@scanntech.com on 18 Jun 2013 at 9:56

GoogleCodeExporter commented 9 years ago
Are you thinking you could set a 'locale' property on the TypeHandler 
declaration?  Or that 'locale' would be a new attribute on a field declaration?

And then I assume you are using the locale to get a DecimalFormatSymbols 
instance to set on the DecimalFormat, right?  Or is there another way I don't 
know about?

Thanks,
Kevin

Original comment by kevin.s...@gmail.com on 19 Jun 2013 at 1:57

GoogleCodeExporter commented 9 years ago
This is the implemented workaround:

public class BigDecimalUsLocaleTypeHandler extends BigDecimalTypeHandler {
 @Override
 protected DecimalFormat createDecimalFormat() {
  return new DecimalFormat(getPattern(), DecimalFormatSymbols.getInstance(Locale.US));
 }
}

I think that the most common scenario will probably be to use the same locale 
all along the project. In this scenario, overriding the locale property on the 
TypeHandler declaration will do.

Another posibility is to have a global property that configures all handlers so 
in case I wish to use the same locale on different handlers I don't need to 
configure them individually.

Thanks.

Original comment by fbar...@scanntech.com on 19 Jun 2013 at 2:01

GoogleCodeExporter commented 9 years ago
Thanks, that makes sense to me- I'll look into supporting both of your 
suggestions for 2.1.x.

Original comment by kevin.s...@gmail.com on 19 Jun 2013 at 2:32

GoogleCodeExporter commented 9 years ago
Modified for 2.1.0.M2.  Snapshot JAR attached.

Original comment by kevin.s...@gmail.com on 23 Sep 2013 at 4:06

Attachments:

GoogleCodeExporter commented 9 years ago

Original comment by kevin.s...@gmail.com on 16 Nov 2013 at 5:29