-//##header\r
-/*\r
- *******************************************************************************\r
- * Copyright (C) 1996-2009, International Business Machines Corporation and *\r
- * others. All Rights Reserved. *\r
- *******************************************************************************\r
- */\r
-package com.ibm.icu.dev.test.format;\r
-\r
-import com.ibm.icu.dev.test.*;\r
-import com.ibm.icu.text.*;\r
-import java.text.ParseException;\r
-import java.util.*;\r
-import com.ibm.icu.impl.Utility;\r
-\r
-/**\r
- * @test\r
- * General test of Big NumberFormat\r
- */\r
-public class BigNumberFormatTest extends TestFmwk {\r
-\r
- static final int ILLEGAL = -1;\r
-\r
- public static void main(String[] args) throws Exception {\r
- new BigNumberFormatTest().run(args);\r
- }\r
- \r
- public void TestExponent() {\r
- DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);\r
- DecimalFormat fmt1 = new DecimalFormat("0.###E0", US);\r
- DecimalFormat fmt2 = new DecimalFormat("0.###E+0", US);\r
- Number n = new Long(1234);\r
- expect(fmt1, n, "1.234E3");\r
- expect(fmt2, n, "1.234E+3");\r
- expect(fmt1, "1.234E3", n);\r
- expect(fmt1, "1.234E+3", n); // Either format should parse "E+3"\r
- expect(fmt2, "1.234E+3", n);\r
- }\r
-\r
- /**\r
- * Test the functioning of the secondary grouping value.\r
- */\r
- public void TestSecondaryGrouping() {\r
- DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);\r
- DecimalFormat f = new DecimalFormat("#,##,###", US);\r
- expect(f, new Long(123456789), "12,34,56,789");\r
- expectPat(f, "#,##,###");\r
- f.applyPattern("#,###");\r
- f.setSecondaryGroupingSize(4);\r
- expect(f, new Long(123456789), "12,3456,789");\r
- expectPat(f, "#,####,###");\r
-\r
- // On Sun JDK 1.2-1.3, the hi_IN locale uses '0' for a zero digit,\r
- // but on IBM JDK 1.2-1.3, the locale uses U+0966.\r
- f = (DecimalFormat) NumberFormat.getInstance(new Locale("hi", "IN"));\r
- String str = transmute("1,87,65,43,210",\r
- f.getDecimalFormatSymbols().getZeroDigit());\r
- expect(f, new Long(1876543210), str);\r
- }\r
-\r
- private void expectPad(DecimalFormat fmt, String pat, int pos) {\r
- expectPad(fmt, pat, pos, 0, (char)0);\r
- }\r
-\r
- private void expectPad(DecimalFormat fmt, String pat,\r
- int pos, int width, char pad) {\r
- int apos = 0, awidth = 0;\r
- char apad = 0;\r
- try {\r
- fmt.applyPattern(pat);\r
- apos = fmt.getPadPosition();\r
- awidth = fmt.getFormatWidth();\r
- apad = fmt.getPadCharacter();\r
- } catch (IllegalArgumentException e) {\r
- apos = -1;\r
- awidth = width;\r
- apad = pad;\r
- }\r
- if (apos == pos && awidth == width && apad == pad) {\r
- logln("Ok \"" + pat + "\" pos=" + apos +\r
- ((pos == -1) ? "" : " width=" + awidth + " pad=" + apad));\r
- } else {\r
- logln("FAIL \"" + pat + "\" pos=" + apos +\r
- " width=" + awidth + " pad=" + apad +\r
- ", expected " + pos + " " + width + " " + pad);\r
- }\r
- }\r
-\r
- /**\r
- */\r
- public void TestPatterns() {\r
- DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);\r
- DecimalFormat fmt = new DecimalFormat("#", US);\r
-\r
- expectPad(fmt, "*^#", DecimalFormat.PAD_BEFORE_PREFIX, 1, '^');\r
- expectPad(fmt, "$*^#", DecimalFormat.PAD_AFTER_PREFIX, 2, '^');\r
- expectPad(fmt, "#*^", DecimalFormat.PAD_BEFORE_SUFFIX, 1, '^');\r
- expectPad(fmt, "#$*^", DecimalFormat.PAD_AFTER_SUFFIX, 2, '^');\r
- expectPad(fmt, "$*^$#", ILLEGAL);\r
- expectPad(fmt, "#$*^$", ILLEGAL);\r
- expectPad(fmt, "'pre'#,##0*x'post'", DecimalFormat.PAD_BEFORE_SUFFIX,\r
- 12, 'x');\r
- expectPad(fmt, "''#0*x", DecimalFormat.PAD_BEFORE_SUFFIX,\r
- 3, 'x');\r
- expectPad(fmt, "'I''ll'*a###.##", DecimalFormat.PAD_AFTER_PREFIX,\r
- 10, 'a');\r
-\r
- fmt.applyPattern("AA#,##0.00ZZ");\r
- fmt.setPadCharacter('^');\r
-\r
- fmt.setFormatWidth(10);\r
-\r
- fmt.setPadPosition(DecimalFormat.PAD_BEFORE_PREFIX);\r
- expectPat(fmt, "*^AA#,##0.00ZZ");\r
-\r
- fmt.setPadPosition(DecimalFormat.PAD_BEFORE_SUFFIX);\r
- expectPat(fmt, "AA#,##0.00*^ZZ");\r
-\r
- fmt.setPadPosition(DecimalFormat.PAD_AFTER_SUFFIX);\r
- expectPat(fmt, "AA#,##0.00ZZ*^");\r
-\r
- // 12 3456789012\r
- String exp = "AA*^#,##0.00ZZ";\r
- fmt.setFormatWidth(12);\r
- fmt.setPadPosition(DecimalFormat.PAD_AFTER_PREFIX);\r
- expectPat(fmt, exp);\r
-\r
- fmt.setFormatWidth(13);\r
- // 12 34567890123\r
- expectPat(fmt, "AA*^##,##0.00ZZ");\r
-\r
- fmt.setFormatWidth(14);\r
- // 12 345678901234\r
- expectPat(fmt, "AA*^###,##0.00ZZ");\r
-\r
- fmt.setFormatWidth(15);\r
- // 12 3456789012345\r
- expectPat(fmt, "AA*^####,##0.00ZZ"); // This is the interesting case\r
-\r
- fmt.setFormatWidth(16);\r
- // 12 34567890123456\r
- expectPat(fmt, "AA*^#,###,##0.00ZZ");\r
- }\r
-\r
- private void expectPat(DecimalFormat fmt, String exp) {\r
- String pat = fmt.toPattern();\r
- if (pat.equals(exp)) {\r
- logln("Ok \"" + pat + '"');\r
- } else {\r
- errln("FAIL \"" + pat + "\", expected \"" + exp + '"');\r
- }\r
- }\r
-\r
- /**\r
- * Test the handling of the AlphaWorks BigDecimal\r
- */\r
- public void TestAlphaBigDecimal() {\r
- DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);\r
- /*For ICU compatibility [Richard/GCL]*/\r
- expect(NumberFormat.getScientificInstance(Locale.US),\r
- new Number[] { new com.ibm.icu.math.BigDecimal("12345.678901"),\r
- },\r
- "1.2345678901E4");\r
- expect(new DecimalFormat("##0.####E0", US),\r
- new Number[] { new com.ibm.icu.math.BigDecimal("12345.4999"),\r
- new com.ibm.icu.math.BigDecimal("12344.5001"),\r
- },\r
- "12.345E3");\r
- expect(new DecimalFormat("##0.####E0", US),\r
- new Number[] { new com.ibm.icu.math.BigDecimal("12345.5000"),\r
- new com.ibm.icu.math.BigDecimal("12346.5000"),\r
- },\r
- "12.346E3");\r
- }\r
-\r
- /**\r
- */\r
- public void TestScientific() {\r
- DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);\r
- /*For ICU compatibility [Richard/GCL]*/\r
- expect(NumberFormat.getScientificInstance(Locale.US),\r
- new Number[] { new Double(12345.678901),\r
- new java.math.BigDecimal("12345.678901"),\r
- },\r
- "1.2345678901E4");\r
- expect(new DecimalFormat("##0.###E0", US),\r
- new Double(12345),\r
- "12.34E3");\r
- expect(new DecimalFormat("##0.###E0", US),\r
- new Double(12345.00001),\r
- "12.35E3");\r
- expect(new DecimalFormat("##0.####E0", US),\r
- new Number[] { new Integer(12345),\r
- new Long(12345),\r
- new java.math.BigDecimal("12345.4999"),\r
- new java.math.BigDecimal("12344.5001"),\r
- },\r
- "12.345E3");\r
- expect(new DecimalFormat("##0.####E0", US),\r
- new Number[] { new java.math.BigDecimal("12345.5000"),\r
- new java.math.BigDecimal("12346.5000"),\r
- },\r
- "12.346E3");\r
- /*For ICU compatibility [Richard/GCL]*/\r
- expect(NumberFormat.getScientificInstance(Locale.FRANCE),\r
- new Double(12345.678901),\r
- "1,2345678901E4");\r
- expect(new DecimalFormat("##0.####E0", US),\r
- new Double(789.12345e-9),\r
- "789.12E-9");\r
- expect(new DecimalFormat("##0.####E0", US),\r
- new Double(780.e-9),\r
- "780E-9");\r
- expect(new DecimalFormat(".###E0", US),\r
- new Double(45678),\r
- ".457E5");\r
- expect(new DecimalFormat(".###E0", US),\r
- new Long(0),\r
- ".0E0");\r
- expect(new DecimalFormat[] { new DecimalFormat("#E0", US),\r
- new DecimalFormat("##E0", US),\r
- new DecimalFormat("####E0", US),\r
- new DecimalFormat("0E0", US), \r
- new DecimalFormat("00E0", US), \r
- new DecimalFormat("000E0", US), \r
- },\r
- new Long(45678000),\r
- new String[] { "4.5678E7",\r
- "45.678E6",\r
- "4567.8E4",\r
- "5E7",\r
- "46E6", \r
- "457E5",\r
- }\r
- );\r
- expect(new DecimalFormat("###E0", US),\r
- new Object[] { new Double(0.0000123), "12.3E-6",\r
- new Double(0.000123), "123E-6",\r
- new java.math.BigDecimal("0.00123"), "1.23E-3", // Cafe VM messes up Double(0.00123)\r
- new Double(0.0123), "12.3E-3",\r
- new Double(0.123), "123E-3",\r
- new Double(1.23), "1.23E0",\r
- new Double(12.3), "12.3E0",\r
- new Double(123), "123E0",\r
- new Double(1230), "1.23E3",\r
- });\r
- expect(new DecimalFormat("0.#E+00", US),\r
- new Object[] { new Double(0.00012), "1.2E-04",\r
- new Long(12000), "1.2E+04",\r
- });\r
- }\r
-\r
- /**\r
- */\r
- public void TestPad() {\r
- DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);\r
- expect(new DecimalFormat("*^##.##", US),\r
- new Object[] { new Long(0), "^^^^0",\r
- new Double(-1.3), "^-1.3",\r
- }\r
- );\r
- expect(new DecimalFormat("##0.0####E0*_ 'g-m/s^2'", US),\r
- new Object[] { new Long(0), "0.0E0______ g-m/s^2",\r
- new Double(1.0/3), "333.333E-3_ g-m/s^2",\r
- }\r
- );\r
- expect(new DecimalFormat("##0.0####*_ 'g-m/s^2'", US),\r
- new Object[] { new Long(0), "0.0______ g-m/s^2",\r
- new Double(1.0/3), "0.33333__ g-m/s^2",\r
- }\r
- );\r
- expect(new DecimalFormat("*x#,###,###,##0.00;*x(#,###,###,##0.00)", US),\r
- new Object[] {\r
- new Long(-100), "xxxxxxxx(100.00)",\r
- new Long(-1000), "xxxxxx(1,000.00)",\r
- new Long(-1000000), "xx(1,000,000.00)",\r
- new Long(-1000000000), "(1,000,000,000.00)",\r
- });\r
- }\r
- \r
- private void expect(NumberFormat fmt, Object[] data) {\r
- for (int i=0; i<data.length; i+=2) {\r
- expect(fmt, (Number) data[i], (String) data[i+1]);\r
- }\r
- }\r
- \r
- private void expect(Object fmto, Object numo, Object expo) {\r
- NumberFormat fmt = null, fmts[] = null;\r
- Number num = null, nums[] = null;\r
- String exp = null, exps[] = null;\r
- if (fmto instanceof NumberFormat[]) {\r
- fmts = (NumberFormat[]) fmto;\r
- } else {\r
- fmt = (NumberFormat) fmto;\r
- }\r
- if (numo instanceof Number[]) {\r
- nums = (Number[]) numo;\r
- } else {\r
- num = (Number) numo;\r
- }\r
- if (expo instanceof String[]) {\r
- exps = (String[]) expo;\r
- } else {\r
- exp = (String) expo;\r
- }\r
- int n = 1;\r
- if (fmts != null) {\r
- n = Math.max(n, fmts.length);\r
- }\r
- if (nums != null) {\r
- n = Math.max(n, nums.length);\r
- }\r
- if (exps != null) {\r
- n = Math.max(n, exps.length);\r
- }\r
- for (int i=0; i<n; ++i) {\r
- expect(fmts == null ? fmt : fmts[i],\r
- nums == null ? num : nums[i],\r
- exps == null ? exp : exps[i]);\r
- }\r
- }\r
-\r
- private static String showNumber(Number n) {\r
- String cls = n.getClass().getName();\r
- if (!(n instanceof com.ibm.icu.math.BigDecimal\r
- || n instanceof java.math.BigDecimal)) {\r
- int i = cls.lastIndexOf('.');\r
- cls = cls.substring(i+1);\r
- }\r
- return n.toString() + " (" + cls + ')';\r
- }\r
-\r
- private void expect(NumberFormat fmt, Number n, String exp) {\r
- String saw = fmt.format(n);\r
- String pat = ((DecimalFormat) fmt).toPattern();\r
- if (saw.equals(exp)) {\r
- logln("Ok " + showNumber(n) + " x " +\r
- pat + " = " +\r
- Utility.escape(saw));\r
- } else {\r
- errln("FAIL " + showNumber(n) + " x " +\r
- pat + " = \"" +\r
- Utility.escape(saw) + ", expected " + Utility.escape(exp));\r
- }\r
- }\r
-\r
- private void expect(NumberFormat fmt, String str, Number exp) {\r
- Number saw = null;\r
- try {\r
- saw = fmt.parse(str);\r
- } catch (ParseException e) {\r
- saw = null;\r
- }\r
- String pat = ((DecimalFormat) fmt).toPattern();\r
- if (saw.equals(exp)) {\r
- logln("Ok \"" + str + "\" x " +\r
- pat + " = " +\r
- showNumber(saw));\r
- } else {\r
- errln("FAIL \"" + str + "\" x " +\r
- pat + " = " +\r
- showNumber(saw) + ", expected " + showNumber(exp));\r
- }\r
- }\r
-\r
- /**\r
- * Given a string composed of [0-9] and other chars, convert the\r
- * [0-9] chars to be offsets 0..9 from 'zero'.\r
- */\r
- private static String transmute(String str, char zero) {\r
- StringBuffer buf = new StringBuffer();\r
- for (int i=0; i<str.length(); ++i) {\r
- char c = str.charAt(i);\r
- if (c >= '0' && c <= '9') {\r
- c = (char) (c - '0' + zero);\r
- }\r
- buf.append(c);\r
- }\r
- return buf.toString();\r
- }\r
-\r
- public void Test4161100() {\r
- NumberFormat f = NumberFormat.getInstance();\r
- f.setMinimumFractionDigits(1);\r
- f.setMaximumFractionDigits(1);\r
- double a = -0.09;\r
- String s = f.format(a);\r
- logln(a + " x " +\r
- ((DecimalFormat) f).toPattern() + " = " +\r
- s);\r
- if (!s.equals("-0.1")) {\r
- errln("FAIL");\r
- }\r
- } \r
-\r
- public void TestBigDecimalJ28() {\r
- String[] DATA = {\r
- "1", "1E0",\r
- "-1", "-1E0",\r
- "0", "0E0",\r
- "12e34", "1.2E35",\r
- "-12.3e-45", "-1.23E-44",\r
- "0.73e-7", "7.3E-8",\r
- };\r
- NumberFormat fmt = NumberFormat.getScientificInstance(Locale.US);\r
- logln("Pattern: " + ((DecimalFormat)fmt).toPattern());\r
- for (int i=0; i<DATA.length; i+=2) {\r
- String input = DATA[i];\r
- String exp = DATA[i+1];\r
- com.ibm.icu.math.BigDecimal bd = new com.ibm.icu.math.BigDecimal(input);\r
- String output = fmt.format(bd);\r
- if (output.equals(exp)) {\r
- logln("input=" + input + " num=" + bd + " output=" + output);\r
- } else {\r
- errln("FAIL: input=" + input + " num=" + bd + " output=" + output +\r
- " expected=" + exp);\r
- }\r
- }\r
- }\r
-//#if defined(FOUNDATION10)\r
-//#else\r
- public void TestBigDecimalRounding() {\r
- // jb 3657\r
- java.text.DecimalFormat jdkFormat=new java.text.DecimalFormat("###,###,###,##0");\r
- com.ibm.icu.text.DecimalFormat icuFormat=new com.ibm.icu.text.DecimalFormat("###,###,###,##0");\r
- String[] values = {\r
- "-1.74", "-1.24", "-0.74", "-0.24", "0.24", "0.74", "1.24", "1.74"\r
- };\r
- for (int i = 0; i < values.length; ++i) {\r
- String val = values[i];\r
- java.math.BigDecimal bd = new java.math.BigDecimal(val);\r
- String jdk = jdkFormat.format(bd);\r
- String icu = icuFormat.format(bd);\r
- logln("Format of BigDecimal " + val + " by JDK is " + jdk);\r
- logln("Format of BigDecimal " + val + " by ICU is " + icu);\r
- if (!jdk.equals(icu)) {\r
- errln("BigDecimal jdk: " + jdk + " != icu: " + icu);\r
- }\r
-\r
- double d = bd.doubleValue();\r
- jdk = jdkFormat.format(d);\r
- icu = icuFormat.format(d);\r
- logln("Format of double " + val + " by JDK is " + jdk);\r
- logln("Format of double " + val + " by ICU is " + icu);\r
- if (!jdk.equals(icu)) {\r
- errln("double jdk: " + jdk + " != icu: " + icu);\r
- }\r
- }\r
- }\r
-//#endif\r
-}\r
+//##header J2SE15
+/*
+ *******************************************************************************
+ * Copyright (C) 1996-2009, International Business Machines Corporation and *
+ * others. All Rights Reserved. *
+ *******************************************************************************
+ */
+package com.ibm.icu.dev.test.format;
+
+import com.ibm.icu.dev.test.*;
+import com.ibm.icu.text.*;
+import java.text.ParseException;
+import java.util.*;
+import com.ibm.icu.impl.Utility;
+
+/**
+ * @test
+ * General test of Big NumberFormat
+ */
+public class BigNumberFormatTest extends TestFmwk {
+
+ static final int ILLEGAL = -1;
+
+ public static void main(String[] args) throws Exception {
+ new BigNumberFormatTest().run(args);
+ }
+
+ public void TestExponent() {
+ DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
+ DecimalFormat fmt1 = new DecimalFormat("0.###E0", US);
+ DecimalFormat fmt2 = new DecimalFormat("0.###E+0", US);
+ Number n = new Long(1234);
+ expect(fmt1, n, "1.234E3");
+ expect(fmt2, n, "1.234E+3");
+ expect(fmt1, "1.234E3", n);
+ expect(fmt1, "1.234E+3", n); // Either format should parse "E+3"
+ expect(fmt2, "1.234E+3", n);
+ }
+
+ /**
+ * Test the functioning of the secondary grouping value.
+ */
+ public void TestSecondaryGrouping() {
+ DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
+ DecimalFormat f = new DecimalFormat("#,##,###", US);
+ expect(f, new Long(123456789), "12,34,56,789");
+ expectPat(f, "#,##,###");
+ f.applyPattern("#,###");
+ f.setSecondaryGroupingSize(4);
+ expect(f, new Long(123456789), "12,3456,789");
+ expectPat(f, "#,####,###");
+
+ // On Sun JDK 1.2-1.3, the hi_IN locale uses '0' for a zero digit,
+ // but on IBM JDK 1.2-1.3, the locale uses U+0966.
+ f = (DecimalFormat) NumberFormat.getInstance(new Locale("hi", "IN"));
+ String str = transmute("1,87,65,43,210",
+ f.getDecimalFormatSymbols().getZeroDigit());
+ expect(f, new Long(1876543210), str);
+ }
+
+ private void expectPad(DecimalFormat fmt, String pat, int pos) {
+ expectPad(fmt, pat, pos, 0, (char)0);
+ }
+
+ private void expectPad(DecimalFormat fmt, String pat,
+ int pos, int width, char pad) {
+ int apos = 0, awidth = 0;
+ char apad = 0;
+ try {
+ fmt.applyPattern(pat);
+ apos = fmt.getPadPosition();
+ awidth = fmt.getFormatWidth();
+ apad = fmt.getPadCharacter();
+ } catch (IllegalArgumentException e) {
+ apos = -1;
+ awidth = width;
+ apad = pad;
+ }
+ if (apos == pos && awidth == width && apad == pad) {
+ logln("Ok \"" + pat + "\" pos=" + apos +
+ ((pos == -1) ? "" : " width=" + awidth + " pad=" + apad));
+ } else {
+ logln("FAIL \"" + pat + "\" pos=" + apos +
+ " width=" + awidth + " pad=" + apad +
+ ", expected " + pos + " " + width + " " + pad);
+ }
+ }
+
+ /**
+ */
+ public void TestPatterns() {
+ DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
+ DecimalFormat fmt = new DecimalFormat("#", US);
+
+ expectPad(fmt, "*^#", DecimalFormat.PAD_BEFORE_PREFIX, 1, '^');
+ expectPad(fmt, "$*^#", DecimalFormat.PAD_AFTER_PREFIX, 2, '^');
+ expectPad(fmt, "#*^", DecimalFormat.PAD_BEFORE_SUFFIX, 1, '^');
+ expectPad(fmt, "#$*^", DecimalFormat.PAD_AFTER_SUFFIX, 2, '^');
+ expectPad(fmt, "$*^$#", ILLEGAL);
+ expectPad(fmt, "#$*^$", ILLEGAL);
+ expectPad(fmt, "'pre'#,##0*x'post'", DecimalFormat.PAD_BEFORE_SUFFIX,
+ 12, 'x');
+ expectPad(fmt, "''#0*x", DecimalFormat.PAD_BEFORE_SUFFIX,
+ 3, 'x');
+ expectPad(fmt, "'I''ll'*a###.##", DecimalFormat.PAD_AFTER_PREFIX,
+ 10, 'a');
+
+ fmt.applyPattern("AA#,##0.00ZZ");
+ fmt.setPadCharacter('^');
+
+ fmt.setFormatWidth(10);
+
+ fmt.setPadPosition(DecimalFormat.PAD_BEFORE_PREFIX);
+ expectPat(fmt, "*^AA#,##0.00ZZ");
+
+ fmt.setPadPosition(DecimalFormat.PAD_BEFORE_SUFFIX);
+ expectPat(fmt, "AA#,##0.00*^ZZ");
+
+ fmt.setPadPosition(DecimalFormat.PAD_AFTER_SUFFIX);
+ expectPat(fmt, "AA#,##0.00ZZ*^");
+
+ // 12 3456789012
+ String exp = "AA*^#,##0.00ZZ";
+ fmt.setFormatWidth(12);
+ fmt.setPadPosition(DecimalFormat.PAD_AFTER_PREFIX);
+ expectPat(fmt, exp);
+
+ fmt.setFormatWidth(13);
+ // 12 34567890123
+ expectPat(fmt, "AA*^##,##0.00ZZ");
+
+ fmt.setFormatWidth(14);
+ // 12 345678901234
+ expectPat(fmt, "AA*^###,##0.00ZZ");
+
+ fmt.setFormatWidth(15);
+ // 12 3456789012345
+ expectPat(fmt, "AA*^####,##0.00ZZ"); // This is the interesting case
+
+ fmt.setFormatWidth(16);
+ // 12 34567890123456
+ expectPat(fmt, "AA*^#,###,##0.00ZZ");
+ }
+
+ private void expectPat(DecimalFormat fmt, String exp) {
+ String pat = fmt.toPattern();
+ if (pat.equals(exp)) {
+ logln("Ok \"" + pat + '"');
+ } else {
+ errln("FAIL \"" + pat + "\", expected \"" + exp + '"');
+ }
+ }
+
+ /**
+ * Test the handling of the AlphaWorks BigDecimal
+ */
+ public void TestAlphaBigDecimal() {
+ DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
+ /*For ICU compatibility [Richard/GCL]*/
+ expect(NumberFormat.getScientificInstance(Locale.US),
+ new Number[] { new com.ibm.icu.math.BigDecimal("12345.678901"),
+ },
+ "1.2345678901E4");
+ expect(new DecimalFormat("##0.####E0", US),
+ new Number[] { new com.ibm.icu.math.BigDecimal("12345.4999"),
+ new com.ibm.icu.math.BigDecimal("12344.5001"),
+ },
+ "12.345E3");
+ expect(new DecimalFormat("##0.####E0", US),
+ new Number[] { new com.ibm.icu.math.BigDecimal("12345.5000"),
+ new com.ibm.icu.math.BigDecimal("12346.5000"),
+ },
+ "12.346E3");
+ }
+
+ /**
+ */
+ public void TestScientific() {
+ DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
+ /*For ICU compatibility [Richard/GCL]*/
+ expect(NumberFormat.getScientificInstance(Locale.US),
+ new Number[] { new Double(12345.678901),
+ new java.math.BigDecimal("12345.678901"),
+ },
+ "1.2345678901E4");
+ expect(new DecimalFormat("##0.###E0", US),
+ new Double(12345),
+ "12.34E3");
+ expect(new DecimalFormat("##0.###E0", US),
+ new Double(12345.00001),
+ "12.35E3");
+ expect(new DecimalFormat("##0.####E0", US),
+ new Number[] { new Integer(12345),
+ new Long(12345),
+ new java.math.BigDecimal("12345.4999"),
+ new java.math.BigDecimal("12344.5001"),
+ },
+ "12.345E3");
+ expect(new DecimalFormat("##0.####E0", US),
+ new Number[] { new java.math.BigDecimal("12345.5000"),
+ new java.math.BigDecimal("12346.5000"),
+ },
+ "12.346E3");
+ /*For ICU compatibility [Richard/GCL]*/
+ expect(NumberFormat.getScientificInstance(Locale.FRANCE),
+ new Double(12345.678901),
+ "1,2345678901E4");
+ expect(new DecimalFormat("##0.####E0", US),
+ new Double(789.12345e-9),
+ "789.12E-9");
+ expect(new DecimalFormat("##0.####E0", US),
+ new Double(780.e-9),
+ "780E-9");
+ expect(new DecimalFormat(".###E0", US),
+ new Double(45678),
+ ".457E5");
+ expect(new DecimalFormat(".###E0", US),
+ new Long(0),
+ ".0E0");
+ expect(new DecimalFormat[] { new DecimalFormat("#E0", US),
+ new DecimalFormat("##E0", US),
+ new DecimalFormat("####E0", US),
+ new DecimalFormat("0E0", US),
+ new DecimalFormat("00E0", US),
+ new DecimalFormat("000E0", US),
+ },
+ new Long(45678000),
+ new String[] { "4.5678E7",
+ "45.678E6",
+ "4567.8E4",
+ "5E7",
+ "46E6",
+ "457E5",
+ }
+ );
+ expect(new DecimalFormat("###E0", US),
+ new Object[] { new Double(0.0000123), "12.3E-6",
+ new Double(0.000123), "123E-6",
+ new java.math.BigDecimal("0.00123"), "1.23E-3", // Cafe VM messes up Double(0.00123)
+ new Double(0.0123), "12.3E-3",
+ new Double(0.123), "123E-3",
+ new Double(1.23), "1.23E0",
+ new Double(12.3), "12.3E0",
+ new Double(123), "123E0",
+ new Double(1230), "1.23E3",
+ });
+ expect(new DecimalFormat("0.#E+00", US),
+ new Object[] { new Double(0.00012), "1.2E-04",
+ new Long(12000), "1.2E+04",
+ });
+ }
+
+ /**
+ */
+ public void TestPad() {
+ DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
+ expect(new DecimalFormat("*^##.##", US),
+ new Object[] { new Long(0), "^^^^0",
+ new Double(-1.3), "^-1.3",
+ }
+ );
+ expect(new DecimalFormat("##0.0####E0*_ 'g-m/s^2'", US),
+ new Object[] { new Long(0), "0.0E0______ g-m/s^2",
+ new Double(1.0/3), "333.333E-3_ g-m/s^2",
+ }
+ );
+ expect(new DecimalFormat("##0.0####*_ 'g-m/s^2'", US),
+ new Object[] { new Long(0), "0.0______ g-m/s^2",
+ new Double(1.0/3), "0.33333__ g-m/s^2",
+ }
+ );
+ expect(new DecimalFormat("*x#,###,###,##0.00;*x(#,###,###,##0.00)", US),
+ new Object[] {
+ new Long(-100), "xxxxxxxx(100.00)",
+ new Long(-1000), "xxxxxx(1,000.00)",
+ new Long(-1000000), "xx(1,000,000.00)",
+ new Long(-1000000000), "(1,000,000,000.00)",
+ });
+ }
+
+ private void expect(NumberFormat fmt, Object[] data) {
+ for (int i=0; i<data.length; i+=2) {
+ expect(fmt, (Number) data[i], (String) data[i+1]);
+ }
+ }
+
+ private void expect(Object fmto, Object numo, Object expo) {
+ NumberFormat fmt = null, fmts[] = null;
+ Number num = null, nums[] = null;
+ String exp = null, exps[] = null;
+ if (fmto instanceof NumberFormat[]) {
+ fmts = (NumberFormat[]) fmto;
+ } else {
+ fmt = (NumberFormat) fmto;
+ }
+ if (numo instanceof Number[]) {
+ nums = (Number[]) numo;
+ } else {
+ num = (Number) numo;
+ }
+ if (expo instanceof String[]) {
+ exps = (String[]) expo;
+ } else {
+ exp = (String) expo;
+ }
+ int n = 1;
+ if (fmts != null) {
+ n = Math.max(n, fmts.length);
+ }
+ if (nums != null) {
+ n = Math.max(n, nums.length);
+ }
+ if (exps != null) {
+ n = Math.max(n, exps.length);
+ }
+ for (int i=0; i<n; ++i) {
+ expect(fmts == null ? fmt : fmts[i],
+ nums == null ? num : nums[i],
+ exps == null ? exp : exps[i]);
+ }
+ }
+
+ private static String showNumber(Number n) {
+ String cls = n.getClass().getName();
+ if (!(n instanceof com.ibm.icu.math.BigDecimal
+ || n instanceof java.math.BigDecimal)) {
+ int i = cls.lastIndexOf('.');
+ cls = cls.substring(i+1);
+ }
+ return n.toString() + " (" + cls + ')';
+ }
+
+ private void expect(NumberFormat fmt, Number n, String exp) {
+ String saw = fmt.format(n);
+ String pat = ((DecimalFormat) fmt).toPattern();
+ if (saw.equals(exp)) {
+ logln("Ok " + showNumber(n) + " x " +
+ pat + " = " +
+ Utility.escape(saw));
+ } else {
+ errln("FAIL " + showNumber(n) + " x " +
+ pat + " = \"" +
+ Utility.escape(saw) + ", expected " + Utility.escape(exp));
+ }
+ }
+
+ private void expect(NumberFormat fmt, String str, Number exp) {
+ Number saw = null;
+ try {
+ saw = fmt.parse(str);
+ } catch (ParseException e) {
+ saw = null;
+ }
+ String pat = ((DecimalFormat) fmt).toPattern();
+ if (saw.equals(exp)) {
+ logln("Ok \"" + str + "\" x " +
+ pat + " = " +
+ showNumber(saw));
+ } else {
+ errln("FAIL \"" + str + "\" x " +
+ pat + " = " +
+ showNumber(saw) + ", expected " + showNumber(exp));
+ }
+ }
+
+ /**
+ * Given a string composed of [0-9] and other chars, convert the
+ * [0-9] chars to be offsets 0..9 from 'zero'.
+ */
+ private static String transmute(String str, char zero) {
+ StringBuffer buf = new StringBuffer();
+ for (int i=0; i<str.length(); ++i) {
+ char c = str.charAt(i);
+ if (c >= '0' && c <= '9') {
+ c = (char) (c - '0' + zero);
+ }
+ buf.append(c);
+ }
+ return buf.toString();
+ }
+
+ public void Test4161100() {
+ NumberFormat f = NumberFormat.getInstance();
+ f.setMinimumFractionDigits(1);
+ f.setMaximumFractionDigits(1);
+ double a = -0.09;
+ String s = f.format(a);
+ logln(a + " x " +
+ ((DecimalFormat) f).toPattern() + " = " +
+ s);
+ if (!s.equals("-0.1")) {
+ errln("FAIL");
+ }
+ }
+
+ public void TestBigDecimalJ28() {
+ String[] DATA = {
+ "1", "1E0",
+ "-1", "-1E0",
+ "0", "0E0",
+ "12e34", "1.2E35",
+ "-12.3e-45", "-1.23E-44",
+ "0.73e-7", "7.3E-8",
+ };
+ NumberFormat fmt = NumberFormat.getScientificInstance(Locale.US);
+ logln("Pattern: " + ((DecimalFormat)fmt).toPattern());
+ for (int i=0; i<DATA.length; i+=2) {
+ String input = DATA[i];
+ String exp = DATA[i+1];
+ com.ibm.icu.math.BigDecimal bd = new com.ibm.icu.math.BigDecimal(input);
+ String output = fmt.format(bd);
+ if (output.equals(exp)) {
+ logln("input=" + input + " num=" + bd + " output=" + output);
+ } else {
+ errln("FAIL: input=" + input + " num=" + bd + " output=" + output +
+ " expected=" + exp);
+ }
+ }
+ }
+//#if defined(FOUNDATION10)
+//#else
+ public void TestBigDecimalRounding() {
+ // jb 3657
+ java.text.DecimalFormat jdkFormat=new java.text.DecimalFormat("###,###,###,##0");
+ com.ibm.icu.text.DecimalFormat icuFormat=new com.ibm.icu.text.DecimalFormat("###,###,###,##0");
+ String[] values = {
+ "-1.74", "-1.24", "-0.74", "-0.24", "0.24", "0.74", "1.24", "1.74"
+ };
+ for (int i = 0; i < values.length; ++i) {
+ String val = values[i];
+ java.math.BigDecimal bd = new java.math.BigDecimal(val);
+ String jdk = jdkFormat.format(bd);
+ String icu = icuFormat.format(bd);
+ logln("Format of BigDecimal " + val + " by JDK is " + jdk);
+ logln("Format of BigDecimal " + val + " by ICU is " + icu);
+ if (!jdk.equals(icu)) {
+ errln("BigDecimal jdk: " + jdk + " != icu: " + icu);
+ }
+
+ double d = bd.doubleValue();
+ jdk = jdkFormat.format(d);
+ icu = icuFormat.format(d);
+ logln("Format of double " + val + " by JDK is " + jdk);
+ logln("Format of double " + val + " by ICU is " + icu);
+ if (!jdk.equals(icu)) {
+ errln("double jdk: " + jdk + " != icu: " + icu);
+ }
+ }
+ }
+//#endif
+}