Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8059175

Zero BigDecimal with negative scale prints leading zeroes in String.format

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: 8
    • Fix Version/s: 9
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • Resolved In Build:
      b46
    • CPU:
      x86_64
    • OS:
      linux

      Backports

        Description

        FULL PRODUCT VERSION :
        java version "1.8.0"
        Java(TM) SE Runtime Environment (build 1.8.0-b132)
        Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode)


        ADDITIONAL OS VERSION INFORMATION :
        Linux tim-desktop 3.2.0-69-generic #103-Ubuntu SMP Tue Sep 2 05:02:14 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

        A DESCRIPTION OF THE PROBLEM :
        Printing a zero-valued BigDecimal with negative scale (e.g. 0 * 10^34) using String.format conversion 'f' prints extraneous leading zeroes, contrary to the API description.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Execute the following lines:

        BigDecimal x = new BigDecimal(BigInteger.ZERO, -6);
        System.out.println(String.format("%.4f", x));


        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        The expected output is

        0.0000

        ACTUAL -
        The actual output is

        0000000.0000


        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        import java.math.BigDecimal;
        import java.math.BigInteger;

        public class Temp {
        public static void main(String[] args) {
        BigDecimal x = new BigDecimal(BigInteger.ZERO, -6);
        System.out.println(String.format("%.4f", x));
        }
        }

        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        Converting the value to a nonnegative scale works around the problem.


          Attachments

            Issue Links

              Activity

                People

                Assignee:
                bpb Brian Burkhalter
                Reporter:
                webbuggrp Webbug Group
                Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                  Dates

                  Created:
                  Updated:
                  Resolved: