diff options
Diffstat (limited to 'libjava/java/math/BigDecimal.java')
-rw-r--r-- | libjava/java/math/BigDecimal.java | 69 |
1 files changed, 50 insertions, 19 deletions
diff --git a/libjava/java/math/BigDecimal.java b/libjava/java/math/BigDecimal.java index 713ba08e151..fc99cf1f7d2 100644 --- a/libjava/java/math/BigDecimal.java +++ b/libjava/java/math/BigDecimal.java @@ -178,15 +178,31 @@ 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 (signum () == 0) + scale = 0; + else 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; } } @@ -198,7 +214,7 @@ public class BigDecimal extends Number implements Comparable public static BigDecimal valueOf (long val, int scale) throws NumberFormatException { - if (scale == 0) + if ((scale == 0) && ((int)val == val)) switch ((int) val) { case 0: @@ -252,7 +268,7 @@ public class BigDecimal extends Number implements Comparable throw new ArithmeticException ("scale is negative: " + newScale); if (intVal.signum () == 0) // handle special case of 0.0/0.0 - return ZERO; + return newScale == 0 ? ZERO : new BigDecimal (ZERO.intVal, newScale); // Ensure that pow gets a non-negative value. int valScale = val.scale; @@ -415,6 +431,11 @@ public class BigDecimal extends Number implements Comparable return scale; } + public BigInteger unscaledValue() + { + return intVal; + } + public BigDecimal abs () { return new BigDecimal (intVal.abs (), scale); @@ -431,19 +452,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(); } |