1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package tests.support; 19 20 import java.text.DecimalFormat; 21 import java.text.NumberFormat; 22 import java.util.Locale; 23 import java.util.Vector; 24 25 public class Support_DecimalFormat extends Support_Format { 26 Support_DecimalFormat(String p1)27 public Support_DecimalFormat(String p1) { 28 super(p1); 29 } 30 31 @Override runTest()32 public void runTest() { 33 t_formatToCharacterIterator(); 34 t_format_with_FieldPosition(); 35 } 36 main(String[] args)37 public static void main(String[] args) { 38 new Support_DecimalFormat("").runTest(); 39 } 40 t_format_with_FieldPosition()41 public void t_format_with_FieldPosition() { 42 DecimalFormat format = (DecimalFormat) NumberFormat 43 .getCurrencyInstance(Locale.US); 44 Number number = new Double(10000000.76); 45 String text = "$10,000,000.76"; 46 47 t_FormatWithField(0, format, number, text, NumberFormat.Field.CURRENCY, 48 0, 1); 49 t_FormatWithField(1, format, number, text, NumberFormat.Field.INTEGER, 50 1, 11); 51 t_FormatWithField(2, format, number, text, 52 NumberFormat.Field.GROUPING_SEPARATOR, 3, 4); 53 t_FormatWithField(3, format, number, text, 54 NumberFormat.Field.DECIMAL_SEPARATOR, 11, 12); 55 t_FormatWithField(4, format, number, text, NumberFormat.Field.FRACTION, 56 12, 14); 57 58 // test fields that are not included in the formatted text 59 t_FormatWithField(5, format, number, text, NumberFormat.Field.SIGN, 0, 60 0); 61 t_FormatWithField(6, format, number, text, NumberFormat.Field.EXPONENT, 62 0, 0); 63 t_FormatWithField(7, format, number, text, 64 NumberFormat.Field.EXPONENT_SIGN, 0, 0); 65 t_FormatWithField(8, format, number, text, 66 NumberFormat.Field.EXPONENT_SYMBOL, 0, 0); 67 t_FormatWithField(9, format, number, text, NumberFormat.Field.PERCENT, 68 0, 0); 69 t_FormatWithField(10, format, number, text, 70 NumberFormat.Field.PERMILLE, 0, 0); 71 72 // test Exponential 73 format = new DecimalFormat("000000000.0#E0"); 74 text = "100000007.6E-1"; 75 t_FormatWithField(11, format, number, text, NumberFormat.Field.INTEGER, 76 0, 9); 77 t_FormatWithField(12, format, number, text, 78 NumberFormat.Field.DECIMAL_SEPARATOR, 9, 10); 79 t_FormatWithField(13, format, number, text, 80 NumberFormat.Field.FRACTION, 10, 11); 81 t_FormatWithField(14, format, number, text, 82 NumberFormat.Field.EXPONENT_SYMBOL, 11, 12); 83 t_FormatWithField(15, format, number, text, 84 NumberFormat.Field.EXPONENT_SIGN, 12, 13); 85 t_FormatWithField(16, format, number, text, 86 NumberFormat.Field.EXPONENT, 13, 14); 87 88 // test fields that are not included in the formatted text 89 t_FormatWithField(17, format, number, text, 90 NumberFormat.Field.GROUPING_SEPARATOR, 0, 0); 91 t_FormatWithField(18, format, number, text, NumberFormat.Field.SIGN, 0, 92 0); 93 t_FormatWithField(19, format, number, text, 94 NumberFormat.Field.CURRENCY, 0, 0); 95 t_FormatWithField(20, format, number, text, NumberFormat.Field.PERCENT, 96 0, 0); 97 t_FormatWithField(21, format, number, text, 98 NumberFormat.Field.PERMILLE, 0, 0); 99 100 // test currency instance with TR Locale 101 number = new Double(350.76); 102 format = (DecimalFormat) NumberFormat.getCurrencyInstance(new Locale( 103 "de", "CH")); 104 text = "SFr. 350.76"; 105 t_FormatWithField(24, format, number, text, 106 NumberFormat.Field.CURRENCY, 0, 4); 107 t_FormatWithField(22, format, number, text, NumberFormat.Field.INTEGER, 108 5, 8); 109 t_FormatWithField(23, format, number, text, 110 NumberFormat.Field.DECIMAL_SEPARATOR, 8, 9); 111 t_FormatWithField(23, format, number, text, 112 NumberFormat.Field.FRACTION, 9, 11); 113 114 // test fields that are not included in the formatted text 115 t_FormatWithField(25, format, number, text, 116 NumberFormat.Field.GROUPING_SEPARATOR, 0, 0); 117 t_FormatWithField(27, format, number, text, NumberFormat.Field.SIGN, 0, 118 0); 119 t_FormatWithField(28, format, number, text, 120 NumberFormat.Field.EXPONENT, 0, 0); 121 t_FormatWithField(29, format, number, text, 122 NumberFormat.Field.EXPONENT_SIGN, 0, 0); 123 t_FormatWithField(30, format, number, text, 124 NumberFormat.Field.EXPONENT_SYMBOL, 0, 0); 125 t_FormatWithField(31, format, number, text, NumberFormat.Field.PERCENT, 126 0, 0); 127 t_FormatWithField(32, format, number, text, 128 NumberFormat.Field.PERMILLE, 0, 0); 129 130 } 131 t_formatToCharacterIterator()132 public void t_formatToCharacterIterator() { 133 134 Number number = new Double(350.76); 135 Number longNumber = new Long(100300400); 136 Number zeroNumber = new Long(0); 137 Number negativeNumber = new Double(-350.76); 138 139 Locale us = Locale.US; 140 Locale tr = new Locale("de", "CH"); 141 142 // test number instance 143 t_Format(1, number, NumberFormat.getNumberInstance(us), getNumberVectorUS()); 144 145 // test integer instance 146 t_Format(2, number, NumberFormat.getIntegerInstance(us), getIntegerVectorUS()); 147 148 // test percent instance 149 t_Format(3, number, NumberFormat.getPercentInstance(us), getPercentVectorUS()); 150 151 // test currency instance with US Locale 152 t_Format(4, number, NumberFormat.getCurrencyInstance(us), getPositiveCurrencyVectorUS()); 153 154 // test negative currency instance with US Locale 155 t_Format(5, negativeNumber, NumberFormat.getCurrencyInstance(us), getNegativeCurrencyVectorUS()); 156 157 // test multiple grouping separators 158 t_Format(6, longNumber, NumberFormat.getNumberInstance(us), getNumberVector2US()); 159 160 // test 0 161 t_Format(7, zeroNumber, NumberFormat.getNumberInstance(us), getZeroVector()); 162 163 // test permille pattern 164 DecimalFormat format = new DecimalFormat("###0.##\u2030"); 165 t_Format(8, number, format, getPermilleVector()); 166 167 // test exponential pattern with positive exponent 168 format = new DecimalFormat("00.0#E0"); 169 t_Format(9, number, format, getPositiveExponentVector()); 170 171 // test exponential pattern with negative exponent 172 format = new DecimalFormat("0000.0#E0"); 173 t_Format(10, number, format, getNegativeExponentVector()); 174 175 // test currency instance with TR Locale 176 t_Format(11, number, NumberFormat.getCurrencyInstance(tr), getPositiveCurrencyVectorCH()); 177 178 // test negative currency instance with TR Locale 179 t_Format(12, negativeNumber, NumberFormat.getCurrencyInstance(tr), getNegativeCurrencyVectorCH()); 180 } 181 getNumberVectorUS()182 private static Vector<FieldContainer> getNumberVectorUS() { 183 Vector<FieldContainer> v = new Vector<FieldContainer>(); 184 v.add(new FieldContainer(0, 3, NumberFormat.Field.INTEGER)); 185 v.add(new FieldContainer(3, 4, NumberFormat.Field.DECIMAL_SEPARATOR)); 186 v.add(new FieldContainer(4, 6, NumberFormat.Field.FRACTION)); 187 return v; 188 } 189 getIntegerVectorUS()190 private static Vector<FieldContainer> getIntegerVectorUS() { 191 Vector<FieldContainer> v = new Vector<FieldContainer>(); 192 v.add(new FieldContainer(0, 3, NumberFormat.Field.INTEGER)); 193 return v; 194 } 195 getPositiveCurrencyVectorCH()196 private static Vector<FieldContainer> getPositiveCurrencyVectorCH() { 197 Vector<FieldContainer> v = new Vector<FieldContainer>(); 198 v.add(new FieldContainer(0, 4, NumberFormat.Field.CURRENCY)); 199 v.add(new FieldContainer(5, 8, NumberFormat.Field.INTEGER)); 200 v.add(new FieldContainer(8, 9, NumberFormat.Field.DECIMAL_SEPARATOR)); 201 v.add(new FieldContainer(9, 11, NumberFormat.Field.FRACTION)); 202 return v; 203 } 204 getNegativeCurrencyVectorCH()205 private static Vector<FieldContainer> getNegativeCurrencyVectorCH() { 206 Vector<FieldContainer> v = new Vector<FieldContainer>(); 207 v.add(new FieldContainer(0, 4, NumberFormat.Field.CURRENCY)); 208 v.add(new FieldContainer(4, 5, NumberFormat.Field.SIGN)); 209 v.add(new FieldContainer(5, 8, NumberFormat.Field.INTEGER)); 210 v.add(new FieldContainer(8, 9, NumberFormat.Field.DECIMAL_SEPARATOR)); 211 v.add(new FieldContainer(9, 11, NumberFormat.Field.FRACTION)); 212 return v; 213 } 214 getPositiveCurrencyVectorUS()215 private static Vector<FieldContainer> getPositiveCurrencyVectorUS() { 216 Vector<FieldContainer> v = new Vector<FieldContainer>(); 217 v.add(new FieldContainer(0, 1, NumberFormat.Field.CURRENCY)); 218 v.add(new FieldContainer(1, 4, NumberFormat.Field.INTEGER)); 219 v.add(new FieldContainer(4, 5, NumberFormat.Field.DECIMAL_SEPARATOR)); 220 v.add(new FieldContainer(5, 7, NumberFormat.Field.FRACTION)); 221 return v; 222 } 223 getNegativeCurrencyVectorUS()224 private static Vector<FieldContainer> getNegativeCurrencyVectorUS() { 225 Vector<FieldContainer> v = new Vector<FieldContainer>(); 226 v.add(new FieldContainer(1, 2, NumberFormat.Field.CURRENCY)); 227 v.add(new FieldContainer(2, 5, NumberFormat.Field.INTEGER)); 228 v.add(new FieldContainer(5, 6, NumberFormat.Field.DECIMAL_SEPARATOR)); 229 v.add(new FieldContainer(6, 8, NumberFormat.Field.FRACTION)); 230 return v; 231 } 232 getPercentVectorUS()233 private static Vector<FieldContainer> getPercentVectorUS() { 234 Vector<FieldContainer> v = new Vector<FieldContainer>(); 235 v.add(new FieldContainer(0, 2, NumberFormat.Field.INTEGER)); 236 v.add(new FieldContainer(2, 3, NumberFormat.Field.INTEGER)); 237 v.add(new FieldContainer(2, 3, NumberFormat.Field.GROUPING_SEPARATOR)); 238 v.add(new FieldContainer(3, 6, NumberFormat.Field.INTEGER)); 239 v.add(new FieldContainer(6, 7, NumberFormat.Field.PERCENT)); 240 return v; 241 } 242 getPermilleVector()243 private static Vector<FieldContainer> getPermilleVector() { 244 Vector<FieldContainer> v = new Vector<FieldContainer>(); 245 v.add(new FieldContainer(0, 6, NumberFormat.Field.INTEGER)); 246 v.add(new FieldContainer(6, 7, NumberFormat.Field.PERMILLE)); 247 return v; 248 } 249 getNegativeExponentVector()250 private static Vector<FieldContainer> getNegativeExponentVector() { 251 Vector<FieldContainer> v = new Vector<FieldContainer>(); 252 v.add(new FieldContainer(0, 4, NumberFormat.Field.INTEGER)); 253 v.add(new FieldContainer(4, 5, NumberFormat.Field.DECIMAL_SEPARATOR)); 254 v.add(new FieldContainer(5, 6, NumberFormat.Field.FRACTION)); 255 v.add(new FieldContainer(6, 7, NumberFormat.Field.EXPONENT_SYMBOL)); 256 v.add(new FieldContainer(7, 8, NumberFormat.Field.EXPONENT_SIGN)); 257 v.add(new FieldContainer(8, 9, NumberFormat.Field.EXPONENT)); 258 return v; 259 } 260 getPositiveExponentVector()261 private static Vector<FieldContainer> getPositiveExponentVector() { 262 Vector<FieldContainer> v = new Vector<FieldContainer>(); 263 v.add(new FieldContainer(0, 2, NumberFormat.Field.INTEGER)); 264 v.add(new FieldContainer(2, 3, NumberFormat.Field.DECIMAL_SEPARATOR)); 265 v.add(new FieldContainer(3, 5, NumberFormat.Field.FRACTION)); 266 v.add(new FieldContainer(5, 6, NumberFormat.Field.EXPONENT_SYMBOL)); 267 v.add(new FieldContainer(6, 7, NumberFormat.Field.EXPONENT)); 268 return v; 269 } 270 getNumberVector2US()271 private static Vector<FieldContainer> getNumberVector2US() { 272 Vector<FieldContainer> v = new Vector<FieldContainer>(); 273 v.add(new FieldContainer(0, 3, NumberFormat.Field.INTEGER)); 274 v.add(new FieldContainer(3, 4, NumberFormat.Field.GROUPING_SEPARATOR)); 275 v.add(new FieldContainer(3, 4, NumberFormat.Field.INTEGER)); 276 v.add(new FieldContainer(4, 7, NumberFormat.Field.INTEGER)); 277 v.add(new FieldContainer(7, 8, NumberFormat.Field.GROUPING_SEPARATOR)); 278 v.add(new FieldContainer(7, 8, NumberFormat.Field.INTEGER)); 279 v.add(new FieldContainer(8, 11, NumberFormat.Field.INTEGER)); 280 return v; 281 } 282 getZeroVector()283 private static Vector<FieldContainer> getZeroVector() { 284 Vector<FieldContainer> v = new Vector<FieldContainer>(); 285 v.add(new FieldContainer(0, 1, NumberFormat.Field.INTEGER)); 286 return v; 287 } 288 289 } 290