BigDecimal problem.

Alexandre Petit-Bianco apbianco@cygnus.com
Tue Oct 16 12:59:00 GMT 2001


I'm running into the following situation with BigDecimal. Consider
this code:

  private static final BigDecimal minDouble =
    new java.math.BigDecimal(Double.MIN_VALUE);

It ends up throwing a java.lang.NumberFormatException.

It seems to be that Long.parseLong doesn't handle what it is given as
an argument (`50E-324', and `scale' in BigDecimal would be modified
accordingly) as Long.parseLong won't handle anything but digits, and
the exception is thrown from there:

  private static long parseLong(String str, int index, int len, boolean isNeg,
				int radix) throws NumberFormatException
  {
    ...          
        if ((digval = Character.digit(str.charAt(index), radix)) < 0)
	  throw new NumberFormatException();

Long.parseLong() complies with the spec, but BigDecimal uses
Double.toString to convert the input number. If the input can't be
represented in a string without using the scientific notation,
Long.parseLong() won't receive something it can deal with. 4.0e-4
instead of Double.MIN_VALUE will trigger the same error; but 4.0e-3
won't.

I guess I can work around the problem by hand building a String
argument for BigDecimal(<min_value>) Nonetheless, I'm going to file a
PR.

./A



More information about the Java mailing list