summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian Jones <cbj@gnu.org>2003-01-25 20:14:56 +0000
committerBrian Jones <cbj@gnu.org>2003-01-25 20:14:56 +0000
commit5684576cbf115e647cd10b8198e1d05dd349119b (patch)
tree880105e075123d7e208790224fc6a2a27a737b9b
parent91e929c29a906e330df9c5585f085bf27b92a870 (diff)
downloadclasspath-5684576cbf115e647cd10b8198e1d05dd349119b.tar.gz
2003-01-25 Stephen Crawley <crawley@dstc.edu.au>
* java/math/BigDecimal.java (BigDecimal): enhance parsing of exponents (toString): do not return Strings starting with . and - erroneously. Improves Mauve results to 12 of 600 instead of 16 of 338 on DiagBigDecimal.
-rw-r--r--ChangeLog6
-rw-r--r--java/math/BigDecimal.java58
2 files changed, 47 insertions, 17 deletions
diff --git a/ChangeLog b/ChangeLog
index baeda34fc..4eec10500 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2003-01-25 Stephen Crawley <crawley@dstc.edu.au>
+
+ * java/math/BigDecimal.java (BigDecimal): enhance parsing of exponents
+ (toString): do not return Strings starting with . and - erroneously.
+ Improves Mauve results to 12 of 600 instead of 16 of 338 on DiagBigDecimal.
+
2003-01-25 C. Brian Jones <cbj@gnu.org>
* NEWS: note jni.h updates and patches
diff --git a/java/math/BigDecimal.java b/java/math/BigDecimal.java
index 713ba08e1..403ba18b3 100644
--- a/java/math/BigDecimal.java
+++ b/java/math/BigDecimal.java
@@ -178,15 +178,29 @@ public class BigDecimal extends Number implements Comparable
// Now parse exponent.
if (point < len)
{
- int exp = Integer.parseInt (num.substring (point + 1));
- exp -= scale;
- if (exp > 0)
+ point++;
+ if (num.charAt(point) == '+')
+ point++;
+
+ if (point >= len )
+ throw new NumberFormatException ("no exponent following e or E");
+
+ try
{
- intVal = intVal.multiply (BigInteger.valueOf (10).pow (exp));
- scale = 0;
+ int exp = Integer.parseInt (num.substring (point));
+ exp -= scale;
+ if (exp > 0)
+ {
+ intVal = intVal.multiply (BigInteger.valueOf (10).pow (exp));
+ scale = 0;
+ }
+ else
+ scale = - exp;
+ }
+ catch (NumberFormatException ex)
+ {
+ throw new NumberFormatException ("malformed exponent");
}
- else
- scale = - exp;
}
}
@@ -431,19 +445,29 @@ public class BigDecimal extends Number implements Comparable
if (scale == 0)
return bigStr;
- int point = bigStr.length() - scale;
boolean negative = (bigStr.charAt(0) == '-');
- StringBuffer sb = new StringBuffer(bigStr.length() + 1 +
- (point <= 0 ? -point+1 : 0));
- if (negative)
- sb.append('-');
- while (point <= 0)
+
+ int point = bigStr.length() - scale - (negative ? 1 : 0);
+
+ StringBuffer sb = new StringBuffer(bigStr.length() + 2 +
+ (point <= 0 ? (-point + 1) : 0));
+ if (point <= 0)
+ {
+ if (negative)
+ sb.append('-');
+ sb.append('0').append('.');
+ while (point < 0)
+ {
+ sb.append('0');
+ point++;
+ }
+ sb.append(bigStr.substring(negative ? 1 : 0));
+ }
+ else
{
- sb.append('0');
- point++;
+ sb.append(bigStr);
+ sb.insert(point + (negative ? 1 : 0), '.');
}
- sb.append(bigStr.substring(negative ? 1 : 0));
- sb.insert(point, '.');
return sb.toString();
}