Source Code Cross Referenced for BigNumberFormatTest.java in  » Internationalization-Localization » icu4j » com » ibm » icu » dev » test » format » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Internationalization Localization » icu4j » com.ibm.icu.dev.test.format 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        //##header
002:        /*
003:         *******************************************************************************
004:         * Copyright (C) 1996-2005, International Business Machines Corporation and    *
005:         * others. All Rights Reserved.                                                *
006:         *******************************************************************************
007:         */
008:        package com.ibm.icu.dev.test.format;
009:
010:        import com.ibm.icu.dev.test.*;
011:        import com.ibm.icu.text.*;
012:        import java.text.ParseException;
013:        import java.util.*;
014:        import com.ibm.icu.impl.Utility;
015:
016:        /**
017:         * @test
018:         * General test of Big NumberFormat
019:         */
020:        public class BigNumberFormatTest extends TestFmwk {
021:
022:            static final int ILLEGAL = -1;
023:
024:            public static void main(String[] args) throws Exception {
025:                new BigNumberFormatTest().run(args);
026:            }
027:
028:            public void TestExponent() {
029:                DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
030:                DecimalFormat fmt1 = new DecimalFormat("0.###E0", US);
031:                DecimalFormat fmt2 = new DecimalFormat("0.###E+0", US);
032:                Number n = new Long(1234);
033:                expect(fmt1, n, "1.234E3");
034:                expect(fmt2, n, "1.234E+3");
035:                expect(fmt1, "1.234E3", n);
036:                expect(fmt1, "1.234E+3", n); // Either format should parse "E+3"
037:                expect(fmt2, "1.234E+3", n);
038:            }
039:
040:            /**
041:             * Test the functioning of the secondary grouping value.
042:             */
043:            public void TestSecondaryGrouping() {
044:                DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
045:                DecimalFormat f = new DecimalFormat("#,##,###", US);
046:                expect(f, new Long(123456789), "12,34,56,789");
047:                expectPat(f, "#,##,###");
048:                f.applyPattern("#,###");
049:                f.setSecondaryGroupingSize(4);
050:                expect(f, new Long(123456789), "12,3456,789");
051:                expectPat(f, "#,####,###");
052:
053:                // On Sun JDK 1.2-1.3, the hi_IN locale uses '0' for a zero digit,
054:                // but on IBM JDK 1.2-1.3, the locale uses U+0966.
055:                f = (DecimalFormat) NumberFormat.getInstance(new Locale("hi",
056:                        "IN"));
057:                String str = transmute("1,87,65,43,210", f
058:                        .getDecimalFormatSymbols().getZeroDigit());
059:                expect(f, new Long(1876543210), str);
060:            }
061:
062:            private void expectPad(DecimalFormat fmt, String pat, int pos) {
063:                expectPad(fmt, pat, pos, 0, (char) 0);
064:            }
065:
066:            private void expectPad(DecimalFormat fmt, String pat, int pos,
067:                    int width, char pad) {
068:                int apos = 0, awidth = 0;
069:                char apad = 0;
070:                try {
071:                    fmt.applyPattern(pat);
072:                    apos = fmt.getPadPosition();
073:                    awidth = fmt.getFormatWidth();
074:                    apad = fmt.getPadCharacter();
075:                } catch (IllegalArgumentException e) {
076:                    apos = -1;
077:                    awidth = width;
078:                    apad = pad;
079:                }
080:                if (apos == pos && awidth == width && apad == pad) {
081:                    logln("Ok   \""
082:                            + pat
083:                            + "\" pos="
084:                            + apos
085:                            + ((pos == -1) ? "" : " width=" + awidth + " pad="
086:                                    + apad));
087:                } else {
088:                    logln("FAIL \"" + pat + "\" pos=" + apos + " width="
089:                            + awidth + " pad=" + apad + ", expected " + pos
090:                            + " " + width + " " + pad);
091:                }
092:            }
093:
094:            /**
095:             */
096:            public void TestPatterns() {
097:                DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
098:                DecimalFormat fmt = new DecimalFormat("#", US);
099:
100:                expectPad(fmt, "*^#", DecimalFormat.PAD_BEFORE_PREFIX, 1, '^');
101:                expectPad(fmt, "$*^#", DecimalFormat.PAD_AFTER_PREFIX, 2, '^');
102:                expectPad(fmt, "#*^", DecimalFormat.PAD_BEFORE_SUFFIX, 1, '^');
103:                expectPad(fmt, "#$*^", DecimalFormat.PAD_AFTER_SUFFIX, 2, '^');
104:                expectPad(fmt, "$*^$#", ILLEGAL);
105:                expectPad(fmt, "#$*^$", ILLEGAL);
106:                expectPad(fmt, "'pre'#,##0*x'post'",
107:                        DecimalFormat.PAD_BEFORE_SUFFIX, 12, 'x');
108:                expectPad(fmt, "''#0*x", DecimalFormat.PAD_BEFORE_SUFFIX, 3,
109:                        'x');
110:                expectPad(fmt, "'I''ll'*a###.##",
111:                        DecimalFormat.PAD_AFTER_PREFIX, 10, 'a');
112:
113:                fmt.applyPattern("AA#,##0.00ZZ");
114:                fmt.setPadCharacter('^');
115:
116:                fmt.setFormatWidth(10);
117:
118:                fmt.setPadPosition(DecimalFormat.PAD_BEFORE_PREFIX);
119:                expectPat(fmt, "*^AA#,##0.00ZZ");
120:
121:                fmt.setPadPosition(DecimalFormat.PAD_BEFORE_SUFFIX);
122:                expectPat(fmt, "AA#,##0.00*^ZZ");
123:
124:                fmt.setPadPosition(DecimalFormat.PAD_AFTER_SUFFIX);
125:                expectPat(fmt, "AA#,##0.00ZZ*^");
126:
127:                //            12  3456789012
128:                String exp = "AA*^#,##0.00ZZ";
129:                fmt.setFormatWidth(12);
130:                fmt.setPadPosition(DecimalFormat.PAD_AFTER_PREFIX);
131:                expectPat(fmt, exp);
132:
133:                fmt.setFormatWidth(13);
134:                //              12  34567890123
135:                expectPat(fmt, "AA*^##,##0.00ZZ");
136:
137:                fmt.setFormatWidth(14);
138:                //              12  345678901234
139:                expectPat(fmt, "AA*^###,##0.00ZZ");
140:
141:                fmt.setFormatWidth(15);
142:                //              12  3456789012345
143:                expectPat(fmt, "AA*^####,##0.00ZZ"); // This is the interesting case
144:
145:                fmt.setFormatWidth(16);
146:                //              12  34567890123456
147:                expectPat(fmt, "AA*^#,###,##0.00ZZ");
148:            }
149:
150:            private void expectPat(DecimalFormat fmt, String exp) {
151:                String pat = fmt.toPattern();
152:                if (pat.equals(exp)) {
153:                    logln("Ok   \"" + pat + '"');
154:                } else {
155:                    errln("FAIL \"" + pat + "\", expected \"" + exp + '"');
156:                }
157:            }
158:
159:            /**
160:             * Test the handling of the AlphaWorks BigDecimal
161:             */
162:            public void TestAlphaBigDecimal() {
163:                DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
164:                /*For ICU compatibility [Richard/GCL]*/
165:                expect(NumberFormat.getScientificInstance(Locale.US),
166:                        new Number[] { new com.ibm.icu.math.BigDecimal(
167:                                "12345.678901"), }, "1.2345678901E4");
168:                expect(new DecimalFormat("##0.####E0", US), new Number[] {
169:                        new com.ibm.icu.math.BigDecimal("12345.4999"),
170:                        new com.ibm.icu.math.BigDecimal("12344.5001"), },
171:                        "12.345E3");
172:                expect(new DecimalFormat("##0.####E0", US), new Number[] {
173:                        new com.ibm.icu.math.BigDecimal("12345.5000"),
174:                        new com.ibm.icu.math.BigDecimal("12346.5000"), },
175:                        "12.346E3");
176:            }
177:
178:            /**
179:             */
180:            public void TestScientific() {
181:                DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
182:                /*For ICU compatibility [Richard/GCL]*/
183:                expect(NumberFormat.getScientificInstance(Locale.US),
184:                        new Number[] { new Double(12345.678901),
185:                                new java.math.BigDecimal("12345.678901"), },
186:                        "1.2345678901E4");
187:                expect(new DecimalFormat("##0.###E0", US), new Double(12345),
188:                        "12.34E3");
189:                expect(new DecimalFormat("##0.###E0", US), new Double(
190:                        12345.00001), "12.35E3");
191:                expect(new DecimalFormat("##0.####E0", US), new Number[] {
192:                        new Integer(12345), new Long(12345),
193:                        new java.math.BigDecimal("12345.4999"),
194:                        new java.math.BigDecimal("12344.5001"), }, "12.345E3");
195:                expect(new DecimalFormat("##0.####E0", US), new Number[] {
196:                        new java.math.BigDecimal("12345.5000"),
197:                        new java.math.BigDecimal("12346.5000"), }, "12.346E3");
198:                /*For ICU compatibility [Richard/GCL]*/
199:                expect(NumberFormat.getScientificInstance(Locale.FRANCE),
200:                        new Double(12345.678901), "1,2345678901E4");
201:                expect(new DecimalFormat("##0.####E0", US), new Double(
202:                        789.12345e-9), "789.12E-9");
203:                expect(new DecimalFormat("##0.####E0", US),
204:                        new Double(780.e-9), "780E-9");
205:                expect(new DecimalFormat(".###E0", US), new Double(45678),
206:                        ".457E5");
207:                expect(new DecimalFormat(".###E0", US), new Long(0), ".0E0");
208:                expect(new DecimalFormat[] { new DecimalFormat("#E0", US),
209:                        new DecimalFormat("##E0", US),
210:                        new DecimalFormat("####E0", US),
211:                        new DecimalFormat("0E0", US),
212:                        new DecimalFormat("00E0", US),
213:                        new DecimalFormat("000E0", US), }, new Long(45678000),
214:                        new String[] { "4.5678E7", "45.678E6", "4567.8E4",
215:                                "5E7", "46E6", "457E5", });
216:                expect(new DecimalFormat("###E0", US), new Object[] {
217:                        new Double(0.0000123),
218:                        "12.3E-6",
219:                        new Double(0.000123),
220:                        "123E-6",
221:                        new java.math.BigDecimal("0.00123"),
222:                        "1.23E-3", // Cafe VM messes up Double(0.00123)
223:                        new Double(0.0123), "12.3E-3", new Double(0.123),
224:                        "123E-3", new Double(1.23), "1.23E0", new Double(12.3),
225:                        "12.3E0", new Double(123), "123E0", new Double(1230),
226:                        "1.23E3", });
227:                expect(new DecimalFormat("0.#E+00", US), new Object[] {
228:                        new Double(0.00012), "1.2E-04", new Long(12000),
229:                        "1.2E+04", });
230:            }
231:
232:            /**
233:             */
234:            public void TestPad() {
235:                DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
236:                expect(new DecimalFormat("*^##.##", US), new Object[] {
237:                        new Long(0), "^^^^0", new Double(-1.3), "^-1.3", });
238:                expect(new DecimalFormat("##0.0####E0*_ 'g-m/s^2'", US),
239:                        new Object[] { new Long(0), "0.0E0______ g-m/s^2",
240:                                new Double(1.0 / 3), "333.333E-3_ g-m/s^2", });
241:                expect(new DecimalFormat("##0.0####*_ 'g-m/s^2'", US),
242:                        new Object[] { new Long(0), "0.0______ g-m/s^2",
243:                                new Double(1.0 / 3), "0.33333__ g-m/s^2", });
244:                expect(new DecimalFormat(
245:                        "*x#,###,###,##0.00;*x(#,###,###,##0.00)", US),
246:                        new Object[] { new Long(-100), "xxxxxxxx(100.00)",
247:                                new Long(-1000), "xxxxxx(1,000.00)",
248:                                new Long(-1000000), "xx(1,000,000.00)",
249:                                new Long(-1000000000), "(1,000,000,000.00)", });
250:            }
251:
252:            private void expect(NumberFormat fmt, Object[] data) {
253:                for (int i = 0; i < data.length; i += 2) {
254:                    expect(fmt, (Number) data[i], (String) data[i + 1]);
255:                }
256:            }
257:
258:            private void expect(Object fmto, Object numo, Object expo) {
259:                NumberFormat fmt = null, fmts[] = null;
260:                Number num = null, nums[] = null;
261:                String exp = null, exps[] = null;
262:                if (fmto instanceof  NumberFormat[]) {
263:                    fmts = (NumberFormat[]) fmto;
264:                } else {
265:                    fmt = (NumberFormat) fmto;
266:                }
267:                if (numo instanceof  Number[]) {
268:                    nums = (Number[]) numo;
269:                } else {
270:                    num = (Number) numo;
271:                }
272:                if (expo instanceof  String[]) {
273:                    exps = (String[]) expo;
274:                } else {
275:                    exp = (String) expo;
276:                }
277:                int n = 1;
278:                if (fmts != null) {
279:                    n = Math.max(n, fmts.length);
280:                }
281:                if (nums != null) {
282:                    n = Math.max(n, nums.length);
283:                }
284:                if (exps != null) {
285:                    n = Math.max(n, exps.length);
286:                }
287:                for (int i = 0; i < n; ++i) {
288:                    expect(fmts == null ? fmt : fmts[i], nums == null ? num
289:                            : nums[i], exps == null ? exp : exps[i]);
290:                }
291:            }
292:
293:            private static String showNumber(Number n) {
294:                String cls = n.getClass().getName();
295:                if (!(n instanceof  com.ibm.icu.math.BigDecimal || n instanceof  java.math.BigDecimal)) {
296:                    int i = cls.lastIndexOf('.');
297:                    cls = cls.substring(i + 1);
298:                }
299:                return n.toString() + " (" + cls + ')';
300:            }
301:
302:            private void expect(NumberFormat fmt, Number n, String exp) {
303:                String saw = fmt.format(n);
304:                String pat = ((DecimalFormat) fmt).toPattern();
305:                if (saw.equals(exp)) {
306:                    logln("Ok   " + showNumber(n) + " x " + pat + " = "
307:                            + Utility.escape(saw));
308:                } else {
309:                    errln("FAIL " + showNumber(n) + " x " + pat + " = \""
310:                            + Utility.escape(saw) + ", expected "
311:                            + Utility.escape(exp));
312:                }
313:            }
314:
315:            private void expect(NumberFormat fmt, String str, Number exp) {
316:                Number saw = null;
317:                try {
318:                    saw = fmt.parse(str);
319:                } catch (ParseException e) {
320:                    saw = null;
321:                }
322:                String pat = ((DecimalFormat) fmt).toPattern();
323:                if (saw.equals(exp)) {
324:                    logln("Ok   \"" + str + "\" x " + pat + " = "
325:                            + showNumber(saw));
326:                } else {
327:                    errln("FAIL \"" + str + "\" x " + pat + " = "
328:                            + showNumber(saw) + ", expected " + showNumber(exp));
329:                }
330:            }
331:
332:            /**
333:             * Given a string composed of [0-9] and other chars, convert the
334:             * [0-9] chars to be offsets 0..9 from 'zero'.
335:             */
336:            private static String transmute(String str, char zero) {
337:                StringBuffer buf = new StringBuffer();
338:                for (int i = 0; i < str.length(); ++i) {
339:                    char c = str.charAt(i);
340:                    if (c >= '0' && c <= '9') {
341:                        c = (char) (c - '0' + zero);
342:                    }
343:                    buf.append(c);
344:                }
345:                return buf.toString();
346:            }
347:
348:            public void Test4161100() {
349:                NumberFormat f = NumberFormat.getInstance();
350:                f.setMinimumFractionDigits(1);
351:                f.setMaximumFractionDigits(1);
352:                double a = -0.09;
353:                String s = f.format(a);
354:                logln(a + " x " + ((DecimalFormat) f).toPattern() + " = " + s);
355:                if (!s.equals("-0.1")) {
356:                    errln("FAIL");
357:                }
358:            }
359:
360:            public void TestBigDecimalJ28() {
361:                String[] DATA = { "1", "1E0", "-1", "-1E0", "0", "0E0",
362:                        "12e34", "1.2E35", "-12.3e-45", "-1.23E-44", "0.73e-7",
363:                        "7.3E-8", };
364:                NumberFormat fmt = NumberFormat
365:                        .getScientificInstance(Locale.US);
366:                logln("Pattern: " + ((DecimalFormat) fmt).toPattern());
367:                for (int i = 0; i < DATA.length; i += 2) {
368:                    String input = DATA[i];
369:                    String exp = DATA[i + 1];
370:                    com.ibm.icu.math.BigDecimal bd = new com.ibm.icu.math.BigDecimal(
371:                            input);
372:                    String output = fmt.format(bd);
373:                    if (output.equals(exp)) {
374:                        logln("input=" + input + " num=" + bd + " output="
375:                                + output);
376:                    } else {
377:                        errln("FAIL: input=" + input + " num=" + bd
378:                                + " output=" + output + " expected=" + exp);
379:                    }
380:                }
381:            }
382:
383:            //#ifndef FOUNDATION
384:            public void TestBigDecimalRounding() {
385:                // jb 3657
386:                java.text.DecimalFormat jdkFormat = new java.text.DecimalFormat(
387:                        "###,###,###,##0");
388:                com.ibm.icu.text.DecimalFormat icuFormat = new com.ibm.icu.text.DecimalFormat(
389:                        "###,###,###,##0");
390:                String[] values = { "-1.74", "-1.24", "-0.74", "-0.24", "0.24",
391:                        "0.74", "1.24", "1.74" };
392:                for (int i = 0; i < values.length; ++i) {
393:                    String val = values[i];
394:                    java.math.BigDecimal bd = new java.math.BigDecimal(val);
395:                    String jdk = jdkFormat.format(bd);
396:                    String icu = icuFormat.format(bd);
397:                    logln("Format of BigDecimal " + val + " by JDK is " + jdk);
398:                    logln("Format of BigDecimal " + val + " by ICU is " + icu);
399:                    if (!jdk.equals(icu)) {
400:                        errln("BigDecimal jdk: " + jdk + " != icu: " + icu);
401:                    }
402:
403:                    double d = bd.doubleValue();
404:                    jdk = jdkFormat.format(d);
405:                    icu = icuFormat.format(d);
406:                    logln("Format of double " + val + " by JDK is " + jdk);
407:                    logln("Format of double " + val + " by ICU is " + icu);
408:                    if (!jdk.equals(icu)) {
409:                        errln("double jdk: " + jdk + " != icu: " + icu);
410:                    }
411:                }
412:            }
413:            //#endif
414:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.