30 import java.math.*; 31 import java.text.*; 32 import java.util.*; 33 34 public class Bug4208135 { 35 36 static DecimalFormat df; 37 38 static boolean err = false; 39 40 static public void main(String[] args){ 41 42 Locale defaultLoc = Locale.getDefault(); 43 Locale.setDefault(Locale.US); 44 45 df = new DecimalFormat(); 46 47 df.applyPattern("0.#E0"); 48 49 df.setDecimalSeparatorAlwaysShown(true); 50 checkFormat(new Double(0.0), "0.E0"); 51 checkFormat(new Double(10.0), "1.E1"); 52 checkFormat(new Double(1000.0), "1.E3"); 53 checkFormat(new Long(0), "0.E0"); 54 checkFormat(new Long(10), "1.E1"); 55 checkFormat(new Long(1000), "1.E3"); 56 checkFormat(new BigDecimal("0.0"), "0.E0"); 57 checkFormat(new BigDecimal("10.0"), "1.E1"); 58 checkFormat(new BigDecimal("1000.0"), "1.E3"); 59 checkFormat(new BigInteger("00"), "0.E0"); 60 checkFormat(new BigInteger("10"), "1.E1"); 61 checkFormat(new BigInteger("1000"), "1.E3"); 62 63 df.setDecimalSeparatorAlwaysShown(false); 64 checkFormat(new Double(0.0), "0E0"); 65 checkFormat(new Double(10.0), "1E1"); 66 checkFormat(new Double(1000.0), "1E3"); 67 checkFormat(new Long(0), "0E0"); 68 checkFormat(new Long(10), "1E1"); 69 checkFormat(new Long(1000), "1E3"); 70 checkFormat(new BigDecimal("0.0"), "0E0"); 71 checkFormat(new BigDecimal("10.0"), "1E1"); 72 checkFormat(new BigDecimal("1000.0"), "1E3"); 73 checkFormat(new BigInteger("0"), "0E0"); 74 checkFormat(new BigInteger("10"), "1E1"); 75 checkFormat(new BigInteger("1000"), "1E3"); 76 77 df.applyPattern("0.###"); 78 79 df.setDecimalSeparatorAlwaysShown(true); 80 checkFormat(new Double(0.0), "0."); 81 checkFormat(new Double(10.0), "10."); 82 checkFormat(new Double(1000.0), "1000."); 83 checkFormat(new Long(0), "0."); 84 checkFormat(new Long(10), "10."); 85 checkFormat(new Long(1000), "1000."); 86 checkFormat(new BigDecimal("0.0"), "0."); 87 checkFormat(new BigDecimal("10.0"), "10."); 88 checkFormat(new BigDecimal("1000.0"), "1000."); 89 checkFormat(new BigInteger("0"), "0."); 90 checkFormat(new BigInteger("10"), "10."); 91 checkFormat(new BigInteger("1000"), "1000."); 92 93 df.setDecimalSeparatorAlwaysShown(false); 94 checkFormat(new Double(0.0), "0"); 95 checkFormat(new Double(10.0), "10"); 96 checkFormat(new Double(1000.0), "1000"); 97 checkFormat(new Long(0), "0"); 98 checkFormat(new Long(10), "10"); 99 checkFormat(new Long(1000), "1000"); 100 checkFormat(new BigDecimal("0.0"), "0"); 101 checkFormat(new BigDecimal("10.0"), "10"); 102 checkFormat(new BigDecimal("1000.0"), "1000"); 103 checkFormat(new BigInteger("0"), "0"); 104 checkFormat(new BigInteger("10"), "10"); 105 checkFormat(new BigInteger("1000"), "1000"); 106 107 Locale.setDefault(defaultLoc); 108 109 if (err) { 110 throw new RuntimeException("Wrong format/parse with DecimalFormat"); 111 } 112 } 113 114 static void checkFormat(Number num, String expected) { 115 String got = df.format(num); 116 if (!got.equals(expected)) { 117 err = true; 118 System.err.println(" DecimalFormat format(" + 119 num.getClass().getName() + | 30 import java.math.*; 31 import java.text.*; 32 import java.util.*; 33 34 public class Bug4208135 { 35 36 static DecimalFormat df; 37 38 static boolean err = false; 39 40 static public void main(String[] args){ 41 42 Locale defaultLoc = Locale.getDefault(); 43 Locale.setDefault(Locale.US); 44 45 df = new DecimalFormat(); 46 47 df.applyPattern("0.#E0"); 48 49 df.setDecimalSeparatorAlwaysShown(true); 50 checkFormat(0.0, "0.E0"); 51 checkFormat(10.0, "1.E1"); 52 checkFormat(1000.0, "1.E3"); 53 checkFormat(0L, "0.E0"); 54 checkFormat(10L, "1.E1"); 55 checkFormat(1000L, "1.E3"); 56 checkFormat(new BigDecimal("0.0"), "0.E0"); 57 checkFormat(new BigDecimal("10.0"), "1.E1"); 58 checkFormat(new BigDecimal("1000.0"), "1.E3"); 59 checkFormat(new BigInteger("00"), "0.E0"); 60 checkFormat(new BigInteger("10"), "1.E1"); 61 checkFormat(new BigInteger("1000"), "1.E3"); 62 63 df.setDecimalSeparatorAlwaysShown(false); 64 checkFormat(0.0, "0E0"); 65 checkFormat(10.0, "1E1"); 66 checkFormat(1000.0, "1E3"); 67 checkFormat(0L, "0E0"); 68 checkFormat(10L, "1E1"); 69 checkFormat(1000L, "1E3"); 70 checkFormat(new BigDecimal("0.0"), "0E0"); 71 checkFormat(new BigDecimal("10.0"), "1E1"); 72 checkFormat(new BigDecimal("1000.0"), "1E3"); 73 checkFormat(new BigInteger("0"), "0E0"); 74 checkFormat(new BigInteger("10"), "1E1"); 75 checkFormat(new BigInteger("1000"), "1E3"); 76 77 df.applyPattern("0.###"); 78 79 df.setDecimalSeparatorAlwaysShown(true); 80 checkFormat(0.0, "0."); 81 checkFormat(10.0, "10."); 82 checkFormat(1000.0, "1000."); 83 checkFormat(0L, "0."); 84 checkFormat(10L, "10."); 85 checkFormat(1000L, "1000."); 86 checkFormat(new BigDecimal("0.0"), "0."); 87 checkFormat(new BigDecimal("10.0"), "10."); 88 checkFormat(new BigDecimal("1000.0"), "1000."); 89 checkFormat(new BigInteger("0"), "0."); 90 checkFormat(new BigInteger("10"), "10."); 91 checkFormat(new BigInteger("1000"), "1000."); 92 93 df.setDecimalSeparatorAlwaysShown(false); 94 checkFormat(0.0, "0"); 95 checkFormat(10.0, "10"); 96 checkFormat(1000.0, "1000"); 97 checkFormat(0L, "0"); 98 checkFormat(10L, "10"); 99 checkFormat(1000L, "1000"); 100 checkFormat(new BigDecimal("0.0"), "0"); 101 checkFormat(new BigDecimal("10.0"), "10"); 102 checkFormat(new BigDecimal("1000.0"), "1000"); 103 checkFormat(new BigInteger("0"), "0"); 104 checkFormat(new BigInteger("10"), "10"); 105 checkFormat(new BigInteger("1000"), "1000"); 106 107 Locale.setDefault(defaultLoc); 108 109 if (err) { 110 throw new RuntimeException("Wrong format/parse with DecimalFormat"); 111 } 112 } 113 114 static void checkFormat(Number num, String expected) { 115 String got = df.format(num); 116 if (!got.equals(expected)) { 117 err = true; 118 System.err.println(" DecimalFormat format(" + 119 num.getClass().getName() + |