LEFT | RIGHT |
(no file at all) | |
1 // © 2016 and later: Unicode, Inc. and others. | 1 // © 2016 and later: Unicode, Inc. and others. |
2 // License & terms of use: http://www.unicode.org/copyright.html#License | 2 // License & terms of use: http://www.unicode.org/copyright.html#License |
3 /*******************************************************************************
********** | 3 /*******************************************************************************
********** |
4 * | 4 * |
5 * Copyright (C) 1996-2012, International Business Machines | 5 * Copyright (C) 1996-2012, International Business Machines |
6 * Corporation and others. All Rights Reserved. | 6 * Corporation and others. All Rights Reserved. |
7 **/ | 7 **/ |
8 | 8 |
9 /** | 9 /** |
10 * Port From: JDK 1.4b1 : java.text.Format.IntlTestDecimalFormatAPI | 10 * Port From: JDK 1.4b1 : java.text.Format.IntlTestDecimalFormatAPI |
11 * Source File: java/text/format/IntlTestDecimalFormatAPI.java | 11 * Source File: java/text/format/IntlTestDecimalFormatAPI.java |
12 **/ | 12 **/ |
13 | 13 |
14 /* | 14 /* |
15 @test 1.4 98/03/06 | 15 @test 1.4 98/03/06 |
16 @summary test International Decimal Format API | 16 @summary test International Decimal Format API |
17 */ | 17 */ |
18 | 18 |
19 package com.ibm.icu.dev.test.format; | 19 package com.ibm.icu.dev.test.format; |
20 | 20 |
21 import java.text.FieldPosition; | 21 import java.text.FieldPosition; |
22 import java.text.Format; | 22 import java.text.Format; |
23 import java.text.ParseException; | 23 import java.text.ParseException; |
24 import java.text.ParsePosition; | 24 import java.text.ParsePosition; |
25 import java.util.Locale; | 25 import java.util.Locale; |
26 | 26 |
27 import org.junit.Test; | 27 import org.junit.Test; |
28 | 28 |
29 import com.ibm.icu.math.BigDecimal; | 29 import com.ibm.icu.math.BigDecimal; |
30 import com.ibm.icu.math.MathContext; | 30 import com.ibm.icu.math.MathContext; |
31 import com.ibm.icu.text.DecimalFormat; | 31 import com.ibm.icu.text.DecimalFormat; |
32 import com.ibm.icu.text.DecimalFormatSymbols; | 32 import com.ibm.icu.text.DecimalFormatSymbols; |
33 import com.ibm.icu.text.NumberFormat; | 33 import com.ibm.icu.text.NumberFormat; |
34 | 34 |
35 public class IntlTestDecimalFormatAPI extends com.ibm.icu.dev.test.TestFmwk | 35 public class IntlTestDecimalFormatAPI extends com.ibm.icu.dev.test.TestFmwk |
36 { | 36 { |
37 /** | 37 /** |
38 * Problem 1: simply running | 38 * Problem 1: simply running |
39 * decF4.setRoundingMode(java.math.BigDecimal.ROUND_HALF_UP) does not work | 39 * decF4.setRoundingMode(java.math.BigDecimal.ROUND_HALF_UP) does not work |
40 * as decF4.setRoundingIncrement(.0001) must also be run. | 40 * as decF4.setRoundingIncrement(.0001) must also be run. |
41 * Problem 2: decF4.format(8.88885) does not return 8.8889 as expected. | 41 * Problem 2: decF4.format(8.88885) does not return 8.8889 as expected. |
42 * You must run decF4.format(new BigDecimal(Double.valueOf(8.88885))) in | 42 * You must run decF4.format(new BigDecimal(Double.valueOf(8.88885))) in |
43 * order for this to work as expected. | 43 * order for this to work as expected. |
44 * Problem 3: There seems to be no way to set half up to be the default | 44 * Problem 3: There seems to be no way to set half up to be the default |
45 * rounding mode. | 45 * rounding mode. |
46 * We solved the problem with the code at the bottom of this page however | 46 * We solved the problem with the code at the bottom of this page however |
47 * this is not quite general purpose enough to include in icu4j. A static | 47 * this is not quite general purpose enough to include in icu4j. A static |
48 * setDefaultRoundingMode function would solve the problem nicely. Also | 48 * setDefaultRoundingMode function would solve the problem nicely. Also |
49 * decimal places past 20 are not handled properly. A small ammount of work | 49 * decimal places past 20 are not handled properly. A small ammount of work |
50 * would make bring this up to snuff. | 50 * would make bring this up to snuff. |
51 */ | 51 */ |
52 @Test | 52 @Test |
53 public void testJB1871() | 53 public void testJB1871() |
54 { | 54 { |
55 // problem 2 | 55 // problem 2 |
56 double number = 8.88885; | 56 double number = 8.88885; |
57 String expected = "8.8889"; | 57 String expected = "8.8889"; |
58 | 58 |
59 String pat = ",##0.0000"; | 59 String pat = ",##0.0000"; |
60 DecimalFormat dec = new DecimalFormat(pat); | 60 DecimalFormat dec = new DecimalFormat(pat); |
61 dec.setRoundingMode(BigDecimal.ROUND_HALF_UP); | 61 dec.setRoundingMode(BigDecimal.ROUND_HALF_UP); |
62 double roundinginc = 0.0001; | 62 double roundinginc = 0.0001; |
63 dec.setRoundingIncrement(roundinginc); | 63 dec.setRoundingIncrement(roundinginc); |
64 String str = dec.format(number); | 64 String str = dec.format(number); |
65 if (!str.equals(expected)) { | 65 if (!str.equals(expected)) { |
66 errln("Fail: " + number + " x \"" + pat + "\" = \"" + | 66 errln("Fail: " + number + " x \"" + pat + "\" = \"" + |
67 str + "\", expected \"" + expected + "\""); | 67 str + "\", expected \"" + expected + "\""); |
68 } | 68 } |
69 | 69 |
70 pat = ",##0.0001"; | 70 pat = ",##0.0001"; |
71 dec = new DecimalFormat(pat); | 71 dec = new DecimalFormat(pat); |
72 dec.setRoundingMode(BigDecimal.ROUND_HALF_UP); | 72 dec.setRoundingMode(BigDecimal.ROUND_HALF_UP); |
73 str = dec.format(number); | 73 str = dec.format(number); |
74 if (!str.equals(expected)) { | 74 if (!str.equals(expected)) { |
75 errln("Fail: " + number + " x \"" + pat + "\" = \"" + | 75 errln("Fail: " + number + " x \"" + pat + "\" = \"" + |
76 str + "\", expected \"" + expected + "\""); | 76 str + "\", expected \"" + expected + "\""); |
77 } | 77 } |
78 | 78 |
79 // testing 20 decimal places | 79 // testing 20 decimal places |
80 pat = ",##0.00000000000000000001"; | 80 pat = ",##0.00000000000000000001"; |
81 dec = new DecimalFormat(pat); | 81 dec = new DecimalFormat(pat); |
82 BigDecimal bignumber = new BigDecimal("8.888888888888888888885"); | 82 BigDecimal bignumber = new BigDecimal("8.888888888888888888885"); |
83 expected = "8.88888888888888888889"; | 83 expected = "8.88888888888888888889"; |
84 | 84 |
85 dec.setRoundingMode(BigDecimal.ROUND_HALF_UP); | 85 dec.setRoundingMode(BigDecimal.ROUND_HALF_UP); |
86 str = dec.format(bignumber); | 86 str = dec.format(bignumber); |
87 if (!str.equals(expected)) { | 87 if (!str.equals(expected)) { |
88 errln("Fail: " + bignumber + " x \"" + pat + "\" = \"" + | 88 errln("Fail: " + bignumber + " x \"" + pat + "\" = \"" + |
89 str + "\", expected \"" + expected + "\""); | 89 str + "\", expected \"" + expected + "\""); |
90 } | 90 } |
91 | 91 |
92 } | 92 } |
93 | 93 |
94 /** | 94 /** |
95 * This test checks various generic API methods in DecimalFormat to achieve | 95 * This test checks various generic API methods in DecimalFormat to achieve |
96 * 100% API coverage. | 96 * 100% API coverage. |
97 */ | 97 */ |
98 @Test | 98 @Test |
99 public void TestAPI() | 99 public void TestAPI() |
100 { | 100 { |
101 logln("DecimalFormat API test---"); logln(""); | 101 logln("DecimalFormat API test---"); logln(""); |
102 Locale.setDefault(Locale.ENGLISH); | 102 Locale.setDefault(Locale.ENGLISH); |
103 | 103 |
104 // ======= Test constructors | 104 // ======= Test constructors |
105 | 105 |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 " / fposByField: " + fposByField.getEndIndex()); | 291 " / fposByField: " + fposByField.getEndIndex()); |
292 } | 292 } |
293 } | 293 } |
294 | 294 |
295 @Test | 295 @Test |
296 public void testJB4971() | 296 public void testJB4971() |
297 { | 297 { |
298 DecimalFormat decfmt = new DecimalFormat(); | 298 DecimalFormat decfmt = new DecimalFormat(); |
299 MathContext resultICU; | 299 MathContext resultICU; |
300 | 300 |
301 MathContext comp1 = new MathContext(0, MathContext.PLAIN); | 301 MathContext comp1 = new MathContext(0, MathContext.PLAIN, false, MathCon
text.ROUND_HALF_EVEN); |
302 resultICU = decfmt.getMathContextICU(); | 302 resultICU = decfmt.getMathContextICU(); |
303 if ((comp1.getDigits() != resultICU.getDigits()) || | 303 if ((comp1.getDigits() != resultICU.getDigits()) || |
304 (comp1.getForm() != resultICU.getForm()) || | 304 (comp1.getForm() != resultICU.getForm()) || |
305 (comp1.getLostDigits() != resultICU.getLostDigits()) || | 305 (comp1.getLostDigits() != resultICU.getLostDigits()) || |
306 (comp1.getRoundingMode() != resultICU.getRoundingMode())) | 306 (comp1.getRoundingMode() != resultICU.getRoundingMode())) |
307 { | 307 { |
308 errln("ERROR: Math context 1 not equal - result: " + resultICU.toStr
ing() + | 308 errln("ERROR: Math context 1 not equal - result: " + resultICU.toStr
ing() + |
309 " / expected: " + comp1.toString()); | 309 " / expected: " + comp1.toString()); |
310 } | 310 } |
311 | 311 |
312 MathContext comp2 = new MathContext(5, MathContext.ENGINEERING); | 312 MathContext comp2 = new MathContext(5, MathContext.ENGINEERING, false, M
athContext.ROUND_HALF_EVEN); |
313 decfmt.setMathContextICU(comp2); | 313 decfmt.setMathContextICU(comp2); |
314 resultICU = decfmt.getMathContextICU(); | 314 resultICU = decfmt.getMathContextICU(); |
315 if ((comp2.getDigits() != resultICU.getDigits()) || | 315 if ((comp2.getDigits() != resultICU.getDigits()) || |
316 (comp2.getForm() != resultICU.getForm()) || | 316 (comp2.getForm() != resultICU.getForm()) || |
317 (comp2.getLostDigits() != resultICU.getLostDigits()) || | 317 (comp2.getLostDigits() != resultICU.getLostDigits()) || |
318 (comp2.getRoundingMode() != resultICU.getRoundingMode())) | 318 (comp2.getRoundingMode() != resultICU.getRoundingMode())) |
319 { | 319 { |
320 errln("ERROR: Math context 2 not equal - result: " + resultICU.toStr
ing() + | 320 errln("ERROR: Math context 2 not equal - result: " + resultICU.toStr
ing() + |
321 " / expected: " + comp2.toString()); | 321 " / expected: " + comp2.toString()); |
322 } | 322 } |
(...skipping 14 matching lines...) Expand all Loading... |
337 | 337 |
338 @Test | 338 @Test |
339 public void testJB6354() | 339 public void testJB6354() |
340 { | 340 { |
341 DecimalFormat pat = new DecimalFormat("#,##0.00"); | 341 DecimalFormat pat = new DecimalFormat("#,##0.00"); |
342 java.math.BigDecimal r1, r2; | 342 java.math.BigDecimal r1, r2; |
343 | 343 |
344 // get default rounding increment | 344 // get default rounding increment |
345 r1 = pat.getRoundingIncrement(); | 345 r1 = pat.getRoundingIncrement(); |
346 | 346 |
347 // set rounding mode with zero increment. Rounding | 347 // set rounding mode with zero increment. Rounding |
348 // increment should be set by this operation | 348 // increment should be set by this operation |
349 pat.setRoundingMode(BigDecimal.ROUND_UP); | 349 pat.setRoundingMode(BigDecimal.ROUND_UP); |
350 r2 = pat.getRoundingIncrement(); | 350 r2 = pat.getRoundingIncrement(); |
351 | 351 |
352 // check for different values | 352 // check for different values |
353 if ((r1 != null) && (r2 != null)) | 353 if ((r1 != null) && (r2 != null)) |
354 { | 354 { |
355 if (r1.compareTo(r2) == 0) | 355 if (r1.compareTo(r2) == 0) |
356 { | 356 { |
357 errln("ERROR: Rounding increment did not change"); | 357 errln("ERROR: Rounding increment did not change"); |
358 } | 358 } |
359 } | 359 } |
360 } | 360 } |
361 | 361 |
362 @Test | 362 @Test |
363 public void testJB6648() | 363 public void testJB6648() |
364 { | 364 { |
365 DecimalFormat df = new DecimalFormat(); | 365 DecimalFormat df = new DecimalFormat(); |
366 df.setParseStrict(true); | 366 df.setParseStrict(true); |
367 | 367 |
368 String numstr = new String(); | 368 String numstr = new String(); |
369 | 369 |
370 String[] patterns = { | 370 String[] patterns = { |
371 "0", | 371 "0", |
372 "00", | 372 "00", |
373 "000", | 373 "000", |
374 "0,000", | 374 "0,000", |
375 "0.0", | 375 "0.0", |
376 "#000.0" | 376 "#000.0" |
377 }; | 377 }; |
378 | 378 |
379 for(int i=0; i < patterns.length; i++) { | 379 for(int i=0; i < patterns.length; i++) { |
380 df.applyPattern(patterns[i]); | 380 df.applyPattern(patterns[i]); |
381 numstr = df.format(5); | 381 numstr = df.format(5); |
382 try { | 382 try { |
383 Number n = df.parse(numstr); | 383 Number n = df.parse(numstr); |
384 logln("INFO: Parsed " + numstr + " -> " + n); | 384 logln("INFO: Parsed " + numstr + " -> " + n); |
385 } catch (ParseException pe) { | 385 } catch (ParseException pe) { |
386 errln("ERROR: Failed round trip with strict parsing."); | 386 errln("ERROR: Failed round trip with strict parsing."); |
387 }··········· | 387 } |
388 } | 388 } |
389 ········ | 389 |
390 df.applyPattern(patterns[1]); | 390 df.applyPattern(patterns[1]); |
391 numstr = "005"; | 391 numstr = "005"; |
392 try { | 392 try { |
393 Number n = df.parse(numstr); | 393 Number n = df.parse(numstr); |
394 logln("INFO: Successful parse for " + numstr + " with strict parse e
nabled. Number is " + n); | 394 logln("INFO: Successful parse for " + numstr + " with strict parse e
nabled. Number is " + n); |
395 } catch (ParseException pe) { | 395 } catch (ParseException pe) { |
396 errln("ERROR: Parse Exception encountered in strict mode: numstr ->
" + numstr); | 396 errln("ERROR: Parse Exception encountered in strict mode: numstr ->
" + numstr); |
397 } | 397 } |
398 | 398 |
399 } | 399 } |
400 } | 400 } |
LEFT | RIGHT |