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

java.math.BigDecimal("1.2e") throws StringIndexOutOfBoundsException

XMLWordPrintable

    • kestrel
    • sparc
    • solaris_2.5.1
    • Verified



      Name: sdC67446 Date: 08/04/99


      The constructor
      public BigDecimal(String val)
      of class java.math.BigDecimal throws StringIndexOutOfBoundsException
      if 'val' represents the number with no exponent after 'e'-symbol.
      NumberFormatException expected.

      The doc says:
      -------------------------------------------------------------
      public BigDecimal(String val)

           Translates the String representation of a BigDecmal into a
           BigDecimal. The String representation consists of an optional
           sign ('+' or '-') followed by a sequence of zero or more decimal
           digits ("the integer"), optionally followed by a fraction,
           optionally followed by an exponent.

           The fraction consists of of a decimal point followed by zero or
           more decimal digits. The string must contain at least one digit
           in either the integer or the fraction. The number formed by the
           sign, the integer and the fraction is referred to as the
           significand.

           The exponent consists of the character 'e'(0x75) or E (0x45)
           followed by one or more decimal digits. The value of the
           exponenet must lie between Integer.MIN_VALUE and
           Integer.MAX_VALUE, inclusive.

           The scale of the returned BigDecimal will be the number of digits
           in the fraction, or zero if the string contains no decimal point,
           subject to adjustment for any exponent: If the string contains an
           exponent, the exponent is subtracted from the scale. If the
           resulting scale is negative, the scale of the returned BigDecimal
           is zero and the unscaled value is multiplied by the appropriate
           power of ten so that, in every case, the resulting BigDecimal is
           equal to significand * 10exponent.

           The character-to-digit mapping is provided by
           Character.digit(char, int). The String may not contain any
           extraneous characters (whitespace, for example).

           Note: For floats (and doubles) other that NAN, +INFINITY and
           -INFINITY, this constructor is compatible with the values
           returned by Float.toString (and Double.toString). This is
           generally the preferred way to convert a float (or double) into a
           BigDecimal, as it doesn't suffer from the unpredictability of the
           BigDecimal(double) constructor.

           Note: the optional leading plus sign and trailing exponent were
           added in release 1.3.
           Parameters:
               val - String representation of BigDecimal.
           Throws:
               NumberFormatException - val is not a valid representation of
               a BigDecimal.
      ------------------------------------------------------------
      The test demonstrating the bug:
      -------------------------------------------------------------
      import java.math.BigDecimal;

      public class Test {
          public static void main(String[] args) {
      try {
                  BigDecimal bd = new BigDecimal("1.2e");
      } catch (Exception e) {
                  e.printStackTrace();
      }
          }
      }

      Output for Classic VM (build JDK-1.3-L, green threads, nojit)
      -------------------------------------------------------------
      Warning: JIT compiler "sunwjit" not found. Will use interpreter.
      java.lang.StringIndexOutOfBoundsException: String index out of range: 0
              at java.lang.String.charAt(String.java:506)
              at java.math.BigDecimal.<init>(BigDecimal.java:144)
              at Test.main(Test.java:6)
      -------------------------------------------------------------
      ======================================================================

            mmcclosksunw Michael Mccloskey (Inactive)
            dsvsunw Dsv Dsv (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: