Menu

#112 Parsing numbers with @CsvNumber

v1.0 (example)
wont-fix
None
5
2023-02-06
2023-02-06
Diego
No

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?

Discussion

  • Andrew Rucker Jones

    • status: open --> wont-fix
    • assigned_to: Andrew Rucker Jones
     
  • Andrew Rucker Jones

    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.

     
  • Diego

    Diego - 2023-02-06

    What should I do then if I want different formats for reading and writing? Or to support different formats for reading only?

     
  • Andrew Rucker Jones

    Please read the Javadoc for CsvNumber. There are only four parameters, and they all relate to this question.

     
  • Diego

    Diego - 2023-02-06

    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 to 17739.89

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.