1 /*****************************************************************************************
\r
3 * Copyright (C) 1996-2009, International Business Machines
\r
4 * Corporation and others. All Rights Reserved.
\r
8 * Port From: JDK 1.4b1 : java.text.Format.IntlTestDecimalFormatAPI
\r
9 * Source File: java/text/format/IntlTestDecimalFormatAPI.java
\r
14 @summary test International Decimal Format API
\r
17 package com.ibm.icu.dev.test.format;
\r
19 import java.text.FieldPosition;
\r
20 import java.text.Format;
\r
21 import java.text.ParseException;
\r
22 import java.text.ParsePosition;
\r
23 import java.util.Locale;
\r
25 import com.ibm.icu.math.BigDecimal;
\r
26 import com.ibm.icu.math.MathContext;
\r
27 import com.ibm.icu.text.DecimalFormat;
\r
28 import com.ibm.icu.text.DecimalFormatSymbols;
\r
29 import com.ibm.icu.text.NumberFormat;
\r
31 public class IntlTestDecimalFormatAPI extends com.ibm.icu.dev.test.TestFmwk
\r
33 public static void main(String[] args) throws Exception {
\r
34 new IntlTestDecimalFormatAPI().run(args);
\r
38 * Problem 1: simply running
\r
39 * decF4.setRoundingMode(java.math.BigDecimal.ROUND_HALF_UP) does not work
\r
40 * as decF4.setRoundingIncrement(.0001) must also be run.
\r
41 * Problem 2: decF4.format(8.88885) does not return 8.8889 as expected.
\r
42 * You must run decF4.format(new BigDecimal(Double.valueOf(8.88885))) in
\r
43 * order for this to work as expected.
\r
44 * Problem 3: There seems to be no way to set half up to be the default
\r
46 * We solved the problem with the code at the bottom of this page however
\r
47 * this is not quite general purpose enough to include in icu4j. A static
\r
48 * setDefaultRoundingMode function would solve the problem nicely. Also
\r
49 * decimal places past 20 are not handled properly. A small ammount of work
\r
50 * would make bring this up to snuff.
\r
52 public void testJB1871()
\r
55 double number = 8.88885;
\r
56 String expected = "8.8889";
\r
58 String pat = ",##0.0000";
\r
59 DecimalFormat dec = new DecimalFormat(pat);
\r
60 dec.setRoundingMode(BigDecimal.ROUND_HALF_UP);
\r
61 double roundinginc = 0.0001;
\r
62 dec.setRoundingIncrement(roundinginc);
\r
63 String str = dec.format(number);
\r
64 if (!str.equals(expected)) {
\r
65 errln("Fail: " + number + " x \"" + pat + "\" = \"" +
\r
66 str + "\", expected \"" + expected + "\"");
\r
70 dec = new DecimalFormat(pat);
\r
71 dec.setRoundingMode(BigDecimal.ROUND_HALF_UP);
\r
72 str = dec.format(number);
\r
73 if (!str.equals(expected)) {
\r
74 errln("Fail: " + number + " x \"" + pat + "\" = \"" +
\r
75 str + "\", expected \"" + expected + "\"");
\r
78 // testing 20 decimal places
\r
79 pat = ",##0.00000000000000000001";
\r
80 dec = new DecimalFormat(pat);
\r
81 BigDecimal bignumber = new BigDecimal("8.888888888888888888885");
\r
82 expected = "8.88888888888888888889";
\r
84 dec.setRoundingMode(BigDecimal.ROUND_HALF_UP);
\r
85 str = dec.format(bignumber);
\r
86 if (!str.equals(expected)) {
\r
87 errln("Fail: " + bignumber + " x \"" + pat + "\" = \"" +
\r
88 str + "\", expected \"" + expected + "\"");
\r
94 * This test checks various generic API methods in DecimalFormat to achieve
\r
95 * 100% API coverage.
\r
97 public void TestAPI()
\r
99 logln("DecimalFormat API test---"); logln("");
\r
100 Locale.setDefault(Locale.ENGLISH);
\r
102 // ======= Test constructors
\r
104 logln("Testing DecimalFormat constructors");
\r
106 DecimalFormat def = new DecimalFormat();
\r
108 final String pattern = new String("#,##0.# FF");
\r
109 DecimalFormat pat = null;
\r
111 pat = new DecimalFormat(pattern);
\r
113 catch (IllegalArgumentException e) {
\r
114 errln("ERROR: Could not create DecimalFormat (pattern)");
\r
117 DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.FRENCH);
\r
119 DecimalFormat cust1 = new DecimalFormat(pattern, symbols);
\r
121 // ======= Test clone(), assignment, and equality
\r
123 logln("Testing clone() and equality operators");
\r
125 Format clone = (Format) def.clone();
\r
126 if( ! def.equals(clone)) {
\r
127 errln("ERROR: Clone() failed");
\r
130 // ======= Test various format() methods
\r
132 logln("Testing various format() methods");
\r
134 // final double d = -10456.0037; // this appears as -10456.003700000001 on NT
\r
135 // final double d = -1.04560037e-4; // this appears as -1.0456003700000002E-4 on NT
\r
136 final double d = -10456.00370000000000; // this works!
\r
137 final long l = 100000000;
\r
138 logln("" + d + " is the double value");
\r
140 StringBuffer res1 = new StringBuffer();
\r
141 StringBuffer res2 = new StringBuffer();
\r
142 StringBuffer res3 = new StringBuffer();
\r
143 StringBuffer res4 = new StringBuffer();
\r
144 FieldPosition pos1 = new FieldPosition(0);
\r
145 FieldPosition pos2 = new FieldPosition(0);
\r
146 FieldPosition pos3 = new FieldPosition(0);
\r
147 FieldPosition pos4 = new FieldPosition(0);
\r
149 res1 = def.format(d, res1, pos1);
\r
150 logln("" + d + " formatted to " + res1);
\r
152 res2 = pat.format(l, res2, pos2);
\r
153 logln("" + l + " formatted to " + res2);
\r
155 res3 = cust1.format(d, res3, pos3);
\r
156 logln("" + d + " formatted to " + res3);
\r
158 res4 = cust1.format(l, res4, pos4);
\r
159 logln("" + l + " formatted to " + res4);
\r
161 // ======= Test parse()
\r
163 logln("Testing parse()");
\r
165 String text = new String("-10,456.0037");
\r
166 ParsePosition pos = new ParsePosition(0);
\r
167 String patt = new String("#,##0.#");
\r
168 pat.applyPattern(patt);
\r
169 double d2 = pat.parse(text, pos).doubleValue();
\r
171 errln("ERROR: Roundtrip failed (via parse(" + d2 + " != " + d + ")) for " + text);
\r
173 logln(text + " parsed into " + (long) d2);
\r
175 // ======= Test getters and setters
\r
177 logln("Testing getters and setters");
\r
179 final DecimalFormatSymbols syms = pat.getDecimalFormatSymbols();
\r
180 def.setDecimalFormatSymbols(syms);
\r
181 if( ! pat.getDecimalFormatSymbols().equals(def.getDecimalFormatSymbols())) {
\r
182 errln("ERROR: set DecimalFormatSymbols() failed");
\r
186 pat.setPositivePrefix("+");
\r
187 posPrefix = pat.getPositivePrefix();
\r
188 logln("Positive prefix (should be +): " + posPrefix);
\r
189 if(posPrefix != "+") {
\r
190 errln("ERROR: setPositivePrefix() failed");
\r
194 pat.setNegativePrefix("-");
\r
195 negPrefix = pat.getNegativePrefix();
\r
196 logln("Negative prefix (should be -): " + negPrefix);
\r
197 if(negPrefix != "-") {
\r
198 errln("ERROR: setNegativePrefix() failed");
\r
202 pat.setPositiveSuffix("_");
\r
203 posSuffix = pat.getPositiveSuffix();
\r
204 logln("Positive suffix (should be _): " + posSuffix);
\r
205 if(posSuffix != "_") {
\r
206 errln("ERROR: setPositiveSuffix() failed");
\r
210 pat.setNegativeSuffix("~");
\r
211 negSuffix = pat.getNegativeSuffix();
\r
212 logln("Negative suffix (should be ~): " + negSuffix);
\r
213 if(negSuffix != "~") {
\r
214 errln("ERROR: setNegativeSuffix() failed");
\r
217 long multiplier = 0;
\r
218 pat.setMultiplier(8);
\r
219 multiplier = pat.getMultiplier();
\r
220 logln("Multiplier (should be 8): " + multiplier);
\r
221 if(multiplier != 8) {
\r
222 errln("ERROR: setMultiplier() failed");
\r
225 int groupingSize = 0;
\r
226 pat.setGroupingSize(2);
\r
227 groupingSize = pat.getGroupingSize();
\r
228 logln("Grouping size (should be 2): " + (long) groupingSize);
\r
229 if(groupingSize != 2) {
\r
230 errln("ERROR: setGroupingSize() failed");
\r
233 pat.setDecimalSeparatorAlwaysShown(true);
\r
234 boolean tf = pat.isDecimalSeparatorAlwaysShown();
\r
235 logln("DecimalSeparatorIsAlwaysShown (should be true) is " + (tf ? "true" : "false"));
\r
237 errln("ERROR: setDecimalSeparatorAlwaysShown() failed");
\r
241 funkyPat = pat.toPattern();
\r
242 logln("Pattern is " + funkyPat);
\r
245 locPat = pat.toLocalizedPattern();
\r
246 logln("Localized pattern is " + locPat);
\r
248 // ======= Test applyPattern()
\r
250 logln("Testing applyPattern()");
\r
252 String p1 = new String("#,##0.0#;(#,##0.0#)");
\r
253 logln("Applying pattern " + p1);
\r
254 pat.applyPattern(p1);
\r
256 s2 = pat.toPattern();
\r
257 logln("Extracted pattern is " + s2);
\r
258 if( ! s2.equals(p1) ) {
\r
259 errln("ERROR: toPattern() result did not match pattern applied");
\r
262 String p2 = new String("#,##0.0# FF;(#,##0.0# FF)");
\r
263 logln("Applying pattern " + p2);
\r
264 pat.applyLocalizedPattern(p2);
\r
266 s3 = pat.toLocalizedPattern();
\r
267 logln("Extracted pattern is " + s3);
\r
268 if( ! s3.equals(p2) ) {
\r
269 errln("ERROR: toLocalizedPattern() result did not match pattern applied");
\r
273 public void testJB6134()
\r
275 DecimalFormat decfmt = new DecimalFormat();
\r
276 StringBuffer buf = new StringBuffer();
\r
278 FieldPosition fposByInt = new FieldPosition(NumberFormat.INTEGER_FIELD);
\r
279 decfmt.format(123, buf, fposByInt);
\r
282 FieldPosition fposByField = new FieldPosition(NumberFormat.Field.INTEGER);
\r
283 decfmt.format(123, buf, fposByField);
\r
285 if (fposByInt.getEndIndex() != fposByField.getEndIndex())
\r
287 errln("ERROR: End index for integer field - fposByInt:" + fposByInt.getEndIndex() +
\r
288 " / fposByField: " + fposByField.getEndIndex());
\r
292 public void testJB4971()
\r
294 DecimalFormat decfmt = new DecimalFormat();
\r
295 MathContext resultICU;
\r
297 MathContext comp1 = new MathContext(0, MathContext.PLAIN);
\r
298 resultICU = decfmt.getMathContextICU();
\r
299 if ((comp1.getDigits() != resultICU.getDigits()) ||
\r
300 (comp1.getForm() != resultICU.getForm()) ||
\r
301 (comp1.getLostDigits() != resultICU.getLostDigits()) ||
\r
302 (comp1.getRoundingMode() != resultICU.getRoundingMode()))
\r
304 errln("ERROR: Math context 1 not equal - result: " + resultICU.toString() +
\r
305 " / expected: " + comp1.toString());
\r
308 MathContext comp2 = new MathContext(5, MathContext.ENGINEERING);
\r
309 decfmt.setMathContextICU(comp2);
\r
310 resultICU = decfmt.getMathContextICU();
\r
311 if ((comp2.getDigits() != resultICU.getDigits()) ||
\r
312 (comp2.getForm() != resultICU.getForm()) ||
\r
313 (comp2.getLostDigits() != resultICU.getLostDigits()) ||
\r
314 (comp2.getRoundingMode() != resultICU.getRoundingMode()))
\r
316 errln("ERROR: Math context 2 not equal - result: " + resultICU.toString() +
\r
317 " / expected: " + comp2.toString());
\r
320 java.math.MathContext result;
\r
322 java.math.MathContext comp3 = new java.math.MathContext(3, java.math.RoundingMode.DOWN);
\r
323 decfmt.setMathContext(comp3);
\r
324 result = decfmt.getMathContext();
\r
325 if ((comp3.getPrecision() != result.getPrecision()) ||
\r
326 (comp3.getRoundingMode() != result.getRoundingMode()))
\r
328 errln("ERROR: Math context 3 not equal - result: " + result.toString() +
\r
329 " / expected: " + comp3.toString());
\r
334 public void testJB6354()
\r
336 DecimalFormat pat = new DecimalFormat("#,##0.00");
\r
338 // get default rounding increment
\r
339 java.math.BigDecimal r1 = pat.getRoundingIncrement();
\r
341 // set rounding mode with zero increment. Rounding
\r
342 // increment should be set by this operation
\r
343 pat.setRoundingMode(BigDecimal.ROUND_UP);
\r
344 java.math.BigDecimal r2 = pat.getRoundingIncrement();
\r
346 // check for different values
\r
347 if ((r1 != null) && (r2 != null))
\r
349 if (r1.compareTo(r2) == 0)
\r
351 errln("ERROR: Rounding increment did not change");
\r
356 public void testJB6648()
\r
358 DecimalFormat df = new DecimalFormat();
\r
359 df.setParseStrict(true);
\r
361 String numstr = new String();
\r
363 String[] patterns = {
\r
372 for(int i=0; i < patterns.length; i++) {
\r
373 df.applyPattern(patterns[i]);
\r
374 numstr = df.format(5);
\r
376 Number n = df.parse(numstr);
\r
377 logln("INFO: Parsed " + numstr + " -> " + n);
\r
378 } catch (ParseException pe) {
\r
379 errln("ERROR: Failed round trip with strict parsing.");
\r
383 df.applyPattern(patterns[1]);
\r
386 Number n = df.parse(numstr);
\r
387 errln("ERROR: Expected round trip failure not encountered: numstr -> " + n);
\r
388 } catch (ParseException pe) {
\r
389 logln("INFO: Expected ParseExpection for " + numstr + " with strick parse enabled");
\r