001: /*
002: * Copyright 2006 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016:
017: package com.google.gwt.i18n.client;
018:
019: import com.google.gwt.junit.client.GWTTestCase;
020:
021: /**
022: * GWT JUnit tests must extend GWTTestCase.
023: */
024: public class NumberFormat_en_Test extends GWTTestCase {
025:
026: /**
027: * Must refer to a valid module that inherits from com.google.gwt.junit.JUnit.
028: */
029: public String getModuleName() {
030: return "com.google.gwt.i18n.I18NTest_en";
031: }
032:
033: public void testAPIs() {
034: NumberFormat formatter;
035: String str;
036:
037: formatter = NumberFormat.getFormat("\u00a4#,###.000");
038: str = formatter.format(123456.7899);
039: assertEquals("$123,456.790", str);
040:
041: formatter = NumberFormat.getFormat("\u00a4#,###.000", "BRL");
042: str = formatter.format(123456.7899);
043: assertEquals("R$123,456.790", str);
044:
045: NumberFormat currencyFormat = NumberFormat.getCurrencyFormat();
046: str = currencyFormat.format(123456.7899);
047: assertEquals("$123,456.79", str);
048:
049: formatter = NumberFormat.getFormat(currencyFormat.getPattern(),
050: "BRL");
051: str = formatter.format(123456.7899);
052: assertEquals("R$123,456.79", str);
053: }
054:
055: /**
056: * Add as many tests as you like.
057: */
058: public void testBasicFormat() {
059: String str = NumberFormat.getFormat("0.0000").format(
060: 123.45789179565757f);
061: assertTrue(str.equals("123.4579"));
062: }
063:
064: public void testCurrency() {
065: String str;
066:
067: str = NumberFormat.getFormat("\u00a4#,##0.00;-\u00a4#,##0.00")
068: .format(1234.56);
069: assertTrue(str.equals("$1,234.56"));
070: str = NumberFormat.getFormat("\u00a4#,##0.00;-\u00a4#,##0.00")
071: .format(-1234.56);
072: assertTrue(str.equals("-$1,234.56"));
073:
074: str = NumberFormat.getFormat(
075: "\u00a4\u00a4 #,##0.00;-\u00a4\u00a4 #,##0.00").format(
076: 1234.56);
077: assertTrue(str.equals("USD 1,234.56"));
078: str = NumberFormat.getFormat(
079: "\u00a4\u00a4 #,##0.00;\u00a4\u00a4 -#,##0.00").format(
080: -1234.56);
081: assertTrue(str.equals("USD -1,234.56"));
082:
083: NumberFormat formatter = NumberFormat.getFormat(
084: "\u00a4#,##0.00;-\u00a4#,##0.00", "BRL");
085: str = formatter.format(1234.56);
086: assertTrue(str.equals("R$1,234.56"));
087: str = formatter.format(-1234.56);
088: assertTrue(str.equals("-R$1,234.56"));
089:
090: formatter = NumberFormat.getFormat(
091: "\u00a4\u00a4 #,##0.00;(\u00a4\u00a4 #,##0.00)", "BRL");
092: str = formatter.format(1234.56);
093: assertTrue(str.equals("BRL 1,234.56"));
094: str = formatter.format(-1234.56);
095: assertTrue(str.equals("(BRL 1,234.56)"));
096: }
097:
098: public void testExponential() {
099: String str;
100:
101: str = NumberFormat.getFormat("0.####E0").format(0.01234);
102: assertTrue(str.equals("1.234E-2"));
103: str = NumberFormat.getFormat("00.000E00").format(0.01234);
104: assertTrue(str.equals("12.340E-03"));
105: str = NumberFormat.getFormat("##0.######E000").format(0.01234);
106: assertTrue(str.equals("12.34E-003"));
107: str = NumberFormat.getFormat("0.###E0;[0.###E0]").format(
108: 0.01234);
109: assertTrue(str.equals("1.234E-2"));
110:
111: str = NumberFormat.getFormat("0.####E0").format(123456789);
112: assertTrue(str.equals("1.2346E8"));
113: str = NumberFormat.getFormat("00.000E00").format(123456789);
114: assertTrue(str.equals("12.346E07"));
115: str = NumberFormat.getFormat("##0.######E000")
116: .format(123456789);
117: assertTrue(str.equals("123.456789E006"));
118: str = NumberFormat.getFormat("0.###E0;[0.###E0]").format(
119: 123456789);
120: assertTrue(str.equals("1.235E8"));
121:
122: str = NumberFormat.getFormat("0.####E0").format(1.23e300);
123: assertTrue(str.equals("1.23E300"));
124: str = NumberFormat.getFormat("00.000E00").format(1.23e300);
125: assertTrue(str.equals("12.300E299"));
126: str = NumberFormat.getFormat("##0.######E000").format(1.23e300);
127: assertTrue(str.equals("1.23E300"));
128: str = NumberFormat.getFormat("0.###E0;[0.###E0]").format(
129: 1.23e300);
130: assertTrue(str.equals("1.23E300"));
131:
132: str = NumberFormat.getFormat("0.####E0").format(
133: -3.141592653e-271);
134: assertTrue(str.equals("-3.1416E-271"));
135: str = NumberFormat.getFormat("00.000E00").format(
136: -3.141592653e-271);
137: assertTrue(str.equals("-31.416E-272"));
138: str = NumberFormat.getFormat("##0.######E000").format(
139: -3.141592653e-271);
140: assertTrue(str.equals("-314.159265E-273"));
141: str = NumberFormat.getFormat("0.###E0;[0.###E0]").format(
142: -3.141592653e-271);
143: assertTrue(str.equals("[3.142E-271]"));
144:
145: str = NumberFormat.getFormat("0.####E0").format(0);
146: assertTrue(str.equals("0E0"));
147: str = NumberFormat.getFormat("00.000E00").format(0);
148: assertTrue(str.equals("00.000E00"));
149: str = NumberFormat.getFormat("##0.######E000").format(0);
150: assertTrue(str.equals("0E000"));
151: str = NumberFormat.getFormat("0.###E0;[0.###E0]").format(0);
152: assertTrue(str.equals("0E0"));
153:
154: str = NumberFormat.getFormat("0.####E0").format(-1);
155: assertTrue(str.equals("-1E0"));
156: str = NumberFormat.getFormat("00.000E00").format(-1);
157: assertTrue(str.equals("-10.000E-01"));
158: str = NumberFormat.getFormat("##0.######E000").format(-1);
159: assertTrue(str.equals("-1E000"));
160: str = NumberFormat.getFormat("0.###E0;[0.###E0]").format(-1);
161: assertTrue(str.equals("[1E0]"));
162:
163: str = NumberFormat.getFormat("0.####E0").format(1);
164: assertTrue(str.equals("1E0"));
165: str = NumberFormat.getFormat("00.000E00").format(1);
166: assertTrue(str.equals("10.000E-01"));
167: str = NumberFormat.getFormat("##0.######E000").format(1);
168: assertTrue(str.equals("1E000"));
169: str = NumberFormat.getFormat("0.###E0;[0.###E0]").format(1);
170: assertTrue(str.equals("1E0"));
171:
172: str = NumberFormat.getFormat("#E0").format(12345.0);
173: assertTrue(str.equals("1E4"));
174: str = NumberFormat.getFormat("0E0").format(12345.0);
175: assertTrue(str.equals("1E4"));
176: str = NumberFormat.getFormat("##0.###E0").format(12345.0);
177: assertTrue(str.equals("12.345E3"));
178: str = NumberFormat.getFormat("##0.###E0").format(12345.00001);
179: assertTrue(str.equals("12.345E3"));
180: str = NumberFormat.getFormat("##0.###E0").format(12345);
181: assertTrue(str.equals("12.345E3"));
182:
183: str = NumberFormat.getFormat("##0.####E0").format(789.12345e-9);
184: assertTrue(str.equals("789.1235E-9"));
185: str = NumberFormat.getFormat("##0.####E0").format(780.e-9);
186: assertTrue(str.equals("780E-9"));
187: str = NumberFormat.getFormat(".###E0").format(45678.0);
188: assertTrue(str.equals(".457E5"));
189: str = NumberFormat.getFormat(".###E0").format(0);
190: assertTrue(str.equals(".0E0"));
191:
192: str = NumberFormat.getFormat("#E0").format(45678000);
193: assertTrue(str.equals("5E7"));
194: str = NumberFormat.getFormat("##E0").format(45678000);
195: assertTrue(str.equals("46E6"));
196: str = NumberFormat.getFormat("####E0").format(45678000);
197: assertTrue(str.equals("4568E4"));
198: str = NumberFormat.getFormat("0E0").format(45678000);
199: assertTrue(str.equals("5E7"));
200: str = NumberFormat.getFormat("00E0").format(45678000);
201: assertTrue(str.equals("46E6"));
202: str = NumberFormat.getFormat("000E0").format(45678000);
203: assertTrue(str.equals("457E5"));
204: str = NumberFormat.getFormat("###E0").format(0.0000123);
205: assertTrue(str.equals("12E-6"));
206: str = NumberFormat.getFormat("###E0").format(0.000123);
207: assertTrue(str.equals("123E-6"));
208: str = NumberFormat.getFormat("###E0").format(0.00123);
209: assertTrue(str.equals("1E-3"));
210: str = NumberFormat.getFormat("###E0").format(0.0123);
211: assertTrue(str.equals("12E-3"));
212: str = NumberFormat.getFormat("###E0").format(0.123);
213: assertTrue(str.equals("123E-3"));
214: str = NumberFormat.getFormat("###E0").format(1.23);
215: assertTrue(str.equals("1E0"));
216: str = NumberFormat.getFormat("###E0").format(12.3);
217: assertTrue(str.equals("12E0"));
218: str = NumberFormat.getFormat("###E0").format(123.0);
219: assertTrue(str.equals("123E0"));
220: str = NumberFormat.getFormat("###E0").format(1230.0);
221: assertTrue(str.equals("1E3"));
222: }
223:
224: public void testExponentParse() {
225: double value;
226:
227: value = NumberFormat.getFormat("#E0").parse("1.234E3");
228: assertTrue(value == 1.234E+3);
229:
230: value = NumberFormat.getFormat("0.###E0").parse("1.234E3");
231: assertTrue(value == 1.234E+3);
232:
233: value = NumberFormat.getFormat("#E0").parse("1.2345E4");
234: assertTrue(value == 12345.0);
235:
236: value = NumberFormat.getFormat("0E0").parse("1.2345E4");
237: assertTrue(value == 12345.0);
238:
239: value = NumberFormat.getFormat("0E0").parse("1.2345E+4");
240: assertTrue(value == 12345.0);
241: }
242:
243: public void testGrouping() {
244: String str;
245:
246: str = NumberFormat.getFormat("#,###").format(1234567890);
247: assertTrue(str.equals("1,234,567,890"));
248: str = NumberFormat.getFormat("#,####").format(1234567890);
249: assertTrue(str.equals("12,3456,7890"));
250:
251: str = NumberFormat.getFormat("#").format(1234567890);
252: assertTrue(str.equals("1234567890"));
253: }
254:
255: public void testPerMill() {
256: String str;
257:
258: str = NumberFormat.getFormat("###.###\u2030").format(0.4857);
259: assertTrue(str.equals("485.7\u2030"));
260: }
261:
262: public void testQuotes() {
263: String str;
264:
265: str = NumberFormat.getFormat("a'fo''o'b#").format(123);
266: assertTrue(str.equals("afo'ob123"));
267:
268: str = NumberFormat.getFormat("a''b#").format(123);
269: assertTrue(str.equals("a'b123"));
270: }
271:
272: public void testStandardFormat() {
273: String str;
274:
275: str = NumberFormat.getCurrencyFormat().format(1234.579);
276: assertTrue(str.equals("$1,234.58"));
277: str = NumberFormat.getDecimalFormat().format(1234.579);
278: assertTrue(str.equals("1,234.579"));
279: str = NumberFormat.getPercentFormat().format(1234.579);
280: assertTrue(str.equals("123,458%"));
281: str = NumberFormat.getScientificFormat().format(1234.579);
282: assertTrue(str.equals("1.235E3"));
283: }
284:
285: public void testZeros() {
286: String str;
287:
288: str = NumberFormat.getFormat("#.#").format(0);
289: assertTrue(str.equals("0"));
290: str = NumberFormat.getFormat("#.").format(0);
291: assertTrue(str.equals("0."));
292: str = NumberFormat.getFormat(".#").format(0);
293: assertTrue(str.equals(".0"));
294: str = NumberFormat.getFormat("#").format(0);
295: assertTrue(str.equals("0"));
296:
297: str = NumberFormat.getFormat("#0.#").format(0);
298: assertTrue(str.equals("0"));
299: str = NumberFormat.getFormat("#0.").format(0);
300: assertTrue(str.equals("0."));
301: str = NumberFormat.getFormat("#.0").format(0);
302: assertTrue(str.equals(".0"));
303: str = NumberFormat.getFormat("#").format(0);
304: assertTrue(str.equals("0"));
305: }
306: }
|