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

The constructor for BigDecimal should accept leading and training spaces

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE REQUEST :
      If you wish to construct a BigDecimal from a String, you have to trim it first. This is according to the specification. The specification is misguided and should be changed.

      JUSTIFICATION :
      There is no value in distinguishing the textual representations " 0 " and "0". They mean and represent the same object to the human reader. The primary purpose of String representation of objects is for human readers to understand. Also, please note that the type Double does not have such a limitation, which makes the interface for creating a BigDecimal inconsistent with other number classes.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Please change this rule to the following one:

      > The String may not contain any extraneous characters, except that it may be preceded and followed by a sequence of white space characters of any length, which will be automatically stripped.

      ACTUAL -
      The documentation for BigDecimal(String val) says:

      > The String may not contain any extraneous characters (whitespace, for example).



      ---------- BEGIN SOURCE ----------
      import java.io.BufferedReader;
      import java.io.IOException;
      import java.io.InputStreamReader;
      import java.io.PrintStream;
      import java.math.BigDecimal;
      import java.net.URI;
      import java.net.URISyntaxException;

      public class hello {

      public static void main(String[] args) {
      final PrintStream out = System.out;
      final String sv = "0 ";
      try (final BufferedReader reader = new BufferedReader(new InputStreamReader((new URI(
      "http://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html#BigDecimal-java.lang.String-"))
      .toURL().openStream()))) {
      for (String line; (line = reader.readLine()) != null;) {
      assert !line
      .contains("String may not contain any extraneous characters (whitespace,");
      }
      } catch (IOException | URISyntaxException e) {
      e.printStackTrace();
      System.exit(01);
      }
      out.println(new Double(sv));
      out.println(new BigDecimal(sv.trim()));
      }
      };
      ---------- END SOURCE ----------

            darcy Joe Darcy
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: