Skip to content

ICU-22872 Fix BigDecimal scale in DecimalFormat.parse()#3853

Open
aryanraj45 wants to merge 1 commit intounicode-org:mainfrom
aryanraj45:ICU-22872-decimal-format-bigdecimal-scale
Open

ICU-22872 Fix BigDecimal scale in DecimalFormat.parse()#3853
aryanraj45 wants to merge 1 commit intounicode-org:mainfrom
aryanraj45:ICU-22872-decimal-format-bigdecimal-scale

Conversation

@aryanraj45
Copy link

Fix BigDecimal scale preservation in DecimalFormat.parse()

Root cause: The internal BCD (Binary Coded Decimal) representation doesn't store trailing zeros. When parsing decimal numbers like "0.0" or "1.00", the BigDecimal scale was incorrectly set to 0 instead of preserving the number of decimal places from the input string.

Solution:

  • Added fractionDigitCount field to ParsedNumber to track the number of digits after the decimal point during parsing
  • Modified getNumber() to apply the correct scale to the resulting BigDecimal when it would otherwise be too small
  • This scale adjustment is skipped for numbers with exponents (e.g., "1.0E3") since the exponent changes the effective decimal position

Changes:

  • ParsedNumber.java: Added fractionDigitCount field and scale adjustment logic in getNumber()
  • DecimalMatcher.java: Set fractionDigitCount when decimal separator is encountered
  • NumberFormatTest.java: Added TestParseBigDecimalScale test case covering inputs like "0.0", "1.00", "123.450"

Testing:

  • All existing tests pass (2890 tests, 0 failures)
  • New unit test validates scale preservation for various decimal inputs
  • Verified that scientific notation is handled correctly (no scale adjustment when exponent is present)

Related: https://github.com/unicode-org/icu/blob/main/CONTRIBUTING.md

Checklist

  • Required: Issue filed: ICU-22872
  • Required: The PR title must be prefixed with a JIRA Issue number. Example: "ICU-22872 Fix BigDecimal scale in DecimalFormat.parse()"
  • Required: Each commit message must be prefixed with a JIRA Issue number. Example: "ICU-22872 Fix BigDecimal scale in DecimalFormat.parse()"
  • Issue accepted (done by Technical Committee after discussion)
  • Tests included, if applicable
  • API docs and/or User Guide docs changed or added, if applicable
  • Approver: Feel free to merge on my behalf

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

Successfully merging this pull request may close these issues.

1 participant