Parsing numbers with @CsvNumber
Brought to you by:
aruckerjones,
sconway
Hi, I'm trying to parse numbers from a CSV, using the following piece of code:
@CsvNumber("0.##")
@CsvBindByName(column = "value", locale = "en-EN")
private BigDecimal value;
However, a value like "17,739.89"
is parsed as 17
, when I'm expecting either the correct number (17739.89
) or a formatting exception since the expected format is not found. How could I fix this?
This is as expected:
1. Your format string is incorrect. Try something like "#,##0.##"
2. java.text.DecimalFormat, which opencsv uses for parsing numbers, does not throw an exception. It simply stops parsing as soon as it hits a character it does not understand.
What should I do then if I want different formats for reading and writing? Or to support different formats for reading only?
Please read the Javadoc for CsvNumber. There are only four parameters, and they all relate to this question.
I think I was not clear enough. I'd like to be able to parse different formats from in the CSV. For instance, I'd like both
"17,739.89"
and"17739.89"
to be translated to17739.89