Parsing a double superCSV with a comma as a decimal separator?

I want to parse a double comma as a decimal separator (',' instead of '.') Using SuperCSV CellProcessor

I want to parse the first element (0.35) on Double

0,35;40000,45 

I tried something like this:

  /** FRENCH_SYMBOLS */ private static final DecimalFormatSymbols FRENCH_SYMBOLS = new DecimalFormatSymbols(Locale.FRANCE); DecimalFormat df = new DecimalFormat(); df.setDecimalFormatSymbols(FRENCH_SYMBOLS); final CellProcessor[] processors = new CellProcessor[] { new NotNull(new ParseDouble(new FmtNumber(df))), new NotNull(new ParseBigDecimal(FRENCH_SYMBOLS)) }; 

ParseBigDecimal works fine, but parseDouble doesn't seem to work, it gives me an exception: org.supercsv.exception.SuperCsvCellProcessorException: '0.35' cannot be parsed as Double

+4
source share
1 answer

You are absolutely right - ParseDouble does not support the French-style decimal separator (comma), but ParseBigDecimal does. If you think this is a useful feature, why not send a request.

The simplest workaround is to simply create a StrReplace before ParseDouble to convert the comma to a full dot.

 new StrReplace(",", ".", new ParseDouble()) 

Alternatively, you can write a custom cell processor that:

  • parses double (with custom decimal separator)

  • converts BigDecimal to Double (call doubleValue() ) - this can be bound after your new ParseBigDecimal(FRENCH_SYMBOLS)

Oh, and in the future you might want to mention that your file is comma separated and you configured Super CSV using CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE :)

+6
source

All Articles