简体   繁体   中英

Java number parse challenge with signed number strings

I'm using Pentaho's Data Integration to parse CSV values. In one of the input files, I have columns of numbers which are formatted as such:

+000000000.00000,-0000001000.0000,00000000.000

Data Integration uses Java number parsing format strings, which apparently can handle the negative sign without issue, and the absence of a sign without issue, but when it encounters the '+' sign, will fail. If I include the '+' in the number format string, it will handle the '+' value, but then fails on the number without the '+' .

I have little control over the way the data comes in, and the only rule seems to be that if there's any negative value in the particular column, then all values will have a sign preceding the number.

I've attempted to use notation such as:

+?###,##0.#####

without success.

Is there a way to hand the number format a string which will handle the '+' if it is present, and ignore when it isn't?

I see only these options:

  1. Create your own version of the DecimalFormat and DecimalFormatSymbols classes to add a plus sign for the parsing; perhaps named ExtendedDecimalFormat and ExtendedDecimalFormatSymbols or some such. add a PlusSign definition to the ExtendedDecimalFormatSymbols (getter, setter, serialization, blah). If you do this, please put it on git and make it open source (so others don't have to repeat the work).

  2. Before handing the line to the csv parser, strip out any plus signs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM