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

PercentFormatter is not doing a RoundUp when formatterPattern is used

    XMLWordPrintable

    Details

      Description

      ADDITIONAL SYSTEM INFORMATION :
      JRE:
      sh-4.2$ java -version
      java version "1.8.0_181"
      Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

      OS:
      NAME="CentOS Linux"
      VERSION="7 (Core)"
      ID="centos"
      ID_LIKE="rhel fedora"
      VERSION_ID="7"
      PRETTY_NAME="CentOS Linux 7 (Core)"
      ANSI_COLOR="0;31"

      A DESCRIPTION OF THE PROBLEM :
      When using a formatPattern, There is no rounding up.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Please execute following program:

      public static void main(String[] args) {
              Object valueObject = new Double(14323.237575);
              String result = null;
              String formatPattern = "##.###%";
              NumberFormat formatter = null;
              formatter = NumberFormat.getPercentInstance();
              ((DecimalFormat) formatter).applyPattern(formatPattern);
               result = formatter.format(valueObject).toString();
              System.out.println("Percent Formatter result for Double with " + formatPattern + " is: " + result);

              result = formatter.format(new BigDecimal((Double)valueObject)).toString();
              System.out.println("Percent Formatter result for BigDecimal with " + formatPattern + " is: " + result);

              formatter = NumberFormat.getPercentInstance();
              formatPattern = "##.#####";
              ((DecimalFormat) formatter).applyPattern(formatPattern);
              result = formatter.format(valueObject).toString();
              System.out.println("Percent Formatter result for Double with " + formatPattern + " is: " + result);
              result = formatter.format(new BigDecimal((Double)valueObject)).toString();
              System.out.println("Percent Formatter result for BigDecimal with " + formatPattern + " is: " + result);


              formatter = NumberFormat.getPercentInstance();
              formatPattern = "##.###";
              ((DecimalFormat) formatter).applyPattern(formatPattern);
              result = formatter.format(valueObject).toString();
              System.out.println("Percent Formatter result for Double with " + formatPattern + " is: " + result);
              result = formatter.format(new BigDecimal((Double)valueObject)).toString();
              System.out.println("Percent Formatter result for BigDecimal with " + formatPattern + " is: " + result);
          }


      results:
      Percent Formatter result for Double with ##.###% is: 1432323.757%
      Percent Formatter result for BigDecimal with ##.###% is: 1432323.757%
      Percent Formatter result for Double with ##.##### is: 14323.23757
      Percent Formatter result for BigDecimal with ##.##### is: 14323.23757
      Percent Formatter result for Double with ##.### is: 14323.238
      Percent Formatter result for BigDecimal with ##.### is: 14323.238

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Whenever a pattern is available for Formatter, it does not round up the value.
      ACTUAL -
      It does round down whenever formatter has a pattern applied.

      ---------- BEGIN SOURCE ----------
      public static void main(String[] args) {
              Object valueObject = new Double(14323.237575);
              String result = null;
              String formatPattern = "##.###%";
              NumberFormat formatter = null;
              formatter = NumberFormat.getPercentInstance();
              ((DecimalFormat) formatter).applyPattern(formatPattern);
               result = formatter.format(valueObject).toString();
              System.out.println("Percent Formatter result for Double with " + formatPattern + " is: " + result);

              result = formatter.format(new BigDecimal((Double)valueObject)).toString();
              System.out.println("Percent Formatter result for BigDecimal with " + formatPattern + " is: " + result);

              formatter = NumberFormat.getPercentInstance();
              formatPattern = "##.#####";
              ((DecimalFormat) formatter).applyPattern(formatPattern);
              result = formatter.format(valueObject).toString();
              System.out.println("Percent Formatter result for Double with " + formatPattern + " is: " + result);
              result = formatter.format(new BigDecimal((Double)valueObject)).toString();
              System.out.println("Percent Formatter result for BigDecimal with " + formatPattern + " is: " + result);


              formatter = NumberFormat.getPercentInstance();
              formatPattern = "##.###";
              ((DecimalFormat) formatter).applyPattern(formatPattern);
              result = formatter.format(valueObject).toString();
              System.out.println("Percent Formatter result for Double with " + formatPattern + " is: " + result);
              result = formatter.format(new BigDecimal((Double)valueObject)).toString();
              System.out.println("Percent Formatter result for BigDecimal with " + formatPattern + " is: " + result);
          }
      ---------- END SOURCE ----------

      FREQUENCY : always


        Attachments

          Activity

            People

            Assignee:
            naoto Naoto Sato
            Reporter:
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: