<i18n dev> RFR: 8174722: Wrong behavior of DecimalFormat with RoundingMode.UP in special case [v3]

Justin Lu jlu at openjdk.org
Wed Aug 2 23:32:23 UTC 2023


> Please review this PR, which addresses a case where Decimal Format would violate certain RoundingMode contracts given the right pattern and input decimal String.
> 
> For example,
> 
> 
> DecimalFormat df = new DecimalFormat();
> df.setRoundingMode(RoundingMode.UP);
> double small = 0.0001;
> double big = 1.0001;
> df.applyPattern("0.00");
> df.format(small); // returns 0.00, which violates UP
> df.format(big); // returns 1.01, which does not violate UP
> 
> 
> In this example `0.0001` becomes `0.00`, a decrease in magnitude. This violates the RoundingMode.UP contract as RoundingMode.UP states "Note that this rounding mode never decreases the magnitude of the calculated value."
> 
> This edge case is a result of when input decimal String values smaller than the absolute value of `.1` have more leading zeros between the decimal and the first non-zero digit (_0.0001 -> **3**_) than maximum fractional digits in the pattern (_0.00 -> **2**_).
> 
> The test validates the change by using data from larger counterparts. For example, If we are testing `0.0009`, we can format `1.0009` with the same pattern and mode that we use on `0.0009`, then compare the fractional portions to each other and ensure they are equal.

Justin Lu has updated the pull request incrementally with one additional commit since the last revision:

  Refactor underflowToZero

-------------

Changes:
  - all: https://git.openjdk.org/jdk/pull/14110/files
  - new: https://git.openjdk.org/jdk/pull/14110/files/a8e1a81b..ed889741

Webrevs:
 - full: https://webrevs.openjdk.org/?repo=jdk&pr=14110&range=02
 - incr: https://webrevs.openjdk.org/?repo=jdk&pr=14110&range=01-02

  Stats: 39 lines in 1 file changed: 2 ins; 22 del; 15 mod
  Patch: https://git.openjdk.org/jdk/pull/14110.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/14110/head:pull/14110

PR: https://git.openjdk.org/jdk/pull/14110


More information about the i18n-dev mailing list