2 /*****************************************************************************************
\r
4 * Copyright (C) 1996-2009, International Business Machines
\r
5 * Corporation and others. All Rights Reserved.
\r
9 * Port From: JDK 1.4b1 : java.text.Format.IntlTestDecimalFormatAPI
\r
10 * Source File: java/text/format/IntlTestDecimalFormatAPI.java
\r
15 @summary test International Decimal Format API
\r
18 package com.ibm.icu.dev.test.format;
\r
20 import com.ibm.icu.text.*;
\r
21 import com.ibm.icu.math.BigDecimal;
\r
22 import com.ibm.icu.math.MathContext;
\r
23 import java.util.Locale;
\r
24 import java.text.ParsePosition;
\r
25 import java.text.Format;
\r
26 import java.text.FieldPosition;
\r
27 import java.text.ParseException;
\r
28 import com.ibm.icu.text.DecimalFormat;
\r
30 public class IntlTestDecimalFormatAPI extends com.ibm.icu.dev.test.TestFmwk
\r
32 public static void main(String[] args) throws Exception {
\r
33 new IntlTestDecimalFormatAPI().run(args);
\r
37 * Problem 1: simply running
\r
38 * decF4.setRoundingMode(java.math.BigDecimal.ROUND_HALF_UP) does not work
\r
39 * as decF4.setRoundingIncrement(.0001) must also be run.
\r
40 * Problem 2: decF4.format(8.88885) does not return 8.8889 as expected.
\r
41 * You must run decF4.format(new BigDecimal(Double.valueOf(8.88885))) in
\r
42 * order for this to work as expected.
\r
43 * Problem 3: There seems to be no way to set half up to be the default
\r
45 * We solved the problem with the code at the bottom of this page however
\r
46 * this is not quite general purpose enough to include in icu4j. A static
\r
47 * setDefaultRoundingMode function would solve the problem nicely. Also
\r
48 * decimal places past 20 are not handled properly. A small ammount of work
\r
49 * would make bring this up to snuff.
\r
51 public void testJB1871()
\r
54 double number = 8.88885;
\r
55 String expected = "8.8889";
\r
57 String pat = ",##0.0000";
\r
58 DecimalFormat dec = new DecimalFormat(pat);
\r
59 dec.setRoundingMode(BigDecimal.ROUND_HALF_UP);
\r
60 double roundinginc = 0.0001;
\r
61 dec.setRoundingIncrement(roundinginc);
\r
62 String str = dec.format(number);
\r
63 if (!str.equals(expected)) {
\r
64 errln("Fail: " + number + " x \"" + pat + "\" = \"" +
\r
65 str + "\", expected \"" + expected + "\"");
\r
69 dec = new DecimalFormat(pat);
\r
70 dec.setRoundingMode(BigDecimal.ROUND_HALF_UP);
\r
71 str = dec.format(number);
\r
72 if (!str.equals(expected)) {
\r
73 errln("Fail: " + number + " x \"" + pat + "\" = \"" +
\r
74 str + "\", expected \"" + expected + "\"");
\r
77 // testing 20 decimal places
\r
78 pat = ",##0.00000000000000000001";
\r
79 dec = new DecimalFormat(pat);
\r
80 BigDecimal bignumber = new BigDecimal("8.888888888888888888885");
\r
81 expected = "8.88888888888888888889";
\r
83 dec.setRoundingMode(BigDecimal.ROUND_HALF_UP);
\r
84 str = dec.format(bignumber);
\r
85 if (!str.equals(expected)) {
\r
86 errln("Fail: " + bignumber + " x \"" + pat + "\" = \"" +
\r
87 str + "\", expected \"" + expected + "\"");
\r
93 * This test checks various generic API methods in DecimalFormat to achieve
\r
94 * 100% API coverage.
\r
96 public void TestAPI()
\r
98 logln("DecimalFormat API test---"); logln("");
\r
99 Locale.setDefault(Locale.ENGLISH);
\r
101 // ======= Test constructors
\r
103 logln("Testing DecimalFormat constructors");
\r
105 DecimalFormat def = new DecimalFormat();
\r
107 final String pattern = new String("#,##0.# FF");
\r
108 DecimalFormat pat = null;
\r
110 pat = new DecimalFormat(pattern);
\r
112 catch (IllegalArgumentException e) {
\r
113 errln("ERROR: Could not create DecimalFormat (pattern)");
\r
116 DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.FRENCH);
\r
118 DecimalFormat cust1 = new DecimalFormat(pattern, symbols);
\r
120 // ======= Test clone(), assignment, and equality
\r
122 logln("Testing clone() and equality operators");
\r
124 Format clone = (Format) def.clone();
\r
125 if( ! def.equals(clone)) {
\r
126 errln("ERROR: Clone() failed");
\r
129 // ======= Test various format() methods
\r
131 logln("Testing various format() methods");
\r
133 // final double d = -10456.0037; // this appears as -10456.003700000001 on NT
\r
134 // final double d = -1.04560037e-4; // this appears as -1.0456003700000002E-4 on NT
\r
135 final double d = -10456.00370000000000; // this works!
\r
136 final long l = 100000000;
\r
137 logln("" + d + " is the double value");
\r
139 StringBuffer res1 = new StringBuffer();
\r
140 StringBuffer res2 = new StringBuffer();
\r
141 StringBuffer res3 = new StringBuffer();
\r
142 StringBuffer res4 = new StringBuffer();
\r
143 FieldPosition pos1 = new FieldPosition(0);
\r
144 FieldPosition pos2 = new FieldPosition(0);
\r
145 FieldPosition pos3 = new FieldPosition(0);
\r
146 FieldPosition pos4 = new FieldPosition(0);
\r
148 res1 = def.format(d, res1, pos1);
\r
149 logln("" + d + " formatted to " + res1);
\r
151 res2 = pat.format(l, res2, pos2);
\r
152 logln("" + l + " formatted to " + res2);
\r
154 res3 = cust1.format(d, res3, pos3);
\r
155 logln("" + d + " formatted to " + res3);
\r
157 res4 = cust1.format(l, res4, pos4);
\r
158 logln("" + l + " formatted to " + res4);
\r
160 // ======= Test parse()
\r
162 logln("Testing parse()");
\r
164 String text = new String("-10,456.0037");
\r
165 ParsePosition pos = new ParsePosition(0);
\r
166 String patt = new String("#,##0.#");
\r
167 pat.applyPattern(patt);
\r
168 double d2 = pat.parse(text, pos).doubleValue();
\r
170 errln("ERROR: Roundtrip failed (via parse(" + d2 + " != " + d + ")) for " + text);
\r
172 logln(text + " parsed into " + (long) d2);
\r
174 // ======= Test getters and setters
\r
176 logln("Testing getters and setters");
\r
178 final DecimalFormatSymbols syms = pat.getDecimalFormatSymbols();
\r
179 def.setDecimalFormatSymbols(syms);
\r
180 if( ! pat.getDecimalFormatSymbols().equals(def.getDecimalFormatSymbols())) {
\r
181 errln("ERROR: set DecimalFormatSymbols() failed");
\r
185 pat.setPositivePrefix("+");
\r
186 posPrefix = pat.getPositivePrefix();
\r
187 logln("Positive prefix (should be +): " + posPrefix);
\r
188 if(posPrefix != "+") {
\r
189 errln("ERROR: setPositivePrefix() failed");
\r
193 pat.setNegativePrefix("-");
\r
194 negPrefix = pat.getNegativePrefix();
\r
195 logln("Negative prefix (should be -): " + negPrefix);
\r
196 if(negPrefix != "-") {
\r
197 errln("ERROR: setNegativePrefix() failed");
\r
201 pat.setPositiveSuffix("_");
\r
202 posSuffix = pat.getPositiveSuffix();
\r
203 logln("Positive suffix (should be _): " + posSuffix);
\r
204 if(posSuffix != "_") {
\r
205 errln("ERROR: setPositiveSuffix() failed");
\r
209 pat.setNegativeSuffix("~");
\r
210 negSuffix = pat.getNegativeSuffix();
\r
211 logln("Negative suffix (should be ~): " + negSuffix);
\r
212 if(negSuffix != "~") {
\r
213 errln("ERROR: setNegativeSuffix() failed");
\r
216 long multiplier = 0;
\r
217 pat.setMultiplier(8);
\r
218 multiplier = pat.getMultiplier();
\r
219 logln("Multiplier (should be 8): " + multiplier);
\r
220 if(multiplier != 8) {
\r
221 errln("ERROR: setMultiplier() failed");
\r
224 int groupingSize = 0;
\r
225 pat.setGroupingSize(2);
\r
226 groupingSize = pat.getGroupingSize();
\r
227 logln("Grouping size (should be 2): " + (long) groupingSize);
\r
228 if(groupingSize != 2) {
\r
229 errln("ERROR: setGroupingSize() failed");
\r
232 pat.setDecimalSeparatorAlwaysShown(true);
\r
233 boolean tf = pat.isDecimalSeparatorAlwaysShown();
\r
234 logln("DecimalSeparatorIsAlwaysShown (should be true) is " + (tf ? "true" : "false"));
\r
236 errln("ERROR: setDecimalSeparatorAlwaysShown() failed");
\r
240 funkyPat = pat.toPattern();
\r
241 logln("Pattern is " + funkyPat);
\r
244 locPat = pat.toLocalizedPattern();
\r
245 logln("Localized pattern is " + locPat);
\r
247 // ======= Test applyPattern()
\r
249 logln("Testing applyPattern()");
\r
251 String p1 = new String("#,##0.0#;(#,##0.0#)");
\r
252 logln("Applying pattern " + p1);
\r
253 pat.applyPattern(p1);
\r
255 s2 = pat.toPattern();
\r
256 logln("Extracted pattern is " + s2);
\r
257 if( ! s2.equals(p1) ) {
\r
258 errln("ERROR: toPattern() result did not match pattern applied");
\r
261 String p2 = new String("#,##0.0# FF;(#,##0.0# FF)");
\r
262 logln("Applying pattern " + p2);
\r
263 pat.applyLocalizedPattern(p2);
\r
265 s3 = pat.toLocalizedPattern();
\r
266 logln("Extracted pattern is " + s3);
\r
267 if( ! s3.equals(p2) ) {
\r
268 errln("ERROR: toLocalizedPattern() result did not match pattern applied");
\r
272 //#if defined(FOUNDATION10) || defined(J2SE13)
\r
274 public void testJB6134()
\r
276 DecimalFormat decfmt = new DecimalFormat();
\r
277 StringBuffer buf = new StringBuffer();
\r
279 FieldPosition fposByInt = new FieldPosition(NumberFormat.INTEGER_FIELD);
\r
280 decfmt.format(123, buf, fposByInt);
\r
283 FieldPosition fposByField = new FieldPosition(NumberFormat.Field.INTEGER);
\r
284 decfmt.format(123, buf, fposByField);
\r
286 if (fposByInt.getEndIndex() != fposByField.getEndIndex())
\r
288 errln("ERROR: End index for integer field - fposByInt:" + fposByInt.getEndIndex() +
\r
289 " / fposByField: " + fposByField.getEndIndex());
\r
294 public void testJB4971()
\r
296 DecimalFormat decfmt = new DecimalFormat();
\r
297 MathContext resultICU;
\r
299 MathContext comp1 = new MathContext(0, MathContext.PLAIN);
\r
300 resultICU = decfmt.getMathContextICU();
\r
301 if ((comp1.getDigits() != resultICU.getDigits()) ||
\r
302 (comp1.getForm() != resultICU.getForm()) ||
\r
303 (comp1.getLostDigits() != resultICU.getLostDigits()) ||
\r
304 (comp1.getRoundingMode() != resultICU.getRoundingMode()))
\r
306 errln("ERROR: Math context 1 not equal - result: " + resultICU.toString() +
\r
307 " / expected: " + comp1.toString());
\r
310 MathContext comp2 = new MathContext(5, MathContext.ENGINEERING);
\r
311 decfmt.setMathContextICU(comp2);
\r
312 resultICU = decfmt.getMathContextICU();
\r
313 if ((comp2.getDigits() != resultICU.getDigits()) ||
\r
314 (comp2.getForm() != resultICU.getForm()) ||
\r
315 (comp2.getLostDigits() != resultICU.getLostDigits()) ||
\r
316 (comp2.getRoundingMode() != resultICU.getRoundingMode()))
\r
318 errln("ERROR: Math context 2 not equal - result: " + resultICU.toString() +
\r
319 " / expected: " + comp2.toString());
\r
322 //#if defined(FOUNDATION10) || defined(J2SE13) || defined(J2SE14)
\r
325 java.math.MathContext result;
\r
327 java.math.MathContext comp3 = new java.math.MathContext(3, java.math.RoundingMode.DOWN);
\r
328 decfmt.setMathContext(comp3);
\r
329 result = decfmt.getMathContext();
\r
330 if ((comp3.getPrecision() != result.getPrecision()) ||
\r
331 (comp3.getRoundingMode() != result.getRoundingMode()))
\r
333 errln("ERROR: Math context 3 not equal - result: " + result.toString() +
\r
334 " / expected: " + comp3.toString());
\r
341 public void testJB6354()
\r
343 DecimalFormat pat = new DecimalFormat("#,##0.00");
\r
345 // get default rounding increment
\r
346 //#if defined(FOUNDATION10)
\r
347 //## com.ibm.icu.math.BigDecimal r1 = pat.getRoundingIncrement();
\r
349 java.math.BigDecimal r1 = pat.getRoundingIncrement();
\r
352 // set rounding mode with zero increment. Rounding
\r
353 // increment should be set by this operation
\r
354 pat.setRoundingMode(BigDecimal.ROUND_UP);
\r
355 //#if defined(FOUNDATION10)
\r
356 //## com.ibm.icu.math.BigDecimal r2 = pat.getRoundingIncrement();
\r
358 java.math.BigDecimal r2 = pat.getRoundingIncrement();
\r
361 // check for different values
\r
362 if ((r1 != null) && (r2 != null))
\r
364 if (r1.compareTo(r2) == 0)
\r
366 errln("ERROR: Rounding increment did not change");
\r
371 public void testJB6648()
\r
373 DecimalFormat df = new DecimalFormat();
\r
374 df.setParseStrict(true);
\r
376 String numstr = new String();
\r
378 String[] patterns = {
\r
387 for(int i=0; i < patterns.length; i++) {
\r
388 df.applyPattern(patterns[i]);
\r
389 numstr = df.format(5);
\r
391 Number n = df.parse(numstr);
\r
392 logln("INFO: Parsed " + numstr + " -> " + n);
\r
393 } catch (ParseException pe) {
\r
394 errln("ERROR: Failed round trip with strict parsing.");
\r
398 df.applyPattern(patterns[1]);
\r
401 Number n = df.parse(numstr);
\r
402 errln("ERROR: Expected round trip failure not encountered: numstr -> " + n);
\r
403 } catch (ParseException pe) {
\r
404 logln("INFO: Expected ParseExpection for " + numstr + " with strick parse enabled");
\r