Bug 90759 - BigDecimal.toPlainString() gives wrong result for negative integer values
Summary: BigDecimal.toPlainString() gives wrong result for negative integer values
Status: UNCONFIRMED
Alias: None
Product: classpath
Classification: Unclassified
Component: classpath (show other bugs)
Version: 0.99
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-06-05 15:13 UTC by Guillermo Rodriguez
Modified: 2019-06-05 15:15 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Guillermo Rodriguez 2019-06-05 15:13:47 UTC
BigDecimal.toPlainString() does not work as expected for negative integer values if the scale is also < 0.

Here's a test case:

    bd = new BigDecimal(-10);
    bd = bd.stripTrailingZeros();
    System.out.println("toString(): " + bd);
    System.out.println("toPlainString(): " + bd.toPlainString());

    BigDecimal bd = new BigDecimal(new BigInteger("-1", 10), -1);
    System.out.println("toString(): " + bd);
    System.out.println("toPlainString(): " + bd.toPlainString());

This prints:

toString(): -1.E+1
toPlainString(): -1  // -> should be -10
toString(): -1.E+1
toPlainString(): -1  // -> should be -10
Comment 1 Guillermo Rodriguez 2019-06-05 15:15:48 UTC
Here's a fix for this bug:

  public String toPlainString()
  {
    [...]
    else
      {
        // We must append zeros instead of using scientific notation.
        sb.append(bigStr);
+       int ndigits = bigStr.length() - (negative ? 1 : 0);
+       for (int i = ndigits; i < point; i++)
-       for (int i = bigStr.length(); i < point; i++)
          sb.append('0');
      }
    return sb.toString();
  }