]> granicus.if.org Git - icu/commitdiff
ICU-10045 Fix significant digits with zero for JAVA.
authorTravis Keep <keep94@gmail.com>
Fri, 24 May 2013 22:16:05 +0000 (22:16 +0000)
committerTravis Keep <keep94@gmail.com>
Fri, 24 May 2013 22:16:05 +0000 (22:16 +0000)
X-SVN-Rev: 33750

icu4j/main/classes/core/src/com/ibm/icu/text/DecimalFormat.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java

index c3894b81dbf425555acd28e534533ec27c07a696..0c29584c7d174fb450be6b6ff5f81c181662f80f 100644 (file)
@@ -1393,6 +1393,14 @@ public class DecimalFormat extends NumberFormat {
         } else if (fieldPosition.getFieldAttribute() == NumberFormat.Field.INTEGER) {
             fieldPosition.setEndIndex(result.length());
         }
+        
+        // This handles the special case of formatting 0. For zero only, we count the
+        // zero to the left of the decimal point as one signficant digit. Ordinarily we
+        // do not count any leading 0's as significant. If the number we are formatting
+        // is not zero, then either sigCount or digits.getCount() will be non-zero.
+        if (sigCount == 0 && digitList.count == 0) {
+          sigCount = 1;
+        }      
 
         // Determine whether or not there are any printable fractional digits. If
         // we've used up the digits we know there aren't.
index 9f34709980d8f2cc44d8805943132ee874894622..45d786cf7ee38ea7d41f338e246b50dac9b5a61a 100644 (file)
@@ -3220,4 +3220,52 @@ public class NumberFormatTest extends com.ibm.icu.dev.test.TestFmwk {
             testNum++;
         }
     }
+    
+    public void TestSignificantDigits() {
+        double input[] = {
+            0, 0,
+            123, -123,
+            12345, -12345,
+            123.45, -123.45,
+            123.44501, -123.44501,
+            0.001234, -0.001234,
+            0.00000000123, -0.00000000123,
+            0.0000000000000000000123, -0.0000000000000000000123,
+            1.2, -1.2,
+            0.0000000012344501, -0.0000000012344501,
+            123445.01, -123445.01,
+            12344501000000000000000000000000000.0, -12344501000000000000000000000000000.0,
+        };
+        String[] expected = {
+            "0.00", "0.00",
+            "123", "-123",
+            "12345", "-12345",
+            "123.45", "-123.45",
+            "123.45", "-123.45",
+            "0.001234", "-0.001234",
+            "0.00000000123", "-0.00000000123",
+            "0.0000000000000000000123", "-0.0000000000000000000123",
+            "1.20", "-1.20",
+            "0.0000000012345", "-0.0000000012345",
+            "123450", "-123450",
+            "12345000000000000000000000000000000", "-12345000000000000000000000000000000",
+        };
+        DecimalFormat numberFormat =
+            (DecimalFormat) NumberFormat.getInstance(ULocale.US);
+        numberFormat.setSignificantDigitsUsed(true);
+        numberFormat.setMinimumSignificantDigits(3);
+        numberFormat.setMaximumSignificantDigits(5);
+        numberFormat.setGroupingUsed(false);
+        for (int i = 0; i < input.length; i++) {
+            assertEquals("TestSignificantDigits", expected[i], numberFormat.format(input[i]));
+        }
+    }
+    
+    public void TestShowZero() {
+        DecimalFormat numberFormat =
+                (DecimalFormat) NumberFormat.getInstance(ULocale.US);
+        numberFormat.setSignificantDigitsUsed(true);
+        numberFormat.setMaximumSignificantDigits(3);
+        assertEquals("TestShowZero", "0", numberFormat.format(0.0));
+    }
 }