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