3 *******************************************************************************
4 * Copyright (C) 1996-2009, International Business Machines Corporation and *
5 * others. All Rights Reserved. *
6 *******************************************************************************
8 package com.ibm.icu.dev.test.format;
10 import com.ibm.icu.dev.test.*;
11 import com.ibm.icu.text.*;
12 import java.text.ParseException;
14 import com.ibm.icu.impl.Utility;
18 * General test of Big NumberFormat
20 public class BigNumberFormatTest extends TestFmwk {
22 static final int ILLEGAL = -1;
24 public static void main(String[] args) throws Exception {
25 new BigNumberFormatTest().run(args);
28 public void TestExponent() {
29 DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
30 DecimalFormat fmt1 = new DecimalFormat("0.###E0", US);
31 DecimalFormat fmt2 = new DecimalFormat("0.###E+0", US);
32 Number n = new Long(1234);
33 expect(fmt1, n, "1.234E3");
34 expect(fmt2, n, "1.234E+3");
35 expect(fmt1, "1.234E3", n);
36 expect(fmt1, "1.234E+3", n); // Either format should parse "E+3"
37 expect(fmt2, "1.234E+3", n);
41 * Test the functioning of the secondary grouping value.
43 public void TestSecondaryGrouping() {
44 DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
45 DecimalFormat f = new DecimalFormat("#,##,###", US);
46 expect(f, new Long(123456789), "12,34,56,789");
47 expectPat(f, "#,##,###");
48 f.applyPattern("#,###");
49 f.setSecondaryGroupingSize(4);
50 expect(f, new Long(123456789), "12,3456,789");
51 expectPat(f, "#,####,###");
53 // On Sun JDK 1.2-1.3, the hi_IN locale uses '0' for a zero digit,
54 // but on IBM JDK 1.2-1.3, the locale uses U+0966.
55 f = (DecimalFormat) NumberFormat.getInstance(new Locale("hi", "IN"));
56 String str = transmute("1,87,65,43,210",
57 f.getDecimalFormatSymbols().getZeroDigit());
58 expect(f, new Long(1876543210), str);
61 private void expectPad(DecimalFormat fmt, String pat, int pos) {
62 expectPad(fmt, pat, pos, 0, (char)0);
65 private void expectPad(DecimalFormat fmt, String pat,
66 int pos, int width, char pad) {
67 int apos = 0, awidth = 0;
70 fmt.applyPattern(pat);
71 apos = fmt.getPadPosition();
72 awidth = fmt.getFormatWidth();
73 apad = fmt.getPadCharacter();
74 } catch (IllegalArgumentException e) {
79 if (apos == pos && awidth == width && apad == pad) {
80 logln("Ok \"" + pat + "\" pos=" + apos +
81 ((pos == -1) ? "" : " width=" + awidth + " pad=" + apad));
83 logln("FAIL \"" + pat + "\" pos=" + apos +
84 " width=" + awidth + " pad=" + apad +
85 ", expected " + pos + " " + width + " " + pad);
91 public void TestPatterns() {
92 DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
93 DecimalFormat fmt = new DecimalFormat("#", US);
95 expectPad(fmt, "*^#", DecimalFormat.PAD_BEFORE_PREFIX, 1, '^');
96 expectPad(fmt, "$*^#", DecimalFormat.PAD_AFTER_PREFIX, 2, '^');
97 expectPad(fmt, "#*^", DecimalFormat.PAD_BEFORE_SUFFIX, 1, '^');
98 expectPad(fmt, "#$*^", DecimalFormat.PAD_AFTER_SUFFIX, 2, '^');
99 expectPad(fmt, "$*^$#", ILLEGAL);
100 expectPad(fmt, "#$*^$", ILLEGAL);
101 expectPad(fmt, "'pre'#,##0*x'post'", DecimalFormat.PAD_BEFORE_SUFFIX,
103 expectPad(fmt, "''#0*x", DecimalFormat.PAD_BEFORE_SUFFIX,
105 expectPad(fmt, "'I''ll'*a###.##", DecimalFormat.PAD_AFTER_PREFIX,
108 fmt.applyPattern("AA#,##0.00ZZ");
109 fmt.setPadCharacter('^');
111 fmt.setFormatWidth(10);
113 fmt.setPadPosition(DecimalFormat.PAD_BEFORE_PREFIX);
114 expectPat(fmt, "*^AA#,##0.00ZZ");
116 fmt.setPadPosition(DecimalFormat.PAD_BEFORE_SUFFIX);
117 expectPat(fmt, "AA#,##0.00*^ZZ");
119 fmt.setPadPosition(DecimalFormat.PAD_AFTER_SUFFIX);
120 expectPat(fmt, "AA#,##0.00ZZ*^");
123 String exp = "AA*^#,##0.00ZZ";
124 fmt.setFormatWidth(12);
125 fmt.setPadPosition(DecimalFormat.PAD_AFTER_PREFIX);
128 fmt.setFormatWidth(13);
130 expectPat(fmt, "AA*^##,##0.00ZZ");
132 fmt.setFormatWidth(14);
134 expectPat(fmt, "AA*^###,##0.00ZZ");
136 fmt.setFormatWidth(15);
138 expectPat(fmt, "AA*^####,##0.00ZZ"); // This is the interesting case
140 fmt.setFormatWidth(16);
142 expectPat(fmt, "AA*^#,###,##0.00ZZ");
145 private void expectPat(DecimalFormat fmt, String exp) {
146 String pat = fmt.toPattern();
147 if (pat.equals(exp)) {
148 logln("Ok \"" + pat + '"');
150 errln("FAIL \"" + pat + "\", expected \"" + exp + '"');
155 * Test the handling of the AlphaWorks BigDecimal
157 public void TestAlphaBigDecimal() {
158 DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
159 /*For ICU compatibility [Richard/GCL]*/
160 expect(NumberFormat.getScientificInstance(Locale.US),
161 new Number[] { new com.ibm.icu.math.BigDecimal("12345.678901"),
164 expect(new DecimalFormat("##0.####E0", US),
165 new Number[] { new com.ibm.icu.math.BigDecimal("12345.4999"),
166 new com.ibm.icu.math.BigDecimal("12344.5001"),
169 expect(new DecimalFormat("##0.####E0", US),
170 new Number[] { new com.ibm.icu.math.BigDecimal("12345.5000"),
171 new com.ibm.icu.math.BigDecimal("12346.5000"),
178 public void TestScientific() {
179 DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
180 /*For ICU compatibility [Richard/GCL]*/
181 expect(NumberFormat.getScientificInstance(Locale.US),
182 new Number[] { new Double(12345.678901),
183 new java.math.BigDecimal("12345.678901"),
186 expect(new DecimalFormat("##0.###E0", US),
189 expect(new DecimalFormat("##0.###E0", US),
190 new Double(12345.00001),
192 expect(new DecimalFormat("##0.####E0", US),
193 new Number[] { new Integer(12345),
195 new java.math.BigDecimal("12345.4999"),
196 new java.math.BigDecimal("12344.5001"),
199 expect(new DecimalFormat("##0.####E0", US),
200 new Number[] { new java.math.BigDecimal("12345.5000"),
201 new java.math.BigDecimal("12346.5000"),
204 /*For ICU compatibility [Richard/GCL]*/
205 expect(NumberFormat.getScientificInstance(Locale.FRANCE),
206 new Double(12345.678901),
208 expect(new DecimalFormat("##0.####E0", US),
209 new Double(789.12345e-9),
211 expect(new DecimalFormat("##0.####E0", US),
214 expect(new DecimalFormat(".###E0", US),
217 expect(new DecimalFormat(".###E0", US),
220 expect(new DecimalFormat[] { new DecimalFormat("#E0", US),
221 new DecimalFormat("##E0", US),
222 new DecimalFormat("####E0", US),
223 new DecimalFormat("0E0", US),
224 new DecimalFormat("00E0", US),
225 new DecimalFormat("000E0", US),
228 new String[] { "4.5678E7",
236 expect(new DecimalFormat("###E0", US),
237 new Object[] { new Double(0.0000123), "12.3E-6",
238 new Double(0.000123), "123E-6",
239 new java.math.BigDecimal("0.00123"), "1.23E-3", // Cafe VM messes up Double(0.00123)
240 new Double(0.0123), "12.3E-3",
241 new Double(0.123), "123E-3",
242 new Double(1.23), "1.23E0",
243 new Double(12.3), "12.3E0",
244 new Double(123), "123E0",
245 new Double(1230), "1.23E3",
247 expect(new DecimalFormat("0.#E+00", US),
248 new Object[] { new Double(0.00012), "1.2E-04",
249 new Long(12000), "1.2E+04",
255 public void TestPad() {
256 DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
257 expect(new DecimalFormat("*^##.##", US),
258 new Object[] { new Long(0), "^^^^0",
259 new Double(-1.3), "^-1.3",
262 expect(new DecimalFormat("##0.0####E0*_ 'g-m/s^2'", US),
263 new Object[] { new Long(0), "0.0E0______ g-m/s^2",
264 new Double(1.0/3), "333.333E-3_ g-m/s^2",
267 expect(new DecimalFormat("##0.0####*_ 'g-m/s^2'", US),
268 new Object[] { new Long(0), "0.0______ g-m/s^2",
269 new Double(1.0/3), "0.33333__ g-m/s^2",
272 expect(new DecimalFormat("*x#,###,###,##0.00;*x(#,###,###,##0.00)", US),
274 new Long(-100), "xxxxxxxx(100.00)",
275 new Long(-1000), "xxxxxx(1,000.00)",
276 new Long(-1000000), "xx(1,000,000.00)",
277 new Long(-1000000000), "(1,000,000,000.00)",
281 private void expect(NumberFormat fmt, Object[] data) {
282 for (int i=0; i<data.length; i+=2) {
283 expect(fmt, (Number) data[i], (String) data[i+1]);
287 private void expect(Object fmto, Object numo, Object expo) {
288 NumberFormat fmt = null, fmts[] = null;
289 Number num = null, nums[] = null;
290 String exp = null, exps[] = null;
291 if (fmto instanceof NumberFormat[]) {
292 fmts = (NumberFormat[]) fmto;
294 fmt = (NumberFormat) fmto;
296 if (numo instanceof Number[]) {
297 nums = (Number[]) numo;
301 if (expo instanceof String[]) {
302 exps = (String[]) expo;
308 n = Math.max(n, fmts.length);
311 n = Math.max(n, nums.length);
314 n = Math.max(n, exps.length);
316 for (int i=0; i<n; ++i) {
317 expect(fmts == null ? fmt : fmts[i],
318 nums == null ? num : nums[i],
319 exps == null ? exp : exps[i]);
323 private static String showNumber(Number n) {
324 String cls = n.getClass().getName();
325 if (!(n instanceof com.ibm.icu.math.BigDecimal
326 || n instanceof java.math.BigDecimal)) {
327 int i = cls.lastIndexOf('.');
328 cls = cls.substring(i+1);
330 return n.toString() + " (" + cls + ')';
333 private void expect(NumberFormat fmt, Number n, String exp) {
334 String saw = fmt.format(n);
335 String pat = ((DecimalFormat) fmt).toPattern();
336 if (saw.equals(exp)) {
337 logln("Ok " + showNumber(n) + " x " +
339 Utility.escape(saw));
341 errln("FAIL " + showNumber(n) + " x " +
343 Utility.escape(saw) + ", expected " + Utility.escape(exp));
347 private void expect(NumberFormat fmt, String str, Number exp) {
350 saw = fmt.parse(str);
351 } catch (ParseException e) {
354 String pat = ((DecimalFormat) fmt).toPattern();
355 if (saw.equals(exp)) {
356 logln("Ok \"" + str + "\" x " +
360 errln("FAIL \"" + str + "\" x " +
362 showNumber(saw) + ", expected " + showNumber(exp));
367 * Given a string composed of [0-9] and other chars, convert the
368 * [0-9] chars to be offsets 0..9 from 'zero'.
370 private static String transmute(String str, char zero) {
371 StringBuffer buf = new StringBuffer();
372 for (int i=0; i<str.length(); ++i) {
373 char c = str.charAt(i);
374 if (c >= '0' && c <= '9') {
375 c = (char) (c - '0' + zero);
379 return buf.toString();
382 public void Test4161100() {
383 NumberFormat f = NumberFormat.getInstance();
384 f.setMinimumFractionDigits(1);
385 f.setMaximumFractionDigits(1);
387 String s = f.format(a);
389 ((DecimalFormat) f).toPattern() + " = " +
391 if (!s.equals("-0.1")) {
396 public void TestBigDecimalJ28() {
402 "-12.3e-45", "-1.23E-44",
405 NumberFormat fmt = NumberFormat.getScientificInstance(Locale.US);
406 logln("Pattern: " + ((DecimalFormat)fmt).toPattern());
407 for (int i=0; i<DATA.length; i+=2) {
408 String input = DATA[i];
409 String exp = DATA[i+1];
410 com.ibm.icu.math.BigDecimal bd = new com.ibm.icu.math.BigDecimal(input);
411 String output = fmt.format(bd);
412 if (output.equals(exp)) {
413 logln("input=" + input + " num=" + bd + " output=" + output);
415 errln("FAIL: input=" + input + " num=" + bd + " output=" + output +
420 //#if defined(FOUNDATION10)
422 public void TestBigDecimalRounding() {
424 java.text.DecimalFormat jdkFormat=new java.text.DecimalFormat("###,###,###,##0");
425 com.ibm.icu.text.DecimalFormat icuFormat=new com.ibm.icu.text.DecimalFormat("###,###,###,##0");
427 "-1.74", "-1.24", "-0.74", "-0.24", "0.24", "0.74", "1.24", "1.74"
429 for (int i = 0; i < values.length; ++i) {
430 String val = values[i];
431 java.math.BigDecimal bd = new java.math.BigDecimal(val);
432 String jdk = jdkFormat.format(bd);
433 String icu = icuFormat.format(bd);
434 logln("Format of BigDecimal " + val + " by JDK is " + jdk);
435 logln("Format of BigDecimal " + val + " by ICU is " + icu);
436 if (!jdk.equals(icu)) {
437 errln("BigDecimal jdk: " + jdk + " != icu: " + icu);
440 double d = bd.doubleValue();
441 jdk = jdkFormat.format(d);
442 icu = icuFormat.format(d);
443 logln("Format of double " + val + " by JDK is " + jdk);
444 logln("Format of double " + val + " by ICU is " + icu);
445 if (!jdk.equals(icu)) {
446 errln("double jdk: " + jdk + " != icu: " + icu);