Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decimal numbers not being correctly parsed #142

Closed
aspedrosa opened this issue May 31, 2021 · 0 comments
Closed

Decimal numbers not being correctly parsed #142

aspedrosa opened this issue May 31, 2021 · 0 comments

Comments

@aspedrosa
Copy link

I was using the ParseDelimitedRow filter and set the columns config, where some of the columns were double. I end up with parsing problems for valid inputs such as 2.434534534.

Currently, the code to parse both floats and doubles is similar to this

Objects.requireNonNull(value, "value can't be null");

if (value instanceof String && isNumber((String) value)) {
    return new BigDecimal(value.toString()).doubleValue();
}
if (value instanceof Number) {
    Number number = (Number) value;
    return number.doubleValue();
}
throw new DataException(String.format("Cannot parse 64-bits double content from \"%s\"", value));

The isNumber method will always return false for numbers with decimal part since it doesn't check for characters like . or ,. This leads to a DataException for these types.

I think we could change the code to something like this

Objects.requireNonNull(value, "value can't be null");

if (value instanceof String) {
    try {
        return new BigDecimal(value.toString()).doubleValue();
    } catch (NumberFormatException e) {
        throw new DataException(String.format("Cannot parse 64-bits double content from \"%s\"", value));
    }
}
if (value instanceof Number) {
    Number number = (Number) value;
    return number.doubleValue();
}
throw new DataException(String.format("Cannot parse 64-bits double content from \"%s\"", value));

I think we don't need to check if the string number is correct since the BigDecimal is already making those checks. This change is also applicable to the other numeric types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant