]> granicus.if.org Git - icu/commitdiff
ICU-13701 Use exact string-to-double conversion in Java.
authorShane Carr <shane@unicode.org>
Fri, 11 Jan 2019 05:23:37 +0000 (21:23 -0800)
committerShane F. Carr <shane@unicode.org>
Fri, 11 Jan 2019 21:50:39 +0000 (15:50 -0600)
icu4j/main/classes/core/src/com/ibm/icu/impl/number/DecimalQuantity_AbstractBCD.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/number/DecimalQuantityTest.java

index c76247519ea2988c9707d642ccc3ed560d10559f..87434932a859f94176355cf2f379384d8d6ed815 100644 (file)
@@ -657,45 +657,9 @@ public abstract class DecimalQuantity_AbstractBCD implements DecimalQuantity {
             return isNegative() ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
         }
 
-        // TODO: Do like in C++ and use a library function to perform this conversion?
-        // This code is not as hot in Java because .parse() returns a BigDecimal, not a double.
-
-        long tempLong = 0L;
-        int lostDigits = precision - Math.min(precision, 17);
-        for (int shift = precision - 1; shift >= lostDigits; shift--) {
-            tempLong = tempLong * 10 + getDigitPos(shift);
-        }
-        double result = tempLong;
-        int _scale = scale + lostDigits;
-        if (_scale >= 0) {
-            // 1e22 is the largest exact double.
-            int i = _scale;
-            for (; i >= 22; i -= 22) {
-                result *= 1e22;
-                if (Double.isInfinite(result)) {
-                    // Further multiplications will not be productive.
-                    i = 0;
-                    break;
-                }
-            }
-            result *= DOUBLE_MULTIPLIERS[i];
-        } else {
-            // 1e22 is the largest exact double.
-            int i = _scale;
-            for (; i <= -22; i += 22) {
-                result /= 1e22;
-                if (result == 0.0) {
-                    // Further divisions will not be productive.
-                    i = 0;
-                    break;
-                }
-            }
-            result /= DOUBLE_MULTIPLIERS[-i];
-        }
-        if (isNegative()) {
-            result = -result;
-        }
-        return result;
+        StringBuilder sb = new StringBuilder();
+        toScientificString(sb);
+        return Double.valueOf(sb.toString());
     }
 
     @Override
index faaa9b3cf48403caf9e02294c850883ee0864f33..882a6924c3c83a1801446622b36ec553349f0a49 100644 (file)
@@ -492,8 +492,7 @@ public class DecimalQuantityTest extends TestFmwk {
         Object[][] cases = new Object[][] {
             { "0", 0.0 },
             { "514.23", 514.23 },
-            // NOTE: This does not currently pass in Java. See DecimalFormat_AbstractBCD#toDecimal.
-            // { "-3.142E-271", -3.142e-271 }
+            { "-3.142E-271", -3.142e-271 }
         };
 
         for (Object[] cas : cases) {