ICU4J: setDecimalPatternMatchRequired requires decimal even if field parses to INF/NaN
General
Other Data
General
Other Data
Description
If `setDecimalPatternMatchRequired(true)` is called on a `DecimalFormat` and the pattern contains a decimal separator, but the data to parse contains an infinity or NaN representation, then `parse()` returns and error instead of returning Double infinity/NaN.
It seems there might want to be an exception so this validator always succeeds if FLAG_NAN or FLAG_INFINITY is set, so that a decimal is only required if it actually parsed a number?
Activity
Show:
Rich Gillam
March 19, 2025 at 11:20 PM
Really sorry about the long delay here. Thanks for prodding us to finally get this in!
Steve Lawrence
March 19, 2025 at 12:26 PM
Is there anything I can do to help get this merged? This issue has a PR with a fix to Java and C versions, with tests and approvals:
If `setDecimalPatternMatchRequired(true)` is called on a `DecimalFormat` and the pattern contains a decimal separator, but the data to parse contains an infinity or NaN representation, then `parse()` returns and error instead of returning Double infinity/NaN.
Code to reproduce the issue:
```scala
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(java.util.Locale.US);
symbols.setInfinity("INF")
DecimalFormat df = new DecimalFormat("0.00", symbols)
df.setDecimalPatternMatchRequired(true)
Number res = df.parse("INF", new ParsePosition(0))
assertEquals(Double.POSITIVE_INFINITY, res)
```
Note that Inf/NaN works as expected if the pattern does not contain a decimal separator since the data also won't contain a decimal separator.
The issue seems to be in the `RequireDecimalSeparatorValidator`:
https://github.com/unicode-org/icu/blob/main/icu4j/main/classes/core/src/com/ibm/icu/impl/number/parse/RequireDecimalSeparatorValidator.java#L26-L29
It seems there might want to be an exception so this validator always succeeds if FLAG_NAN or FLAG_INFINITY is set, so that a decimal is only required if it actually parsed a number?