1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.harmony.tests.java.text; 19 20 import java.io.ObjectInputStream; 21 import java.math.BigDecimal; 22 import java.math.BigInteger; 23 import java.math.RoundingMode; 24 import java.text.AttributedCharacterIterator; 25 import java.text.DecimalFormat; 26 import java.text.DecimalFormatSymbols; 27 import java.text.FieldPosition; 28 import java.text.NumberFormat; 29 import java.text.ParsePosition; 30 import java.util.ArrayList; 31 import java.util.Currency; 32 import java.util.List; 33 import java.util.Locale; 34 35 import junit.framework.Assert; 36 import junit.framework.AssertionFailedError; 37 import junit.framework.TestCase; 38 39 import org.apache.harmony.testframework.serialization.SerializationTest; 40 41 42 public class DecimalFormatTest extends TestCase { 43 44 // https://code.google.com/p/android/issues/detail?id=59600 test_setNan_emptyString()45 public void test_setNan_emptyString() throws Exception { 46 DecimalFormatSymbols dfs = new DecimalFormatSymbols(); 47 dfs.setNaN(""); 48 DecimalFormat df = new DecimalFormat(); 49 df.setDecimalFormatSymbols(dfs); 50 df.format(Double.NaN); 51 } 52 testAttributedCharacterIterator()53 public void testAttributedCharacterIterator() throws Exception { 54 // Regression for http://issues.apache.org/jira/browse/HARMONY-333 55 AttributedCharacterIterator iterator = new DecimalFormat().formatToCharacterIterator( 56 new Integer(1)); 57 assertNotNull(iterator); 58 assertFalse("attributes should exist", iterator.getAttributes().isEmpty()); 59 } 60 test_parse_bigDecimal()61 public void test_parse_bigDecimal() throws Exception { 62 // parseBigDecimal default to false 63 DecimalFormat form = (DecimalFormat) DecimalFormat.getInstance(Locale.US); 64 assertFalse(form.isParseBigDecimal()); 65 form.setParseBigDecimal(true); 66 assertTrue(form.isParseBigDecimal()); 67 68 Number result = form.parse("123.123"); 69 assertEquals(new BigDecimal("123.123"), result); 70 71 form.setParseBigDecimal(false); 72 assertFalse(form.isParseBigDecimal()); 73 74 result = form.parse("123.123"); 75 assertFalse(result instanceof BigDecimal); 76 } 77 test_parse_integerOnly()78 public void test_parse_integerOnly() throws Exception { 79 DecimalFormat format = new DecimalFormat(); 80 assertFalse("Default value of isParseIntegerOnly is true", format.isParseIntegerOnly()); 81 82 format.setParseIntegerOnly(true); 83 assertTrue(format.isParseIntegerOnly()); 84 Number result = format.parse("123.123"); 85 assertEquals(new Long("123"), result); 86 87 format.setParseIntegerOnly(false); 88 assertFalse(format.isParseIntegerOnly()); 89 result = format.parse("123.123"); 90 assertEquals(new Double("123.123"), result); 91 } 92 93 // Test the type of the returned object test_parse_returnType()94 public void test_parse_returnType() { 95 DecimalFormat form = (DecimalFormat) NumberFormat.getInstance(Locale.US); 96 Number number = form.parse("23.1", new ParsePosition(0)); 97 assertTrue(number instanceof Double); 98 99 // Test parsed object of type double when 100 // parseBigDecimal is set to true 101 102 form = (DecimalFormat) NumberFormat.getInstance(Locale.US); 103 number = form.parse("23.1", new ParsePosition(0)); 104 assertTrue(number instanceof Double); 105 106 form.setParseBigDecimal(true); 107 number = form.parse("23.1", new ParsePosition(0)); 108 109 assertTrue(number instanceof BigDecimal); 110 assertEquals(new BigDecimal("23.1"), number); 111 112 // When parseIntegerOnly set to true, all numbers will be parsed 113 // into Long unless the value is out of the bound of Long or 114 // some special values such as NaN or Infinity. 115 116 form = (DecimalFormat) NumberFormat.getInstance(Locale.US); 117 form.setParseIntegerOnly(true); 118 number = form.parse("23.1f", new ParsePosition(0)); 119 120 assertTrue(number instanceof Long); 121 122 number = form.parse("23.0", new ParsePosition(0)); 123 assertTrue(number instanceof Long); 124 125 number = form.parse("-0.0", new ParsePosition(0)); 126 assertTrue(number instanceof Long); 127 assertTrue(new Long(0).equals(number)); 128 129 // The last integers representable by long. 130 number = form.parse("9223372036854775807.00", new ParsePosition(0)); 131 assertEquals(Long.class, number.getClass()); 132 number = form.parse("9223372036854775808.00", new ParsePosition(0)); 133 assertEquals(Double.class, number.getClass()); 134 // The first integers that need to be represented by double. 135 number = form.parse("-9223372036854775808.00", new ParsePosition(0)); 136 assertEquals(Long.class, number.getClass()); 137 number = form.parse("-9223372036854775809.00", new ParsePosition(0)); 138 assertEquals(Double.class, number.getClass()); 139 140 // Even if parseIntegerOnly is set to true, NaN will be parsed to Double 141 142 form = (DecimalFormat) NumberFormat.getInstance(Locale.US); 143 form.setParseIntegerOnly(true); 144 DecimalFormatSymbols symbols = new DecimalFormatSymbols(); 145 number = form.parse(symbols.getNaN(), new ParsePosition(0)); 146 assertTrue(number instanceof Double); 147 148 // Even if parseIntegerOnly is set to true, Infinity will still be 149 // parsed to Double 150 151 form = (DecimalFormat) NumberFormat.getInstance(Locale.US); 152 form.setParseIntegerOnly(true); 153 symbols = new DecimalFormatSymbols(); 154 number = form.parse(symbols.getInfinity(), new ParsePosition(0)); 155 assertTrue(number instanceof Double); 156 157 // ParseBigDecimal take precedence of parseBigInteger 158 159 form = (DecimalFormat) NumberFormat.getInstance(Locale.US); 160 form.setParseIntegerOnly(true); 161 form.setParseBigDecimal(true); 162 163 number = form.parse("23.1f", new ParsePosition(0)); 164 165 assertTrue(number instanceof BigDecimal); 166 167 number = form.parse("23.0", new ParsePosition(0)); 168 assertTrue(number instanceof BigDecimal); 169 170 number = form.parse("-92,233,720,368,547,758,080.00", new ParsePosition(0)); 171 assertFalse(number instanceof BigInteger); 172 assertTrue(number instanceof BigDecimal); 173 174 // Test whether the parsed object is of type float. (To be specific, 175 // they are of type Double) 176 177 form = (DecimalFormat) NumberFormat.getInstance(Locale.US); 178 179 number = form.parse("23.1f", new ParsePosition(0)); 180 assertTrue(number instanceof Double); 181 182 form.setParseBigDecimal(true); 183 number = form.parse("23.1f", new ParsePosition(0)); 184 assertTrue(number instanceof BigDecimal); 185 assertEquals(new BigDecimal("23.1"), number); 186 187 // Integer will be parsed to Long, unless parseBigDecimal is set to true 188 189 form = (DecimalFormat) NumberFormat.getInstance(Locale.US); 190 191 number = form.parse("123", new ParsePosition(0)); 192 assertTrue(number instanceof Long); 193 194 form.setParseBigDecimal(true); 195 number = form.parse("123", new ParsePosition(0)); 196 assertTrue(number instanceof BigDecimal); 197 assertEquals(new BigDecimal("123"), number); 198 199 // NaN will be parsed to Double, no matter parseBigDecimal set or not. 200 201 form = (DecimalFormat) NumberFormat.getInstance(Locale.US); 202 symbols = new DecimalFormatSymbols(); 203 number = form.parse(symbols.getNaN() + "", new ParsePosition(0)); 204 assertTrue(number instanceof Double); 205 206 form.setParseBigDecimal(true); 207 number = form.parse(symbols.getNaN() + "", new ParsePosition(0)); 208 assertTrue(number instanceof Double); 209 210 // Infinity will be parsed to Double, no matter parseBigDecimal set or 211 // not. 212 213 form = (DecimalFormat) NumberFormat.getInstance(Locale.US); 214 symbols = new DecimalFormatSymbols(); 215 216 number = form.parse(symbols.getInfinity(), new ParsePosition(0)); 217 218 assertTrue(number instanceof Double); 219 assertEquals("Infinity", number.toString()); 220 // When set bigDecimal to true, the result of parsing infinity 221 222 form = (DecimalFormat) NumberFormat.getInstance(Locale.US); 223 symbols = new DecimalFormatSymbols(); 224 form.setParseBigDecimal(true); 225 226 number = form.parse(symbols.getInfinity(), new ParsePosition(0)); 227 assertTrue(number instanceof Double); 228 assertEquals("Infinity", number.toString()); 229 230 // Negative infinity will be parsed to double no matter parseBigDecimal 231 // set or not 232 233 form = (DecimalFormat) NumberFormat.getInstance(Locale.US); 234 symbols = new DecimalFormatSymbols(); 235 236 number = form.parse("-" + symbols.getInfinity(), new ParsePosition(0)); 237 238 assertTrue(number instanceof Double); 239 assertEquals("-Infinity", number.toString()); 240 241 // When set bigDecimal to true, the result of parsing minus infinity 242 243 form = (DecimalFormat) NumberFormat.getInstance(Locale.US); 244 symbols = new DecimalFormatSymbols(); 245 form.setParseBigDecimal(true); 246 247 number = form.parse("-" + symbols.getInfinity(), new ParsePosition(0)); 248 249 assertTrue(number instanceof Double); 250 assertEquals("-Infinity", number.toString()); 251 252 // -0.0 will be parsed to different type according to the combination of 253 // parseBigDecimal and parseIntegerOnly 254 255 form = (DecimalFormat) NumberFormat.getInstance(Locale.US); 256 257 // parseBigDecimal == true; 258 // parseIntegerOnly == false; 259 form.setParseBigDecimal(true); 260 number = form.parse("-0", new ParsePosition(0)); 261 assertTrue(number instanceof BigDecimal); 262 263 number = form.parse("-0.0", new ParsePosition(0)); 264 assertTrue(number instanceof BigDecimal); 265 266 // parseBigDecimal == false; 267 // parseIntegerOnly == true; 268 form.setParseBigDecimal(false); 269 form.setParseIntegerOnly(true); 270 number = form.parse("-0", new ParsePosition(0)); 271 272 assertTrue(number instanceof Long); 273 274 number = form.parse("-0.0", new ParsePosition(0)); 275 assertTrue(number instanceof Long); 276 277 // parseBigDecimal == false; 278 // parseIntegerOnly == false; 279 form.setParseBigDecimal(false); 280 form.setParseIntegerOnly(false); 281 number = form.parse("-0", new ParsePosition(0)); 282 assertTrue(number instanceof Double); 283 284 number = form.parse("-0.0", new ParsePosition(0)); 285 assertTrue(number instanceof Double); 286 287 // parseBigDecimal == true; 288 // parseIntegerOnly == true; 289 // parseBigDecimal take precedence of parseBigInteger 290 form.setParseBigDecimal(true); 291 form.setParseIntegerOnly(true); 292 number = form.parse("-0", new ParsePosition(0)); 293 assertTrue(number instanceof BigDecimal); 294 295 number = form.parse("-0.0", new ParsePosition(0)); 296 assertTrue(number instanceof BigDecimal); 297 298 number = form.parse("12.4", new ParsePosition(0)); 299 assertTrue(number instanceof BigDecimal); 300 301 // When parseBigDecimal is set to false, no matter how massive the 302 // mantissa part of a number is, the number will be parsed into Double 303 304 form = (DecimalFormat) NumberFormat.getInstance(Locale.US); 305 306 number = form.parse("9,223,372,036,854,775,808.00", 307 new ParsePosition(0)); 308 309 assertTrue(number instanceof Double); 310 assertEquals("9.223372036854776E18", number.toString()); 311 312 number = form.parse("-92,233,720,368,547,758,080.00", 313 new ParsePosition(0)); 314 assertTrue(number instanceof Double); 315 assertEquals("-9.223372036854776E19", number.toString()); 316 317 // When parseBigDecimal is set to true, if mantissa part of number 318 // exceeds Long.MAX_VALUE, the number will be parsed into BigDecimal 319 320 form = (DecimalFormat) NumberFormat.getInstance(Locale.US); 321 322 form.setParseBigDecimal(true); 323 number = form.parse("9,223,372,036,854,775,808.00", 324 new ParsePosition(0)); 325 326 assertTrue(number instanceof BigDecimal); 327 328 assertEquals(9.223372036854776E18, number.doubleValue(), 0); 329 330 number = form.parse("-92,233,720,368,547,758,080.00", new ParsePosition(0)); 331 332 assertTrue(number instanceof BigDecimal); 333 assertEquals(-9.223372036854776E19, number.doubleValue(), 0); 334 335 // The minimum value of Long will be parsed to Long when parseBigDecimal 336 // is not set 337 338 ParsePosition pos = new ParsePosition(0); 339 DecimalFormat df = new DecimalFormat(); 340 pos = new ParsePosition(0); 341 Number nb = df.parse("" + Long.MIN_VALUE, pos); 342 assertTrue(nb instanceof Long); 343 344 // The maximum value of Long will be parsed to Long when parseBigDecimal 345 // is set 346 pos = new ParsePosition(0); 347 df = new DecimalFormat(); 348 pos = new ParsePosition(0); 349 nb = df.parse("" + Long.MAX_VALUE, pos); 350 assertTrue(nb instanceof Long); 351 352 // When parsing invalid string( which is neither consist of digits nor 353 // NaN/Infinity), a null will be returned. 354 355 pos = new ParsePosition(0); 356 df = new DecimalFormat(); 357 try { 358 nb = df.parse("invalid", pos); 359 assertNull(nb); 360 } catch (NullPointerException e) { 361 fail("Should not throw NPE"); 362 } 363 } 364 test_parse_largeBigDecimal()365 public void test_parse_largeBigDecimal() { 366 DecimalFormat form = (DecimalFormat) DecimalFormat.getInstance(Locale.US); 367 form.setParseIntegerOnly(true); 368 form.setParseBigDecimal(true); 369 370 final String doubleMax2 = "359,538,626,972,463,141,629,054,847,463,408," 371 + "713,596,141,135,051,689,993,197,834,953,606,314,521,560,057,077," 372 + "521,179,117,265,533,756,343,080,917,907,028,764,928,468,642,653," 373 + "778,928,365,536,935,093,407,075,033,972,099,821,153,102,564,152," 374 + "490,980,180,778,657,888,151,737,016,910,267,884,609,166,473,806," 375 + "445,896,331,617,118,664,246,696,549,595,652,408,289,446,337,476," 376 + "354,361,838,599,762,500,808,052,368,249,716,736"; 377 Number number = form.parse(doubleMax2, new ParsePosition(0)); 378 assertTrue(number instanceof BigDecimal); 379 BigDecimal result = (BigDecimal) number; 380 assertEquals(new BigDecimal(Double.MAX_VALUE).add(new BigDecimal(Double.MAX_VALUE)), 381 result); 382 } 383 testMaximumFractionDigits_getAndSet()384 public void testMaximumFractionDigits_getAndSet() { 385 DecimalFormat form = (DecimalFormat) NumberFormat.getInstance(Locale.US); 386 // getMaximumFractionDigits of DecimalFormat defaults to 3 387 assertEquals(3, form.getMaximumFractionDigits()); 388 389 form.setMaximumFractionDigits(310); 390 assertEquals(310, form.getMaximumFractionDigits()); 391 392 // Deliberately > 340. The API docs mention 340 and suggest that you can set the value 393 // higher but it will use 340 as a ceiling. 394 form.setMaximumFractionDigits(500); 395 assertEquals(500, form.getMaximumFractionDigits()); 396 397 form.setMaximumFractionDigits(500); 398 assertEquals(500, form.getMaximumFractionDigits()); 399 form.format(12.3); 400 assertEquals(500, form.getMaximumFractionDigits()); 401 402 form.setMaximumFractionDigits(-2); 403 assertEquals(0, form.getMaximumFractionDigits()); 404 } 405 testMinimumFractionDigits_getAndSet()406 public void testMinimumFractionDigits_getAndSet() { 407 DecimalFormat form = (DecimalFormat) NumberFormat.getInstance(Locale.US); 408 409 // getMinimumFractionDigits from NumberFormat (default to 0) 410 // getMinimumFractionDigits from DecimalFormat (default to 0) 411 assertEquals(0, form.getMinimumFractionDigits()); 412 413 form.setMinimumFractionDigits(310); 414 assertEquals(310, form.getMinimumFractionDigits()); 415 416 // Deliberately > 340. The API docs mention 340 and suggest that you can set the value 417 // higher but it will use 340 as a ceiling. 418 form.setMinimumFractionDigits(500); 419 assertEquals(500, form.getMinimumFractionDigits()); 420 421 form.setMaximumFractionDigits(400); 422 assertEquals(400, form.getMinimumFractionDigits()); 423 424 form.setMinimumFractionDigits(-3); 425 assertEquals(0, form.getMinimumFractionDigits()); 426 } 427 testMaximumIntegerDigits_getAndSet()428 public void testMaximumIntegerDigits_getAndSet() { 429 // When use default locale, in this case zh_CN 430 // the returned instance of NumberFormat is a DecimalFormat 431 DecimalFormat form = new DecimalFormat("00.###E0"); 432 assertEquals(2, form.getMaximumIntegerDigits()); 433 434 form = (DecimalFormat) NumberFormat.getInstance(Locale.US); 435 436 form.setMaximumIntegerDigits(300); 437 assertEquals(300, form.getMaximumIntegerDigits()); 438 439 // Deliberately > 309. The API docs mention 309 and suggest that you can set the value 440 // higher but it will use 309 as a ceiling. 441 form.setMaximumIntegerDigits(500); 442 assertEquals(500, form.getMaximumIntegerDigits()); 443 444 form = new DecimalFormat("00.###E0"); 445 assertEquals(2, form.getMaximumIntegerDigits()); 446 447 form.setMaximumIntegerDigits(500); 448 assertEquals(500, form.getMaximumIntegerDigits()); 449 form.format(12.3); 450 assertEquals(500, form.getMaximumIntegerDigits()); 451 452 form.setMaximumIntegerDigits(-3); 453 assertEquals(0, form.getMaximumIntegerDigits()); 454 455 // regression test for HARMONY-878 456 assertTrue(new DecimalFormat("0\t'0'").getMaximumIntegerDigits() > 0); 457 } 458 testMinimumIntegerDigits_getAndSet()459 public void testMinimumIntegerDigits_getAndSet() { 460 final int minIntDigit = 1; 461 DecimalFormat form = (DecimalFormat) NumberFormat.getInstance(Locale.US); 462 463 // getMaximumIntegerDigits from DecimalFormat (default to 1) 464 assertEquals(minIntDigit, form.getMinimumIntegerDigits()); 465 466 form.setMinimumIntegerDigits(300); 467 assertEquals(300, form.getMinimumIntegerDigits()); 468 469 // Deliberately > 309. The API docs mention 309 and suggest that you can set the value 470 // higher but it will use 309 as a ceiling. 471 form.setMinimumIntegerDigits(500); 472 assertEquals(500, form.getMinimumIntegerDigits()); 473 474 form.setMaximumIntegerDigits(400); 475 assertEquals(400, form.getMinimumIntegerDigits()); 476 477 form.setMinimumIntegerDigits(-3); 478 assertEquals(0, form.getMinimumIntegerDigits()); 479 } 480 481 // When MaxFractionDigits is set first and less than MinFractionDigits, max 482 // will be changed to min value testMinimumFactionDigits_minChangesMax()483 public void testMinimumFactionDigits_minChangesMax() { 484 DecimalFormat form = (DecimalFormat) NumberFormat.getInstance(Locale.US); 485 486 form.setMaximumFractionDigits(100); 487 form.setMinimumFractionDigits(200); 488 489 assertEquals(200, form.getMaximumFractionDigits()); 490 assertEquals(200, form.getMinimumFractionDigits()); 491 492 form.setMaximumIntegerDigits(100); 493 form.setMinimumIntegerDigits(200); 494 495 assertEquals(200, form.getMaximumIntegerDigits()); 496 assertEquals(200, form.getMinimumIntegerDigits()); 497 } 498 499 // When MinFractionDigits is set first and less than MaxFractionDigits, min 500 // will be changed to max value testMaximumFactionDigits_maxChangesMin()501 public void testMaximumFactionDigits_maxChangesMin() { 502 DecimalFormat form = (DecimalFormat) NumberFormat.getInstance(Locale.US); 503 504 form.setMinimumFractionDigits(200); 505 form.setMaximumFractionDigits(100); 506 507 assertEquals(100, form.getMaximumFractionDigits()); 508 assertEquals(100, form.getMinimumFractionDigits()); 509 510 form.setMinimumIntegerDigits(200); 511 form.setMaximumIntegerDigits(100); 512 513 assertEquals(100, form.getMaximumIntegerDigits()); 514 assertEquals(100, form.getMinimumIntegerDigits()); 515 } 516 test_formatObject_errorCases()517 public void test_formatObject_errorCases() { 518 DecimalFormat form = (DecimalFormat) NumberFormat.getInstance(Locale.US); 519 520 // If Object(including null) is not of type Number, 521 // IllegalArgumentException will be thrown out 522 try { 523 form.format(new Object(), new StringBuffer(), new FieldPosition(0)); 524 fail("Should throw IAE"); 525 } catch (IllegalArgumentException e) { 526 // expected 527 } 528 try { 529 form.format(null, new StringBuffer(), new FieldPosition(0)); 530 fail("Should throw IAE"); 531 } catch (IllegalArgumentException e) { 532 // expected 533 } 534 535 // When StringBuffer == null || FieldPosition == null 536 // NullPointerException will be thrown out. 537 try { 538 form.format(new Double(1.9), null, new FieldPosition(0)); 539 fail("Should throw NPE"); 540 } catch (NullPointerException e) { 541 // expected 542 } 543 544 try { 545 form.format(new Double(1.3), new StringBuffer(), null); 546 fail("Should throw NPE"); 547 } catch (NullPointerException e) { 548 // expected 549 } 550 551 try { 552 form.format(new Object(), new StringBuffer(), new FieldPosition(0)); 553 fail(); 554 } catch (IllegalArgumentException expected) { 555 } 556 } 557 test_formatObject()558 public void test_formatObject() { 559 DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(Locale.US); 560 561 // format maxLong 562 FieldPosition pos = new FieldPosition(0); 563 StringBuffer out = format.format(new Long(Long.MAX_VALUE), new StringBuffer(), pos); 564 assertTrue("Wrong result L1: " + out, out.toString().equals("9,223,372,036,854,775,807")); 565 566 // format minLong 567 pos = new FieldPosition(0); 568 out = format.format(new Long(Long.MIN_VALUE), new StringBuffer(), pos); 569 assertTrue("Wrong result L2: " + out, out.toString().equals("-9,223,372,036,854,775,808")); 570 571 // format maxLong of type BigInteger 572 pos = new FieldPosition(0); 573 out = format.format(new java.math.BigInteger(String.valueOf(Long.MAX_VALUE)), 574 new StringBuffer(), pos); 575 assertTrue("Wrong result BI1: " + out, out.toString().equals("9,223,372,036,854,775,807")); 576 577 // format minLong of type BigInteger 578 pos = new FieldPosition(0); 579 out = format.format(new java.math.BigInteger(String.valueOf(Long.MIN_VALUE)), 580 new StringBuffer(), pos); 581 assertTrue("Wrong result BI2: " + out, out.toString().equals("-9,223,372,036,854,775,808")); 582 583 // format maxLong + 1 584 java.math.BigInteger big; 585 pos = new FieldPosition(0); 586 big = new java.math.BigInteger(String.valueOf(Long.MAX_VALUE)) 587 .add(new java.math.BigInteger("1")); 588 out = format.format(big, new StringBuffer(), pos); 589 assertTrue("Wrong result BI3: " + out, out.toString().equals("9,223,372,036,854,775,808")); 590 591 // format minLong - 1 592 pos = new FieldPosition(0); 593 big = new java.math.BigInteger(String.valueOf(Long.MIN_VALUE)) 594 .add(new java.math.BigInteger("-1")); 595 out = format.format(big, new StringBuffer(), pos); 596 assertTrue("Wrong result BI4: " + out, out.toString().equals("-9,223,372,036,854,775,809")); 597 598 // format big decimal 599 pos = new FieldPosition(0); 600 out = format.format(new java.math.BigDecimal("51.348"), new StringBuffer(), pos); 601 assertTrue("Wrong result BD1: " + out, out.toString().equals("51.348")); 602 603 // format big decimal 604 pos = new FieldPosition(0); 605 out = format.format(new java.math.BigDecimal("51"), new StringBuffer(), pos); 606 assertTrue("Wrong result BD2: " + out, out.toString().equals("51")); 607 608 // format big decimal Double.MAX_VALUE * 2 609 java.math.BigDecimal bigDecimal; 610 pos = new FieldPosition(0); 611 final String doubleMax2 = "359,538,626,972,463,141,629,054,847,463,408," 612 + "713,596,141,135,051,689,993,197,834,953,606,314,521,560,057,077," 613 + "521,179,117,265,533,756,343,080,917,907,028,764,928,468,642,653," 614 + "778,928,365,536,935,093,407,075,033,972,099,821,153,102,564,152," 615 + "490,980,180,778,657,888,151,737,016,910,267,884,609,166,473,806," 616 + "445,896,331,617,118,664,246,696,549,595,652,408,289,446,337,476," 617 + "354,361,838,599,762,500,808,052,368,249,716,736"; 618 bigDecimal = new BigDecimal(Double.MAX_VALUE).add(new BigDecimal(Double.MAX_VALUE)); 619 out = format.format(bigDecimal, new StringBuffer(), pos); 620 assertTrue("Wrong result BDmax2: " + out, out.toString().equals(doubleMax2)); 621 622 // format big decimal Double.MIN_VALUE + Double.MIN_VALUE 623 // and Double.MIN_VALUE - Double.MIN_VALUE 624 pos = new FieldPosition(0); 625 626 bigDecimal = new BigDecimal(Double.MIN_VALUE).add(new BigDecimal(Double.MIN_VALUE)); 627 out = format.format(bigDecimal, new StringBuffer(), pos); 628 629 bigDecimal = new BigDecimal(Float.MAX_VALUE).add(new BigDecimal(Float.MAX_VALUE)); 630 out = format.format(bigDecimal, new StringBuffer(), pos); 631 final String BDFloatMax2 = "680,564,693,277,057,719,623,408,366,969,033,850,880"; 632 assertTrue("Wrong result BDFloatMax2: " + out, out.toString().equals(BDFloatMax2)); 633 // format big decimal Float.MIN_VALUE + Float.MIN_VALUE 634 // and Float.MIN_VALUE - Float.MIN_VALUE 635 bigDecimal = new BigDecimal(Float.MIN_VALUE).add(new BigDecimal(Float.MIN_VALUE)); 636 out = format.format(bigDecimal, new StringBuffer(), pos); 637 final String BDFloatMin2 = "0"; 638 639 bigDecimal = new BigDecimal(Float.MIN_VALUE).subtract(new BigDecimal(Float.MIN_VALUE)); 640 out = format.format(bigDecimal, new StringBuffer(), pos); 641 642 assertTrue("Wrong result BDFloatMax2: " + out, out.toString().equals(BDFloatMin2)); 643 } 644 test_equals()645 public void test_equals() { 646 DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(Locale.US); 647 DecimalFormat cloned = (DecimalFormat) format.clone(); 648 cloned.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US)); 649 assertEquals(format, cloned); 650 651 Currency c = Currency.getInstance(Locale.US); 652 cloned.setCurrency(c); 653 654 assertEquals(format, cloned); 655 } 656 test_getNegativePrefix()657 public void test_getNegativePrefix() { 658 DecimalFormat df = new DecimalFormat(); 659 df.setNegativePrefix("--"); 660 assertTrue("Incorrect negative prefix", df.getNegativePrefix().equals("--")); 661 } 662 test_getNegativeSuffix()663 public void test_getNegativeSuffix() { 664 DecimalFormat df = new DecimalFormat(); 665 df.setNegativeSuffix("&"); 666 assertTrue("Incorrect negative suffix", df.getNegativeSuffix().equals("&")); 667 } 668 test_getPositivePrefix()669 public void test_getPositivePrefix() { 670 DecimalFormat df = new DecimalFormat(); 671 df.setPositivePrefix("++"); 672 assertTrue("Incorrect positive prefix", df.getPositivePrefix().equals("++")); 673 } 674 test_getPositiveSuffix()675 public void test_getPositiveSuffix() { 676 DecimalFormat df = new DecimalFormat(); 677 df.setPositiveSuffix("%"); 678 assertTrue("Incorrect positive prefix", df.getPositiveSuffix().equals("%")); 679 } 680 test_setPositivePrefix()681 public void test_setPositivePrefix() throws Exception { 682 DecimalFormat format = new DecimalFormat(); 683 assertEquals("", format.getPositivePrefix()); 684 685 format.setPositivePrefix("PosPrf"); 686 assertEquals("PosPrf", format.getPositivePrefix()); 687 assertTrue(format.parse("PosPrf123.45").doubleValue() == 123.45); 688 689 format.setPositivePrefix(""); 690 assertEquals("", format.getPositivePrefix()); 691 692 format.setPositivePrefix(null); 693 assertNull(format.getPositivePrefix()); 694 } 695 test_setPositiveSuffix()696 public void test_setPositiveSuffix() throws Exception { 697 DecimalFormat format = new DecimalFormat(); 698 assertEquals("", format.getPositiveSuffix()); 699 700 format.setPositiveSuffix("PosSfx"); 701 assertEquals("PosSfx", format.getPositiveSuffix()); 702 assertTrue(format.parse("123.45PosSfx").doubleValue() == 123.45); 703 704 format.setPositiveSuffix(""); 705 assertEquals("", format.getPositiveSuffix()); 706 707 format.setPositiveSuffix(null); 708 assertNull(format.getPositiveSuffix()); 709 } 710 test_setNegativePrefix()711 public void test_setNegativePrefix() throws Exception { 712 DecimalFormat format = new DecimalFormat(); 713 assertEquals("-", format.getNegativePrefix()); 714 715 format.setNegativePrefix("NegPrf"); 716 assertEquals("NegPrf", format.getNegativePrefix()); 717 assertTrue(format.parse("NegPrf123.45").doubleValue() == -123.45); 718 format.setNegativePrefix(""); 719 assertEquals("", format.getNegativePrefix()); 720 721 format.setNegativePrefix(null); 722 assertNull(format.getNegativePrefix()); 723 } 724 test_setNegativeSuffix()725 public void test_setNegativeSuffix() throws Exception { 726 DecimalFormat format = new DecimalFormat(); 727 assertEquals("", format.getNegativeSuffix()); 728 729 format.setNegativeSuffix("NegSfx"); 730 assertEquals("NegSfx", format.getNegativeSuffix()); 731 assertTrue(format.parse("123.45NegPfx").doubleValue() == 123.45); 732 733 format.setNegativeSuffix(""); 734 assertEquals("", format.getNegativeSuffix()); 735 736 format.setNegativeSuffix(null); 737 assertNull(format.getNegativeSuffix()); 738 } 739 test_setGroupingUsed()740 public void test_setGroupingUsed() { 741 DecimalFormat format = new DecimalFormat(); 742 743 StringBuffer buf = new StringBuffer(); 744 format.setGroupingUsed(false); 745 format.format(new Long(1970), buf, new FieldPosition(0)); 746 assertEquals("1970", buf.toString()); 747 assertFalse(format.isGroupingUsed()); 748 format.format(new Long(1970), buf, new FieldPosition(0)); 749 assertEquals("19701970", buf.toString()); 750 assertFalse(format.isGroupingUsed()); 751 752 format.setGroupingUsed(true); 753 format.format(new Long(1970), buf, new FieldPosition(0)); 754 assertEquals("197019701,970", buf.toString()); 755 assertTrue(format.isGroupingUsed()); 756 } 757 test_isGroupingUsed()758 public void test_isGroupingUsed() { 759 assertFalse(new DecimalFormat("####.##").isGroupingUsed()); 760 assertFalse(new DecimalFormat("######.######").isGroupingUsed()); 761 assertFalse(new DecimalFormat("000000.000000").isGroupingUsed()); 762 assertFalse(new DecimalFormat("######.000000").isGroupingUsed()); 763 assertFalse(new DecimalFormat("000000.######").isGroupingUsed()); 764 assertFalse(new DecimalFormat(" ###.###").isGroupingUsed()); 765 assertFalse(new DecimalFormat("$#####.######").isGroupingUsed()); 766 assertFalse(new DecimalFormat("$$####.######").isGroupingUsed()); 767 768 assertTrue(new DecimalFormat("###,####").isGroupingUsed()); 769 } 770 testConstructor_noArg()771 public void testConstructor_noArg() { 772 // Test for method java.text.DecimalFormat() 773 // the constructor form that specifies a pattern is equal to the form 774 // constructed with no pattern and applying that pattern using the 775 // applyPattern call 776 DecimalFormat format1 = new DecimalFormat(); 777 format1.applyPattern("'$'1000.0000"); 778 DecimalFormat format2 = new DecimalFormat(); 779 format2.applyPattern("'$'1000.0000"); 780 assertTrue("Constructed format did not match applied format object", 781 format2.equals(format1)); 782 DecimalFormat format3 = new DecimalFormat("'$'1000.0000"); 783 assertTrue("Constructed format did not match applied format object", 784 format3.equals(format1)); 785 DecimalFormat format4 = new DecimalFormat("'$'8000.0000"); 786 assertTrue("Constructed format did not match applied format object", 787 !format4.equals(format1)); 788 } 789 testConstructor_string()790 public void testConstructor_string() { 791 // Test for method java.text.DecimalFormat(java.lang.String) 792 // the constructor form that specifies a pattern is equal to the form 793 // constructed with no pattern and applying that pattern using the 794 // applyPattern call 795 DecimalFormat format = new DecimalFormat("'$'0000.0000"); 796 DecimalFormat format1 = new DecimalFormat(); 797 format1.applyPattern("'$'0000.0000"); 798 assertTrue("Constructed format did not match applied format object", 799 format.equals(format1)); 800 801 new DecimalFormat("####.##"); 802 new DecimalFormat("######.######"); 803 new DecimalFormat("000000.000000"); 804 new DecimalFormat("######.000000"); 805 new DecimalFormat("000000.######"); 806 new DecimalFormat(" ###.###"); 807 new DecimalFormat("$#####.######"); 808 new DecimalFormat("$$####.######"); 809 new DecimalFormat("%#,##,###,####"); 810 new DecimalFormat("#,##0.00;(#,##0.00)"); 811 812 try { 813 new DecimalFormat(null); 814 fail(); 815 } catch (NullPointerException expected) { 816 } 817 818 try { 819 new DecimalFormat("%#,##,###,####'"); 820 fail(); 821 } catch (IllegalArgumentException expected) { 822 } 823 824 try { 825 new DecimalFormat("#.##0.00"); 826 fail(); 827 } catch (IllegalArgumentException expected) { 828 } 829 } 830 testConstructor_stringAndSymbols()831 public void testConstructor_stringAndSymbols() { 832 // case 1: Try to construct object using correct pattern and format 833 // symbols. 834 DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.CANADA); 835 DecimalFormat format1 = new DecimalFormat("'$'1000.0000", dfs); 836 DecimalFormat format2 = new DecimalFormat(); 837 format2.applyPattern("'$'1000.0000"); 838 format2.setDecimalFormatSymbols(dfs); 839 assertTrue("Constructed format did not match applied format object", 840 format2.equals(format1)); 841 assertTrue("Constructed format did not match applied format object", 842 !format1.equals( 843 new DecimalFormat("'$'1000.0000", new DecimalFormatSymbols(Locale.CHINA)))); 844 845 // case 2: Try to construct object using null arguments. 846 try { 847 new DecimalFormat("'$'1000.0000", (DecimalFormatSymbols) null); 848 fail(); 849 } catch (NullPointerException expected) { 850 } 851 try { 852 new DecimalFormat(null, new DecimalFormatSymbols()); 853 fail(); 854 } catch (NullPointerException expected) { 855 } 856 try { 857 new DecimalFormat(null, (DecimalFormatSymbols) null); 858 fail(); 859 } catch (NullPointerException expected) { 860 } 861 862 // case 3: Try to construct object using incorrect pattern. 863 try { 864 new DecimalFormat("$'", new DecimalFormatSymbols()); 865 fail(); 866 } catch (IllegalArgumentException expected) { 867 } 868 } 869 test_applyPattern()870 public void test_applyPattern() { 871 DecimalFormat format = new DecimalFormat("#.#"); 872 assertEquals("Wrong pattern 1", "#0.#", format.toPattern()); 873 format = new DecimalFormat("#."); 874 assertEquals("Wrong pattern 2", "#0.", format.toPattern()); 875 format = new DecimalFormat("#"); 876 assertEquals("Wrong pattern 3", "#", format.toPattern()); 877 format = new DecimalFormat(".#"); 878 assertEquals("Wrong pattern 4", "#.0", format.toPattern()); 879 880 // Regression for HARMONY-6485 881 format = new DecimalFormat(); 882 format.setMinimumIntegerDigits(0); 883 format.setMinimumFractionDigits(0); 884 format.setMaximumFractionDigits(0); 885 format.applyPattern("00.0#"); 886 assertEquals("Minimum integer digits not set", 2, format.getMinimumIntegerDigits()); 887 assertEquals("Minimum fraction digits not set", 1, format.getMinimumFractionDigits()); 888 assertEquals("Maximum fraction digits not set", 2, format.getMaximumFractionDigits()); 889 890 try { 891 format.applyPattern(null); 892 fail(); 893 } catch (NullPointerException expected) { 894 } 895 896 try { 897 format.applyPattern("%#,##,###,####'"); 898 fail(); 899 } catch (IllegalArgumentException expected) { 900 } 901 902 try { 903 format.applyPattern("#.##0.00"); 904 fail(); 905 } catch (IllegalArgumentException expected) { 906 } 907 } 908 909 // AndroidOnly: icu supports 2 grouping sizes test_applyPattern_icu2GroupingSizes()910 public void test_applyPattern_icu2GroupingSizes() { 911 DecimalFormat decFormat = new DecimalFormat("#.#"); 912 String[] patterns = { 913 "####.##", "######.######", "000000.000000", 914 "######.000000", "000000.######", " ###.###", "$#####.######", 915 "$$####.######", "%#,##,###,####", "#,##0.00;(#,##0.00)", 916 "##.##-E" 917 }; 918 919 String[] expResult = { 920 "#0.##", "#0.######", "#000000.000000", 921 "#.000000", "#000000.######", " #0.###", "$#0.######", 922 "$$#0.######", 923 "%#,###,####", // icu only. icu supports two grouping sizes 924 "#,##0.00;(#,##0.00)", 925 "#0.##-'E'" 926 // icu only. E in the suffix does not need to be quoted. This is done automatically. 927 }; 928 929 for (int i = 0; i < patterns.length; i++) { 930 decFormat.applyPattern(patterns[i]); 931 String result = decFormat.toPattern(); 932 assertEquals("Failed to apply following pattern: " + patterns[i] + 933 "\n expected: " + expResult[i] + 934 "\n returned: " + result, expResult[i], result); 935 } 936 } 937 test_applyLocalizedPattern()938 public void test_applyLocalizedPattern() throws Exception { 939 DecimalFormat format = new DecimalFormat(); 940 941 // case 1: Try to apply correct variants of pattern. 942 format.applyLocalizedPattern("#.#"); 943 assertEquals("Wrong pattern 1", "#0.#", format.toLocalizedPattern()); 944 format.applyLocalizedPattern("#."); 945 assertEquals("Wrong pattern 2", "#0.", format.toLocalizedPattern()); 946 format.applyLocalizedPattern("#"); 947 assertEquals("Wrong pattern 3", "#", format.toLocalizedPattern()); 948 format.applyLocalizedPattern(".#"); 949 assertEquals("Wrong pattern 4", "#.0", format.toLocalizedPattern()); 950 951 // case 2: Try to apply malformed patten. 952 try { 953 format.applyLocalizedPattern("'#,#:#0.0#;(#)"); 954 fail(); 955 } catch (IllegalArgumentException expected) { 956 } 957 958 // case 3: Try to apply null pattern. 959 try { 960 format.applyLocalizedPattern((String) null); 961 fail(); 962 } catch (NullPointerException expected) { 963 } 964 } 965 test_toPattern()966 public void test_toPattern() { 967 DecimalFormat format = new DecimalFormat(); 968 format.applyPattern("#.#"); 969 assertEquals("Wrong pattern 1", "#0.#", format.toPattern()); 970 format.applyPattern("#."); 971 assertEquals("Wrong pattern 2", "#0.", format.toPattern()); 972 format.applyPattern("#"); 973 assertEquals("Wrong pattern 3", "#", format.toPattern()); 974 format.applyPattern(".#"); 975 assertEquals("Wrong pattern 4", "#.0", format.toPattern()); 976 } 977 test_toLocalizedPattern()978 public void test_toLocalizedPattern() { 979 DecimalFormat format = new DecimalFormat(); 980 format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US)); 981 format.applyLocalizedPattern("#.#"); 982 assertEquals("Wrong pattern 1", "#0.#", format.toLocalizedPattern()); 983 format.applyLocalizedPattern("#."); 984 assertEquals("Wrong pattern 2", "#0.", format.toLocalizedPattern()); 985 format.applyLocalizedPattern("#"); 986 assertEquals("Wrong pattern 3", "#", format.toLocalizedPattern()); 987 format.applyLocalizedPattern(".#"); 988 assertEquals("Wrong pattern 4", "#.0", format.toLocalizedPattern()); 989 } 990 test_hashCode()991 public void test_hashCode() { 992 DecimalFormat df1 = new DecimalFormat(); 993 DecimalFormat df2 = (DecimalFormat) df1.clone(); 994 assertTrue("Hash codes of equals object are not equal", df2.hashCode() == df1.hashCode()); 995 } 996 test_clone()997 public void test_clone() { 998 DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(Locale.US); 999 DecimalFormat cloned = (DecimalFormat) format.clone(); 1000 assertEquals(cloned.getDecimalFormatSymbols(), format.getDecimalFormatSymbols()); 1001 1002 format = new DecimalFormat("'$'0000.0000"); 1003 DecimalFormat format1 = (DecimalFormat) (format.clone()); 1004 // make sure the objects are equal 1005 assertTrue("Object's clone isn't equal!", format.equals(format1)); 1006 // change the content of the clone and make sure it's not equal anymore 1007 // verifies that it's data is now distinct from the original 1008 format1.applyPattern("'$'0000.####"); 1009 assertTrue("Object's changed clone should not be equal!", !format.equals(format1)); 1010 } 1011 test_formatDouble_maximumFractionDigits()1012 public void test_formatDouble_maximumFractionDigits() { 1013 DecimalFormat df = new DecimalFormat("###0.##", new DecimalFormatSymbols(Locale.US)); 1014 df.setMaximumFractionDigits(3); 1015 assertEquals(3, df.getMaximumFractionDigits()); 1016 assertEquals("1.235", df.format(1.23456)); 1017 df.setMinimumFractionDigits(4); 1018 assertEquals(4, df.getMaximumFractionDigits()); 1019 assertEquals("456.0000", df.format(456)); 1020 1021 df = new DecimalFormat("##0.#"); 1022 df.setMaximumFractionDigits(30); 1023 assertEquals("0", df.format(0.0)); 1024 assertEquals("-0", df.format(-0.0)); 1025 assertEquals("1", df.format(1.0)); 1026 assertEquals("-1", df.format(-1.0)); 1027 } 1028 test_formatDouble_minimumFractionDigits()1029 public void test_formatDouble_minimumFractionDigits() { 1030 DecimalFormat df = new DecimalFormat("###0.##", new DecimalFormatSymbols(Locale.US)); 1031 df.setMinimumFractionDigits(4); 1032 assertEquals(4, df.getMinimumFractionDigits()); 1033 assertEquals("1.2300", df.format(1.23)); 1034 df.setMaximumFractionDigits(2); 1035 assertEquals(2, df.getMinimumFractionDigits()); 1036 assertEquals("456.00", df.format(456)); 1037 1038 df = new DecimalFormat("##0.#", new DecimalFormatSymbols(Locale.US)); 1039 df.setMinimumFractionDigits(30); 1040 assertEquals("0.000000000000000000000000000000", df.format(0.0)); 1041 assertEquals("-0.000000000000000000000000000000", df.format(-0.0)); 1042 assertEquals("1.000000000000000000000000000000", df.format(1.0)); 1043 assertEquals("-1.000000000000000000000000000000", df.format(-1.0)); 1044 } 1045 test_formatDouble_withFieldPosition()1046 public void test_formatDouble_withFieldPosition() { 1047 new Support_DecimalFormat( 1048 "test_formatDLjava_lang_StringBufferLjava_text_FieldPosition") 1049 .t_format_with_FieldPosition(); 1050 } 1051 1052 // This test serves as a regression test for Android's behavior. 1053 // There are many patterns that produce different output from the RI but are sometimes the 1054 // consequence of Android following the ICU DecimalFormat rules. test_formatDouble_scientificNotation()1055 public void test_formatDouble_scientificNotation() { 1056 FormatTester formatTester = new FormatTester(); 1057 final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US); 1058 1059 DecimalFormat df = new DecimalFormat("00.0#E0", dfs); 1060 // ["00.0#E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, 1061 // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=2, 1062 // maxFractionDigits=2,minIntegerDigits=2,minFractionDigits=1,grouping=false] 1063 // Because maximum integer digit was not explicitly set: The exponent can be any integer. 1064 // Scientific notation => use significant digit logic 1065 // '@' not present: Significant digits: Min: 1, 1066 // Max: "min integer digits" (2) + "max fractional digits (2) == 4 1067 formatTester.format(df, "00.0E0", 0.0); 1068 formatTester.format(df, "10.0E-1", 1.0); 1069 formatTester.format(df, "12.0E0", 12.0); 1070 formatTester.format(df, "12.3E1", 123.0); 1071 formatTester.format(df, "12.34E2", 1234.0); 1072 formatTester.format(df, "12.35E3", 12346.0); 1073 formatTester.format(df, "10.0E4", 99999.0); 1074 formatTester.format(df, "12.0E-1", 1.2); 1075 formatTester.format(df, "12.3E0", 12.3); 1076 formatTester.format(df, "12.34E1", 123.4); 1077 formatTester.format(df, "12.35E2", 1234.6); 1078 formatTester.format(df, "10.0E3", 9999.9); 1079 formatTester.format(df, "10.0E-2", 0.1); 1080 formatTester.format(df, "12.0E-2", 0.12); 1081 formatTester.format(df, "12.3E-2", 0.123); 1082 formatTester.format(df, "12.34E-2", 0.1234); 1083 formatTester.format(df, "12.35E-2", 0.12346); 1084 formatTester.format(df, "10.0E-1", 0.99999); 1085 formatTester.format(df, "-10.0E-1", -1.0); 1086 formatTester.format(df, "-12.0E0", -12.0); 1087 formatTester.format(df, "-12.3E1", -123.0); 1088 formatTester.format(df, "-12.34E2", -1234.0); 1089 formatTester.format(df, "-12.35E3", -12346.0); 1090 formatTester.format(df, "-10.0E4", -99999.0); 1091 1092 df = new DecimalFormat("#00.0##E0", dfs); 1093 // ["#00.0##E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, 1094 // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=3, 1095 // maxFractionDigits=3,minIntegerDigits=2,minFractionDigits=1,grouping=false] 1096 // Because maximum integer digit count is set: The exponent must be a multiple of it (3). 1097 // Scientific notation => use significant digit logic 1098 // '@' not present: Significant digits: Min: 1, 1099 // Max: "min integer digits" (2) + "max fractional digits (3) == 5 1100 formatTester.format(df, "100E-3", 0.1); 1101 formatTester.format(df, "120E-3", 0.12); 1102 formatTester.format(df, "123E-3", 0.123); 1103 formatTester.format(df, "123.4E-3", 0.1234); 1104 formatTester.format(df, "123.46E-3", 0.1234567); 1105 formatTester.format(df, "10E-3", 0.01); 1106 formatTester.format(df, "12E-3", 0.012); 1107 formatTester.format(df, "12.3E-3", 0.0123); 1108 formatTester.format(df, "12.34E-3", 0.01234); 1109 formatTester.format(df, "12.346E-3", 0.01234567); 1110 formatTester.format(df, "1.0E-3", 0.001); 1111 formatTester.format(df, "1.2E-3", 0.0012); 1112 formatTester.format(df, "1.23E-3", 0.00123); 1113 formatTester.format(df, "1.234E-3", 0.001234); 1114 formatTester.format(df, "1.2346E-3", 0.001234567); 1115 formatTester.format(df, "100E-6", 0.0001); 1116 formatTester.format(df, "120E-6", 0.00012); 1117 formatTester.format(df, "123E-6", 0.000123); 1118 formatTester.format(df, "123.4E-6", 0.0001234); 1119 formatTester.format(df, "123.46E-6", 0.0001234567); 1120 formatTester.format(df, "0.0E0", 0.0); 1121 formatTester.format(df, "1.0E0", 1.0); 1122 formatTester.format(df, "12E0", 12.0); 1123 formatTester.format(df, "123E0", 123.0); 1124 formatTester.format(df, "1.234E3", 1234.0); 1125 formatTester.format(df, "12.345E3", 12345.0); 1126 formatTester.format(df, "123.46E3", 123456.0); 1127 formatTester.format(df, "1.2346E6", 1234567.0); 1128 formatTester.format(df, "12.346E6", 12345678.0); 1129 formatTester.format(df, "100E6", 99999999.0); 1130 1131 df = new DecimalFormat("#.0E0", dfs); 1132 // ["#.0E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, 1133 // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=1, 1134 // maxFractionDigits=1,minIntegerDigits=0,minFractionDigits=1,grouping=false] 1135 // Because maximum integer digit count is set: The exponent must be a multiple of it (1). 1136 // Scientific notation => use significant digit logic 1137 // '@' not present: Significant digits: Min: 1, 1138 // Max: "min integer digits" (0) + "max fractional digits (1) == 1 1139 formatTester.format(df, "0.0E0", 0.0); 1140 formatTester.format(df, "1.0E0", 1.0); 1141 formatTester.format(df, "1.0E1", 12.0); 1142 formatTester.format(df, "1.0E2", 123.0); 1143 formatTester.format(df, "1.0E3", 1234.0); 1144 formatTester.format(df, "1.0E4", 9999.0); 1145 1146 df = new DecimalFormat("0.E0", dfs); 1147 // ["0.E0",isDecimalSeparatorAlwaysShown=true,groupingSize=0,multiplier=1,negativePrefix=-, 1148 // negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=1,maxFractionDigits=0, 1149 // minIntegerDigits=1,minFractionDigits=0,grouping=false] 1150 // Because maximum integer digit was not explicitly set: The exponent can be any integer. 1151 // Scientific notation => use significant digit logic 1152 // '@' not present: Significant digits: Min: 1, 1153 // Max: "min integer digits" (1) + "max fractional digits (0) == 1 1154 formatTester.format(df, "0E0", 0.0); 1155 formatTester.format(df, "1E0", 1.0); 1156 formatTester.format(df, "1E1", 12.0); 1157 formatTester.format(df, "1E2", 123.0); 1158 formatTester.format(df, "1E3", 1234.0); 1159 formatTester.format(df, "1E4", 9999.0); 1160 1161 df = new DecimalFormat("##0.00#E0", dfs); 1162 // ["##0.00#E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, 1163 // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=3, 1164 // maxFractionDigits=3,minIntegerDigits=1,minFractionDigits=2,grouping=false] 1165 // Because maximum integer digit count is set: The exponent must be a multiple of it (3). 1166 // Scientific notation => use significant digit logic 1167 // '@' not present: Significant digits: Min: 1, 1168 // Max: "min integer digits" (1) + "max fractional digits (3) == 4 1169 formatTester.format(df, "100E-3", 0.1); 1170 formatTester.format(df, "123.5E-3", 0.1234567); 1171 formatTester.format(df, "1.00E0", 0.9999999); 1172 formatTester.format(df, "10.0E-3", 0.01); 1173 formatTester.format(df, "12.35E-3", 0.01234567); 1174 formatTester.format(df, "100E-3", 0.09999999); 1175 formatTester.format(df, "1.00E-3", 0.001); 1176 formatTester.format(df, "1.235E-3", 0.001234567); 1177 formatTester.format(df, "10.0E-3", 0.009999999); 1178 formatTester.format(df, "100E-6", 0.0001); 1179 formatTester.format(df, "123.5E-6", 0.0001234567); 1180 formatTester.format(df, "1.00E-3", 0.0009999999); 1181 1182 df = new DecimalFormat("###0.00#E0", dfs); 1183 // ["###0.00#E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, 1184 // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=4, 1185 // maxFractionDigits=3,minIntegerDigits=1,minFractionDigits=2,grouping=false] 1186 // Because maximum integer digit count is set: The exponent must be a multiple of it (4). 1187 // Scientific notation => use significant digit logic 1188 // '@' not present: Significant digits: Min: 1, 1189 // Max: "min integer digits" (1) + "max fractional digits (3) == 4 1190 formatTester.format(df, "1000E-4", 0.1); 1191 formatTester.format(df, "1235E-4", 0.12345678); 1192 formatTester.format(df, "1.00E0", 0.99999999); 1193 formatTester.format(df, "100E-4", 0.01); 1194 formatTester.format(df, "123.5E-4", 0.012345678); 1195 formatTester.format(df, "1000E-4", 0.099999999); 1196 formatTester.format(df, "10.0E-4", 0.001); 1197 formatTester.format(df, "12.35E-4", 0.0012345678); 1198 formatTester.format(df, "100E-4", 0.0099999999); 1199 formatTester.format(df, "1.00E-4", 0.0001); 1200 formatTester.format(df, "1.235E-4", 0.00012345678); 1201 formatTester.format(df, "10.0E-4", 0.00099999999); 1202 formatTester.format(df, "1000E-8", 0.00001); 1203 formatTester.format(df, "1235E-8", 0.000012345678); 1204 formatTester.format(df, "1.00E-4", 0.000099999999); 1205 1206 df = new DecimalFormat("###0.0#E0", dfs); 1207 // ["###0.0#E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, 1208 // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=4, 1209 // maxFractionDigits=2,minIntegerDigits=1,minFractionDigits=1,grouping=false] 1210 // Because maximum integer digit count is set: The exponent must be a multiple of it (4). 1211 // Scientific notation => use significant digit logic 1212 // '@' not present: Significant digits: Min: 1, 1213 // Max: "min integer digits" (1) + "max fractional digits (2) == 3 1214 formatTester.format(df, "1000E-4", 0.1); 1215 formatTester.format(df, "1230E-4", 0.1234567); 1216 formatTester.format(df, "1.0E0", 0.9999999); 1217 formatTester.format(df, "100E-4", 0.01); 1218 formatTester.format(df, "123E-4", 0.01234567); 1219 formatTester.format(df, "1000E-4", 0.09999999); 1220 formatTester.format(df, "10E-4", 0.001); 1221 formatTester.format(df, "12.3E-4", 0.001234567); 1222 formatTester.format(df, "100E-4", 0.009999999); 1223 formatTester.format(df, "1.0E-4", 0.0001); 1224 formatTester.format(df, "1.23E-4", 0.0001234567); 1225 formatTester.format(df, "10E-4", 0.0009999999); 1226 formatTester.format(df, "1000E-8", 0.00001); 1227 formatTester.format(df, "1230E-8", 0.00001234567); 1228 formatTester.format(df, "1.0E-4", 0.00009999999); 1229 1230 df = new DecimalFormat("##0.0E0", dfs); 1231 // ["##0.0E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, 1232 // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=3, 1233 // maxFractionDigits=1,minIntegerDigits=1,minFractionDigits=1,grouping=false] 1234 // Because maximum integer digit count is set: The exponent must be a multiple of it (3). 1235 // Scientific notation => use significant digit logic 1236 // '@' not present: Significant digits: Min: 1, 1237 // Max: "min integer digits" (1) + "max fractional digits (1) == 2 1238 formatTester.format(df, "0.0E0", 0.0); 1239 formatTester.format(df, "1.0E0", 1.0); 1240 formatTester.format(df, "12E0", 12.0); 1241 formatTester.format(df, "120E0", 123.0); 1242 formatTester.format(df, "1.2E3", 1234.0); 1243 formatTester.format(df, "12E3", 12346.0); 1244 formatTester.format(df, "100E3", 99999.0); 1245 formatTester.format(df, "1.0E6", 999999.0); 1246 1247 df = new DecimalFormat("0.#E0", dfs); 1248 // ["0.#E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, 1249 // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=1, 1250 // maxFractionDigits=1,minIntegerDigits=1,minFractionDigits=0,grouping=false] 1251 // Because maximum integer digit was not explicitly set: The exponent can be any integer. 1252 // Scientific notation => use significant digit logic 1253 // '@' not present: Significant digits: Min: 1, 1254 // Max: "min integer digits" (1) + "max fractional digits (1) == 2 1255 formatTester.format(df, "0E0", 0.0); 1256 formatTester.format(df, "1E0", 1.0); 1257 formatTester.format(df, "1.2E1", 12.0); 1258 formatTester.format(df, "1.2E2", 123.0); 1259 formatTester.format(df, "1.2E3", 1234.0); 1260 formatTester.format(df, "1E4", 9999.0); 1261 1262 df = new DecimalFormat(".0E0", dfs); 1263 // [".0E0",isDecimalSeparatorAlwaysShown=true,groupingSize=0,multiplier=1,negativePrefix=-, 1264 // negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=0,maxFractionDigits=1, 1265 // minIntegerDigits=0,minFractionDigits=1,grouping=false] 1266 // Because maximum integer digit was not explicitly set: The exponent can be any integer. 1267 // Scientific notation => use significant digit logic 1268 // '@' not present: Significant digits: Min: 1, 1269 // Max: "min integer digits" (0) + "max fractional digits (1) == 2 1270 formatTester.format(df, ".0E0", 0.0); 1271 formatTester.format(df, ".1E1", 1.0); 1272 formatTester.format(df, ".1E2", 12.0); 1273 formatTester.format(df, ".1E3", 123.0); 1274 formatTester.format(df, ".1E4", 1234.0); 1275 formatTester.format(df, ".1E5", 9999.0); 1276 1277 formatTester.throwFailures(); 1278 } 1279 test_formatDouble_scientificNotationMinusZero()1280 public void test_formatDouble_scientificNotationMinusZero() throws Exception { 1281 FormatTester formatTester = new FormatTester(); 1282 final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US); 1283 1284 DecimalFormat df = new DecimalFormat("00.0#E0", dfs); 1285 // ["00.0#E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, 1286 // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=2, 1287 // maxFractionDigits=2,minIntegerDigits=2,minFractionDigits=1,grouping=false] 1288 // Because maximum integer digit was not explicitly set: The exponent can be any integer. 1289 // Scientific notation => use significant digit logic 1290 // '@' not present: Significant digits: Min: 1, 1291 // Max: "min integer digits" (2) + "max fractional digits (2) == 4 1292 formatTester.format(df, "-00.0E0", -0.0); 1293 1294 df = new DecimalFormat("##0.0E0", dfs); 1295 // ["##0.0E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, 1296 // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=3, 1297 // maxFractionDigits=1,minIntegerDigits=1,minFractionDigits=1,grouping=false] 1298 // Because maximum integer digit count is set: The exponent must be a multiple of it (3). 1299 // Scientific notation => use significant digit logic 1300 // '@' not present: Significant digits: Min: 1, 1301 // Max: "min integer digits" (1) + "max fractional digits (1) == 2 1302 formatTester.format(df, "-0.0E0", -0.0); 1303 1304 df = new DecimalFormat("#.0E0", dfs); 1305 // ["#.0E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, 1306 // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=1, 1307 // maxFractionDigits=1,minIntegerDigits=0,minFractionDigits=1,grouping=false] 1308 // Because maximum integer digit count is set: The exponent must be a multiple of it (1). 1309 // Scientific notation => use significant digit logic 1310 // '@' not present: Significant digits: Min: 1, 1311 // Max: "min integer digits" (0) + "max fractional digits (1) == 2 1312 formatTester.format(df, "-0.0E0", -0.0); 1313 1314 df = new DecimalFormat("0.#E0", dfs); 1315 // ["0.#E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, 1316 // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=1, 1317 // maxFractionDigits=1,minIntegerDigits=1,minFractionDigits=0,grouping=false] 1318 // Because maximum integer digit was not explicitly set: The exponent can be any integer. 1319 // Scientific notation => use significant digit logic 1320 // '@' not present: Significant digits: Min: 1, 1321 // Max: "min integer digits" (1) + "max fractional digits (1) == 2 1322 formatTester.format(df, "-0E0", -0.0); 1323 1324 df = new DecimalFormat(".0E0", dfs); 1325 // [".0E0",isDecimalSeparatorAlwaysShown=true,groupingSize=0,multiplier=1,negativePrefix=-, 1326 // negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=0,maxFractionDigits=1, 1327 // minIntegerDigits=0,minFractionDigits=1,grouping=false] 1328 // Because maximum integer digit was not explicitly set: The exponent can be any integer. 1329 // Scientific notation => use significant digit logic 1330 // '@' not present: Significant digits: Min: 1, 1331 // Max: "min integer digits" (0) + "max fractional digits (1) == 1 1332 formatTester.format(df, "-.0E0", -0.0); 1333 1334 formatTester.throwFailures(); 1335 } 1336 test_formatLong_maximumIntegerDigits()1337 public void test_formatLong_maximumIntegerDigits() { 1338 DecimalFormat df = new DecimalFormat("###0.##"); 1339 df.setMaximumIntegerDigits(2); 1340 assertEquals(2, df.getMaximumIntegerDigits()); 1341 assertEquals("34", df.format(1234)); 1342 df.setMinimumIntegerDigits(4); 1343 assertEquals(4, df.getMaximumIntegerDigits()); 1344 assertEquals("0026", df.format(26)); 1345 } 1346 test_formatLong_minimumIntegerDigits()1347 public void test_formatLong_minimumIntegerDigits() { 1348 DecimalFormat df = new DecimalFormat("###0.##", new DecimalFormatSymbols(Locale.US)); 1349 df.setMinimumIntegerDigits(3); 1350 assertEquals(3, df.getMinimumIntegerDigits()); 1351 assertEquals("012", df.format(12)); 1352 df.setMaximumIntegerDigits(2); 1353 assertEquals(2, df.getMinimumIntegerDigits()); 1354 assertEquals("00.7", df.format(0.7)); 1355 } 1356 1357 // See also the _formatDouble tests. This tests a subset of patterns / values. test_formatLong_scientificNotation()1358 public void test_formatLong_scientificNotation() { 1359 FormatTester formatTester = new FormatTester(); 1360 final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US); 1361 1362 DecimalFormat df = new DecimalFormat("00.0#E0", dfs); 1363 // ["00.0#E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, 1364 // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=2, 1365 // maxFractionDigits=2,minIntegerDigits=2,minFractionDigits=1,grouping=false] 1366 // Because maximum integer digit was not explicitly set: The exponent can be any integer. 1367 // Scientific notation => use significant digit logic 1368 // '@' not present: Significant digits: Min: 1, 1369 // Max: "min integer digits" (2) + "max fractional digits (2) == 4 1370 formatTester.format(df, "00.0E0", 0); 1371 formatTester.format(df, "10.0E-1", 1); 1372 formatTester.format(df, "12.0E0", 12); 1373 formatTester.format(df, "12.3E1", 123); 1374 formatTester.format(df, "12.34E2", 1234); 1375 formatTester.format(df, "12.35E3", 12346); 1376 formatTester.format(df, "10.0E4", 99999); 1377 formatTester.format(df, "-10.0E-1", -1); 1378 formatTester.format(df, "-12.0E0", -12); 1379 formatTester.format(df, "-12.3E1", -123); 1380 formatTester.format(df, "-12.34E2", -1234); 1381 formatTester.format(df, "-12.35E3", -12346); 1382 formatTester.format(df, "-10.0E4", -99999); 1383 1384 df = new DecimalFormat("##0.0E0", dfs); 1385 // ["##0.0E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, 1386 // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=3, 1387 // maxFractionDigits=1,minIntegerDigits=1,minFractionDigits=1,grouping=false] 1388 // Because maximum integer digit count is set: The exponent must be a multiple of it (3). 1389 // Scientific notation => use significant digit logic 1390 // '@' not present: Significant digits: Min: 1, 1391 // Max: "min integer digits" (1) + "max fractional digits (1) == 2 1392 formatTester.format(df, "0.0E0", 0); 1393 formatTester.format(df, "1.0E0", 1); 1394 formatTester.format(df, "12E0", 12); 1395 formatTester.format(df, "120E0", 123); 1396 formatTester.format(df, "1.2E3", 1234); 1397 formatTester.format(df, "12E3", 12346); 1398 formatTester.format(df, "100E3", 99999); 1399 formatTester.format(df, "1.0E6", 999999); 1400 1401 df = new DecimalFormat("#00.0##E0", dfs); 1402 // ["##0.0E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, 1403 // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=3, 1404 // maxFractionDigits=1,minIntegerDigits=1,minFractionDigits=1,grouping=false] 1405 // Because maximum integer digit count is set: The exponent must be a multiple of it (3). 1406 // Scientific notation => use significant digit logic 1407 // '@' not present: Significant digits: Min: 1, 1408 // Max: "min integer digits" (2) + "max fractional digits (3) == 5 1409 formatTester.format(df, "0.0E0", 0); 1410 formatTester.format(df, "1.0E0", 1); 1411 formatTester.format(df, "12E0", 12); 1412 formatTester.format(df, "123E0", 123); 1413 formatTester.format(df, "1.234E3", 1234); 1414 formatTester.format(df, "12.345E3", 12345); 1415 formatTester.format(df, "123.46E3", 123456); 1416 formatTester.format(df, "1.2346E6", 1234567); 1417 formatTester.format(df, "12.346E6", 12345678); 1418 formatTester.format(df, "100E6", 99999999); 1419 1420 df = new DecimalFormat("#.0E0", dfs); 1421 // ["#.0E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, 1422 // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=1, 1423 // maxFractionDigits=1,minIntegerDigits=0,minFractionDigits=1,grouping=false] 1424 // Because maximum integer digit count is set: The exponent must be a multiple of it (1). 1425 // Scientific notation => use significant digit logic 1426 // '@' not present: Significant digits: Min: 1, 1427 // Max: "min integer digits" (0) + "max fractional digits (1) == 1 1428 formatTester.format(df, "0.0E0", 0); 1429 formatTester.format(df, "1.0E0", 1); 1430 formatTester.format(df, "1.0E1", 12); 1431 formatTester.format(df, "1.0E2", 123); 1432 formatTester.format(df, "1.0E3", 1234); 1433 formatTester.format(df, "1.0E4", 9999); 1434 1435 df = new DecimalFormat("0.#E0", dfs); 1436 // ["0.#E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, 1437 // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=1, 1438 // maxFractionDigits=1,minIntegerDigits=1,minFractionDigits=0,grouping=false] 1439 // Because maximum integer digit was not explicitly set: The exponent can be any integer. 1440 // Scientific notation => use significant digit logic 1441 // '@' not present: Significant digits: Min: 1, 1442 // Max: "min integer digits" (1) + "max fractional digits (1) == 2 1443 formatTester.format(df, "0E0", 0); 1444 formatTester.format(df, "1E0", 1); 1445 formatTester.format(df, "1.2E1", 12); 1446 formatTester.format(df, "1.2E2", 123); 1447 formatTester.format(df, "1.2E3", 1234); 1448 formatTester.format(df, "1E4", 9999); 1449 1450 df = new DecimalFormat(".0E0", dfs); 1451 // [".0E0",isDecimalSeparatorAlwaysShown=true,groupingSize=0,multiplier=1,negativePrefix=-, 1452 // negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=0,maxFractionDigits=1, 1453 // minIntegerDigits=0,minFractionDigits=1,grouping=false] 1454 // Because maximum integer digit was not explicitly set: The exponent can be any integer. 1455 // Scientific notation => use significant digit logic 1456 // '@' not present: Significant digits: Min: 1, 1457 // Max: "min integer digits" (0) + "max fractional digits (1) == 1 1458 formatTester.format(df, ".0E0", 0); 1459 formatTester.format(df, ".1E1", 1); 1460 formatTester.format(df, ".1E2", 12); 1461 formatTester.format(df, ".1E3", 123); 1462 formatTester.format(df, ".1E4", 1234); 1463 formatTester.format(df, ".1E5", 9999); 1464 1465 formatTester.throwFailures(); 1466 } 1467 1468 // Demonstrates that fraction digit rounding occurs as expected with 1, 10 and 14 fraction 1469 // digits, using numbers that are well within the precision of IEEE 754. test_formatDouble_maxFractionDigits()1470 public void test_formatDouble_maxFractionDigits() { 1471 final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US); 1472 DecimalFormat format = new DecimalFormat("#0.#", dfs); 1473 format.setGroupingUsed(false); 1474 format.setMaximumIntegerDigits(400); 1475 format.setMaximumFractionDigits(1); 1476 1477 assertEquals("1", format.format(0.99)); 1478 assertEquals("1", format.format(0.95)); 1479 assertEquals("0.9", format.format(0.94)); 1480 assertEquals("0.9", format.format(0.90)); 1481 1482 assertEquals("0.2", format.format(0.19)); 1483 assertEquals("0.2", format.format(0.15)); 1484 assertEquals("0.1", format.format(0.14)); 1485 assertEquals("0.1", format.format(0.10)); 1486 1487 format.setMaximumFractionDigits(10); 1488 assertEquals("1", format.format(0.99999999999)); 1489 assertEquals("1", format.format(0.99999999995)); 1490 assertEquals("0.9999999999", format.format(0.99999999994)); 1491 assertEquals("0.9999999999", format.format(0.99999999990)); 1492 1493 assertEquals("0.1111111112", format.format(0.11111111119)); 1494 assertEquals("0.1111111112", format.format(0.11111111115)); 1495 assertEquals("0.1111111111", format.format(0.11111111114)); 1496 assertEquals("0.1111111111", format.format(0.11111111110)); 1497 1498 format.setMaximumFractionDigits(14); 1499 assertEquals("1", format.format(0.999999999999999)); 1500 assertEquals("1", format.format(0.999999999999995)); 1501 assertEquals("0.99999999999999", format.format(0.999999999999994)); 1502 assertEquals("0.99999999999999", format.format(0.999999999999990)); 1503 1504 assertEquals("0.11111111111112", format.format(0.111111111111119)); 1505 assertEquals("0.11111111111112", format.format(0.111111111111115)); 1506 assertEquals("0.11111111111111", format.format(0.111111111111114)); 1507 assertEquals("0.11111111111111", format.format(0.111111111111110)); 1508 } 1509 1510 // This demonstrates rounding at the 15th decimal digit. By setting the maximum fraction digits 1511 // we force rounding at a point just below the full IEEE 754 precision. IEEE 754 should be 1512 // precise to just above 15 decimal digits. 1513 // df.format() with no limits always emits up to 16 decimal digits, slightly above what IEEE 754 1514 // can store precisely. test_formatDouble_roundingTo15Digits()1515 public void test_formatDouble_roundingTo15Digits() throws Exception { 1516 final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US); 1517 DecimalFormat df = new DecimalFormat("#.#", dfs); 1518 df.setMaximumIntegerDigits(400); 1519 df.setGroupingUsed(false); 1520 1521 df.setMaximumFractionDigits(0); 1522 assertEquals("1000000000000000", df.format(999999999999999.9)); 1523 df.setMaximumFractionDigits(1); 1524 assertEquals("100000000000000", df.format(99999999999999.99)); 1525 df.setMaximumFractionDigits(2); 1526 assertEquals("10000000000000", df.format(9999999999999.999)); 1527 df.setMaximumFractionDigits(3); 1528 assertEquals("1000000000000", df.format(999999999999.9999)); 1529 df.setMaximumFractionDigits(4); 1530 assertEquals("100000000000", df.format(99999999999.99999)); 1531 df.setMaximumFractionDigits(5); 1532 assertEquals("10000000000", df.format(9999999999.999999)); 1533 df.setMaximumFractionDigits(6); 1534 assertEquals("1000000000", df.format(999999999.9999999)); 1535 df.setMaximumFractionDigits(7); 1536 assertEquals("100000000", df.format(99999999.99999999)); 1537 df.setMaximumFractionDigits(8); 1538 assertEquals("10000000", df.format(9999999.999999999)); 1539 df.setMaximumFractionDigits(9); 1540 assertEquals("1000000", df.format(999999.9999999999)); 1541 df.setMaximumFractionDigits(10); 1542 assertEquals("100000", df.format(99999.99999999999)); 1543 df.setMaximumFractionDigits(11); 1544 assertEquals("10000", df.format(9999.999999999999)); 1545 df.setMaximumFractionDigits(12); 1546 assertEquals("1000", df.format(999.9999999999999)); 1547 df.setMaximumFractionDigits(13); 1548 assertEquals("100", df.format(99.99999999999999)); 1549 df.setMaximumFractionDigits(14); 1550 assertEquals("10", df.format(9.999999999999999)); 1551 df.setMaximumFractionDigits(15); 1552 assertEquals("1", df.format(0.9999999999999999)); 1553 } 1554 1555 // This checks formatting over most of the representable IEEE 754 range using a formatter that 1556 // should be performing no lossy rounding. 1557 // It checks that the formatted double can be parsed to get the original double. 1558 // IEEE 754 can represent values from (decimal) ~2.22507E−308 to ~1.79769E308 to full precision. 1559 // Close to zero it can go down to 4.94066E-324 with a loss of precision. 1560 // At the extremes of the double range this test will not be testing doubles that exactly 1561 // represent powers of 10. The test is only interested in whether the doubles closest 1562 // to powers of 10 that can be represented can each be turned into a string and read back again 1563 // to arrive at the original double. test_formatDouble_wideRange()1564 public void test_formatDouble_wideRange() throws Exception { 1565 for (int i = -324; i < 309; i++) { 1566 // Generate a decimal number we are interested in: 1 * 10^i 1567 String stringForm = "1e" + i; 1568 BigDecimal bigDecimal = new BigDecimal(stringForm); 1569 1570 // Obtain the nearest double representation of the decimal number. 1571 // This is lossy because going from BigDecimal -> double is inexact, but we should 1572 // arrive at a number that is as close as possible to the decimal value. We assume that 1573 // BigDecimal is capable of this, but it is not critical to this test if it gets it a 1574 // little wrong, though it may mean we are testing a double value different from the one 1575 // we thought we were. 1576 double d = bigDecimal.doubleValue(); 1577 1578 assertDecimalFormatIsLossless(d); 1579 } 1580 } 1581 1582 // This test is a regression test for http://b/17656132. 1583 // It checks hand-picked values can be formatted and parsed to get the original double. 1584 // The formatter as configured should perform no rounding. test_formatDouble_roundingProblemCases()1585 public void test_formatDouble_roundingProblemCases() throws Exception { 1586 // Most of the double literals below are not exactly representable in IEEE 754 but 1587 // it should not matter to this test. 1588 assertDecimalFormatIsLossless(999999999999999.9); 1589 assertDecimalFormatIsLossless(99999999999999.99); 1590 assertDecimalFormatIsLossless(9999999999999.999); 1591 assertDecimalFormatIsLossless(999999999999.9999); 1592 assertDecimalFormatIsLossless(99999999999.99999); 1593 assertDecimalFormatIsLossless(9999999999.999999); 1594 assertDecimalFormatIsLossless(999999999.9999999); 1595 assertDecimalFormatIsLossless(99999999.99999999); 1596 assertDecimalFormatIsLossless(9999999.999999999); 1597 assertDecimalFormatIsLossless(999999.9999999999); 1598 assertDecimalFormatIsLossless(99999.99999999999); 1599 assertDecimalFormatIsLossless(9999.999999999999); 1600 assertDecimalFormatIsLossless(999.9999999999999); 1601 assertDecimalFormatIsLossless(99.99999999999999); 1602 assertDecimalFormatIsLossless(9.999999999999999); 1603 assertDecimalFormatIsLossless(0.9999999999999999); 1604 } 1605 1606 // This test checks hand-picked values can be formatted and parsed to get the original double. 1607 // The formatter as configured should perform no rounding. 1608 // These numbers are not affected by http://b/17656132. test_formatDouble_varyingScale()1609 public void test_formatDouble_varyingScale() throws Exception { 1610 // Most of the double literals below are not exactly representable in IEEE 754 but 1611 // it should not matter to this test. 1612 1613 assertDecimalFormatIsLossless(999999999999999.); 1614 1615 assertDecimalFormatIsLossless(123456789012345.); 1616 assertDecimalFormatIsLossless(12345678901234.5); 1617 assertDecimalFormatIsLossless(1234567890123.25); 1618 assertDecimalFormatIsLossless(999999999999.375); 1619 assertDecimalFormatIsLossless(99999999999.0625); 1620 assertDecimalFormatIsLossless(9999999999.03125); 1621 assertDecimalFormatIsLossless(999999999.015625); 1622 assertDecimalFormatIsLossless(99999999.0078125); 1623 assertDecimalFormatIsLossless(9999999.00390625); 1624 assertDecimalFormatIsLossless(999999.001953125); 1625 assertDecimalFormatIsLossless(9999.00048828125); 1626 assertDecimalFormatIsLossless(999.000244140625); 1627 assertDecimalFormatIsLossless(99.0001220703125); 1628 assertDecimalFormatIsLossless(9.00006103515625); 1629 assertDecimalFormatIsLossless(0.000030517578125); 1630 } 1631 assertDecimalFormatIsLossless(double d)1632 private static void assertDecimalFormatIsLossless(double d) throws Exception { 1633 final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US); 1634 DecimalFormat format = new DecimalFormat("#0.#", dfs); 1635 format.setGroupingUsed(false); 1636 format.setMaximumIntegerDigits(400); 1637 format.setMaximumFractionDigits(400); 1638 1639 // Every floating point binary can be represented exactly in decimal if you have enough 1640 // digits. This shows the value actually being tested. 1641 String testId = "decimalValue: " + new BigDecimal(d); 1642 1643 // As a sanity check we try out parseDouble() with the string generated by 1644 // Double.toString(). Strictly speaking Double.toString() is probably not guaranteed to be 1645 // lossless, but in reality it probably is, or at least is close enough. 1646 assertDoubleEqual( 1647 testId + " failed parseDouble(toString()) sanity check", 1648 d, Double.parseDouble(Double.toString(d))); 1649 1650 // Format the number: If this is lossy it is a problem. We are trying to check that it 1651 // doesn't lose any unnecessary precision. 1652 String result = format.format(d); 1653 1654 // Here we use Double.parseDouble() which should able to parse a number we know was 1655 // representable as a double into the original double. If parseDouble() is not implemented 1656 // correctly the test is invalid. 1657 double doubleParsed = Double.parseDouble(result); 1658 assertDoubleEqual(testId + " (format() produced " + result + ")", 1659 d, doubleParsed); 1660 1661 // For completeness we try to parse using the formatter too. If this fails but the format 1662 // above didn't it may be a problem with parse(), or with format() that we didn't spot. 1663 assertDoubleEqual(testId + " failed parse(format()) check", 1664 d, format.parse(result).doubleValue()); 1665 } 1666 assertDoubleEqual(String message, double d, double doubleParsed)1667 private static void assertDoubleEqual(String message, double d, double doubleParsed) { 1668 assertEquals(message, 1669 createPrintableDouble(d),createPrintableDouble(doubleParsed)); 1670 } 1671 createPrintableDouble(double d)1672 private static String createPrintableDouble(double d) { 1673 return Double.toString(d) + "(" + Long.toHexString(Double.doubleToRawLongBits(d)) + ")"; 1674 } 1675 1676 // Concise demonstration of http://b/17656132 using hard-coded expected values. test_formatDouble_bug17656132()1677 public void test_formatDouble_bug17656132() { 1678 final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US); 1679 DecimalFormat df = new DecimalFormat("#0.#", dfs); 1680 df.setGroupingUsed(false); 1681 df.setMaximumIntegerDigits(400); 1682 df.setMaximumFractionDigits(400); 1683 1684 // The expected values below come from the RI and are correct 16 decimal digit 1685 // representations of the formatted value. Android does something different. 1686 // The decimal value given in each comment is the actual double value as represented by 1687 // IEEE 754. See new BigDecimal(double). 1688 1689 // double: 999999999999999.9 is decimal 999999999999999.875 1690 assertEquals("999999999999999.9", df.format(999999999999999.9)); 1691 // double: 99999999999999.98 is decimal 99999999999999.984375 1692 assertEquals("99999999999999.98", df.format(99999999999999.98)); 1693 // double 9999999999999.998 is decimal 9999999999999.998046875 1694 assertEquals("9999999999999.998", df.format(9999999999999.998)); 1695 // double 999999999999.9999 is decimal 999999999999.9998779296875 1696 assertEquals("999999999999.9999", df.format(999999999999.9999)); 1697 // double 99999999999.99998 is decimal 99999999999.9999847412109375 1698 assertEquals("99999999999.99998", df.format(99999999999.99998)); 1699 // double 9999999999.999998 is decimal 9999999999.9999980926513671875 1700 assertEquals("9999999999.999998", df.format(9999999999.999998)); 1701 // double 1E23 is decimal 99999999999999991611392 1702 assertEquals("9999999999999999", df.format(1E23)); 1703 } 1704 test_getDecimalFormatSymbols()1705 public void test_getDecimalFormatSymbols() { 1706 DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(Locale.ENGLISH); 1707 DecimalFormatSymbols dfs = df.getDecimalFormatSymbols(); 1708 assertNotSame(dfs, df.getDecimalFormatSymbols()); 1709 } 1710 test_getCurrency()1711 public void test_getCurrency() { 1712 Currency currK = Currency.getInstance("KRW"); 1713 Currency currX = Currency.getInstance("XXX"); 1714 Currency currE = Currency.getInstance("EUR"); 1715 1716 DecimalFormat df = (DecimalFormat) NumberFormat.getCurrencyInstance(new Locale("ko", "KR")); 1717 assertTrue("Test1: Returned incorrect currency", df.getCurrency() == currK); 1718 1719 df = (DecimalFormat) NumberFormat.getCurrencyInstance(new Locale("", "KR")); 1720 assertTrue("Test2: Returned incorrect currency", df.getCurrency() == currK); 1721 1722 df = (DecimalFormat) NumberFormat.getCurrencyInstance(new Locale("ko", "")); 1723 assertTrue("Test3: Returned incorrect currency", df.getCurrency() == currX); 1724 1725 df = (DecimalFormat) NumberFormat.getCurrencyInstance(new Locale("fr", "FR")); 1726 assertTrue("Test4: Returned incorrect currency", df.getCurrency() == currE); 1727 1728 // Regression for HARMONY-1351 1729 df = (DecimalFormat) NumberFormat.getCurrencyInstance(new Locale("QWERTY")); 1730 assertTrue("Test5: Returned incorrect currency", df.getCurrency() == currX); 1731 1732 // JDK fails these tests since it doesn't have the PREEURO variant 1733 // df = (DecimalFormat)NumberFormat.getCurrencyInstance(new Locale("fr", 1734 // "FR","PREEURO")); 1735 // assertTrue("Test5: Returned incorrect currency", df.getCurrency() == 1736 // currF); 1737 } 1738 test_getGroupingSize()1739 public void test_getGroupingSize() { 1740 DecimalFormat df = new DecimalFormat("###0.##"); 1741 assertEquals("Wrong unset size", 0, df.getGroupingSize()); 1742 df = new DecimalFormat("#,##0.##"); 1743 assertEquals("Wrong set size", 3, df.getGroupingSize()); 1744 df = new DecimalFormat("#,###,###0.##"); 1745 assertEquals("Wrong multiple set size", 4, df.getGroupingSize()); 1746 } 1747 test_getMultiplier()1748 public void test_getMultiplier() { 1749 final int defaultMultiplier = 1; 1750 DecimalFormat form = (DecimalFormat) NumberFormat.getInstance(Locale.US); 1751 assertEquals(defaultMultiplier, form.getMultiplier()); 1752 1753 DecimalFormat df = new DecimalFormat("###0.##"); 1754 assertEquals("Wrong unset multiplier", 1, df.getMultiplier()); 1755 df = new DecimalFormat("###0.##%"); 1756 assertEquals("Wrong percent multiplier", 100, df.getMultiplier()); 1757 df = new DecimalFormat("###0.##\u2030"); 1758 assertEquals("Wrong mille multiplier", 1000, df.getMultiplier()); 1759 } 1760 test_isDecimalSeparatorAlwaysShown()1761 public void test_isDecimalSeparatorAlwaysShown() { 1762 DecimalFormat df = new DecimalFormat("###0.##"); 1763 assertTrue("Wrong unset value", !df.isDecimalSeparatorAlwaysShown()); 1764 df = new DecimalFormat("###0.00"); 1765 assertTrue("Wrong unset2 value", !df.isDecimalSeparatorAlwaysShown()); 1766 df = new DecimalFormat("###0."); 1767 assertTrue("Wrong set value", df.isDecimalSeparatorAlwaysShown()); 1768 } 1769 test_parse_withParsePosition()1770 public void test_parse_withParsePosition() { 1771 DecimalFormat format = (DecimalFormat) NumberFormat.getNumberInstance(Locale.ENGLISH); 1772 ParsePosition pos = new ParsePosition(0); 1773 Number result = format.parse("9223372036854775807", pos); 1774 assertTrue("Wrong result type for Long.MAX_VALUE", result.getClass() == Long.class); 1775 assertTrue("Wrong result Long.MAX_VALUE", result.longValue() == Long.MAX_VALUE); 1776 pos = new ParsePosition(0); 1777 result = format.parse("-9223372036854775808", pos); 1778 assertTrue("Wrong result type for Long.MIN_VALUE", result.getClass() == Long.class); 1779 assertTrue("Wrong result Long.MIN_VALUE: " + result.longValue(), 1780 result.longValue() == Long.MIN_VALUE); 1781 pos = new ParsePosition(0); 1782 result = format.parse("9223372036854775808", pos); 1783 assertTrue("Wrong result type for Long.MAX_VALUE+1", result.getClass() == Double.class); 1784 assertTrue("Wrong result Long.MAX_VALUE + 1", 1785 result.doubleValue() == (double) Long.MAX_VALUE + 1); 1786 pos = new ParsePosition(0); 1787 result = format.parse("-9223372036854775809", pos); 1788 assertTrue("Wrong result type for Long.MIN_VALUE+1", result.getClass() == Double.class); 1789 assertTrue("Wrong result Long.MIN_VALUE - 1", 1790 result.doubleValue() == (double) Long.MIN_VALUE - 1); 1791 1792 pos = new ParsePosition(0); 1793 result = format.parse("18446744073709551629", pos); 1794 assertTrue("Wrong result type for overflow", result.getClass() == Double.class); 1795 assertTrue("Wrong result for overflow", result.doubleValue() == 18446744073709551629d); 1796 1797 pos = new ParsePosition(0); 1798 result = format.parse("42325917317067571199", pos); 1799 assertTrue("Wrong result type for overflow a: " + result, 1800 result.getClass() == Double.class); 1801 assertTrue("Wrong result for overflow a: " + result, 1802 result.doubleValue() == 42325917317067571199d); 1803 pos = new ParsePosition(0); 1804 result = format.parse("4232591731706757119E1", pos); 1805 assertTrue("Wrong result type for overflow b: " + result, 1806 result.getClass() == Double.class); 1807 assertTrue("Wrong result for overflow b: " + result, 1808 result.doubleValue() == 42325917317067571190d); 1809 pos = new ParsePosition(0); 1810 result = format.parse(".42325917317067571199E20", pos); 1811 assertTrue("Wrong result type for overflow c: " + result, 1812 result.getClass() == Double.class); 1813 assertTrue("Wrong result for overflow c: " + result, 1814 result.doubleValue() == 42325917317067571199d); 1815 pos = new ParsePosition(0); 1816 result = format.parse("922337203685477580.9E1", pos); 1817 assertTrue("Wrong result type for overflow d: " + result, 1818 result.getClass() == Double.class); 1819 assertTrue("Wrong result for overflow d: " + result, 1820 result.doubleValue() == 9223372036854775809d); 1821 pos = new ParsePosition(0); 1822 result = format.parse("9.223372036854775809E18", pos); 1823 assertTrue("Wrong result type for overflow e: " + result, 1824 result.getClass() == Double.class); 1825 assertTrue("Wrong result for overflow e: " + result, 1826 result.doubleValue() == 9223372036854775809d); 1827 } 1828 test_parse_withMultiplier()1829 public void test_parse_withMultiplier() { 1830 DecimalFormat format = (DecimalFormat) NumberFormat.getNumberInstance(Locale.ENGLISH); 1831 Number result; 1832 1833 format.setMultiplier(100); 1834 result = format.parse("9223372036854775807", new ParsePosition(0)); 1835 assertEquals("Wrong result type multiplier 100: " + result, Double.class, 1836 result.getClass()); 1837 assertEquals("Wrong result for multiplier 100: " + result, 1838 92233720368547758.07d, result.doubleValue()); 1839 1840 format.setMultiplier(1000); 1841 result = format.parse("9223372036854775807", new ParsePosition(0)); 1842 assertEquals("Wrong result type multiplier 1000: " + result, Double.class, 1843 result.getClass()); 1844 assertEquals("Wrong result for multiplier 1000: " + result, 1845 9223372036854775.807d, result.doubleValue()); 1846 1847 format.setMultiplier(10000); 1848 result = format.parse("9223372036854775807", new ParsePosition(0)); 1849 assertEquals("Wrong result type multiplier 10000: " + result, 1850 Double.class, result.getClass()); 1851 assertEquals("Wrong result for multiplier 10000: " + result, 1852 922337203685477.5807d, result.doubleValue()); 1853 } 1854 test_setDecimalFormatSymbols()1855 public void test_setDecimalFormatSymbols() { 1856 DecimalFormat df = new DecimalFormat("###0.##"); 1857 DecimalFormatSymbols dfs = new DecimalFormatSymbols(); 1858 dfs.setDecimalSeparator('@'); 1859 df.setDecimalFormatSymbols(dfs); 1860 assertTrue("Not set", df.getDecimalFormatSymbols().equals(dfs)); 1861 assertEquals("Symbols not used", "1@2", df.format(1.2)); 1862 1863 // The returned symbols may be cloned in two spots 1864 // 1. When set 1865 // 2. When returned 1866 DecimalFormat format = new DecimalFormat(); 1867 DecimalFormatSymbols symbols = new DecimalFormatSymbols(); 1868 format.setDecimalFormatSymbols(symbols); 1869 DecimalFormatSymbols symbolsOut = format.getDecimalFormatSymbols(); 1870 assertNotSame(symbols, symbolsOut); 1871 } 1872 test_setDecimalSeparatorAlwaysShown()1873 public void test_setDecimalSeparatorAlwaysShown() { 1874 DecimalFormat df = new DecimalFormat("###0.##", new DecimalFormatSymbols(Locale.US)); 1875 assertEquals("Wrong default result", "5", df.format(5)); 1876 df.setDecimalSeparatorAlwaysShown(true); 1877 assertTrue("Not set", df.isDecimalSeparatorAlwaysShown()); 1878 assertEquals("Wrong set result", "7.", df.format(7)); 1879 } 1880 test_setCurrency()1881 public void test_setCurrency() { 1882 Locale locale = Locale.CANADA; 1883 DecimalFormat df = ((DecimalFormat) NumberFormat.getCurrencyInstance(locale)); 1884 1885 try { 1886 df.setCurrency(null); 1887 fail("Expected NullPointerException"); 1888 } catch (NullPointerException e) { 1889 } 1890 1891 Currency currency = Currency.getInstance("AED"); 1892 df.setCurrency(currency); 1893 assertTrue("Returned incorrect currency", currency == df.getCurrency()); 1894 assertEquals("Returned incorrect currency symbol", currency.getSymbol(locale), 1895 df.getDecimalFormatSymbols().getCurrencySymbol()); 1896 assertEquals("Returned incorrect international currency symbol", currency.getCurrencyCode(), 1897 df.getDecimalFormatSymbols().getInternationalCurrencySymbol()); 1898 } 1899 test_setGroupingSize()1900 public void test_setGroupingSize() { 1901 DecimalFormat df = new DecimalFormat("###0.##", new DecimalFormatSymbols(Locale.ENGLISH)); 1902 df.setGroupingUsed(true); 1903 df.setGroupingSize(2); 1904 assertEquals("Value not set", 2, df.getGroupingSize()); 1905 String result = df.format(123); 1906 assertTrue("Invalid format:" + result, result.equals("1,23")); 1907 } 1908 testSerializationSelf()1909 public void testSerializationSelf() throws Exception { 1910 SerializationTest.verifySelf(new DecimalFormat()); 1911 } 1912 testSerializationHarmonyRICompatible()1913 public void testSerializationHarmonyRICompatible() throws Exception { 1914 NumberFormat nf = NumberFormat.getInstance(Locale.FRANCE); 1915 1916 DecimalFormat df = null; 1917 if (!(nf instanceof DecimalFormat)) { 1918 throw new Error("This NumberFormat is not a DecimalFormat"); 1919 1920 } 1921 df = (DecimalFormat) nf; 1922 1923 ObjectInputStream oinput = null; 1924 1925 DecimalFormat deserializedDF = null; 1926 1927 try { 1928 oinput = new ObjectInputStream(getClass().getResource( 1929 "/serialization/org/apache/harmony/tests/java/text/DecimalFormat.ser") 1930 .openStream()); 1931 deserializedDF = (DecimalFormat) oinput.readObject(); 1932 } finally { 1933 try { 1934 if (null != oinput) { 1935 oinput.close(); 1936 } 1937 } catch (Exception e) { 1938 // ignore 1939 } 1940 } 1941 1942 assertEquals(df.getNegativePrefix(), deserializedDF.getNegativePrefix()); 1943 assertEquals(df.getNegativeSuffix(), deserializedDF.getNegativeSuffix()); 1944 assertEquals(df.getPositivePrefix(), deserializedDF.getPositivePrefix()); 1945 assertEquals(df.getPositiveSuffix(), deserializedDF.getPositiveSuffix()); 1946 assertEquals(df.getCurrency(), deserializedDF.getCurrency()); 1947 1948 DecimalFormatSymbolsTest.assertDecimalFormatSymbolsRIFrance( 1949 deserializedDF.getDecimalFormatSymbols()); 1950 1951 assertEquals(df.getGroupingSize(), df.getGroupingSize()); 1952 assertEquals(df.getMaximumFractionDigits(), deserializedDF 1953 .getMaximumFractionDigits()); 1954 1955 assertEquals(df.getMaximumIntegerDigits(), deserializedDF 1956 .getMaximumIntegerDigits()); 1957 1958 assertEquals(df.getMinimumFractionDigits(), deserializedDF 1959 .getMinimumFractionDigits()); 1960 assertEquals(df.getMinimumIntegerDigits(), deserializedDF 1961 .getMinimumIntegerDigits()); 1962 assertEquals(df.getMultiplier(), deserializedDF.getMultiplier()); 1963 1964 // Deliberately omitted this assertion. Since different data resource 1965 // will cause the assertion fail. 1966 // assertEquals(df, deserializedDF); 1967 1968 } 1969 test_parse_infinityBigDecimalFalse()1970 public void test_parse_infinityBigDecimalFalse() { 1971 // Regression test for HARMONY-106 1972 DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(); 1973 DecimalFormatSymbols symbols = new DecimalFormatSymbols(); 1974 Number number = format.parse(symbols.getInfinity(), 1975 new ParsePosition(0)); 1976 assertTrue(number instanceof Double); 1977 assertTrue(Double.isInfinite(number.doubleValue())); 1978 } 1979 test_parse_minusInfinityBigDecimalFalse()1980 public void test_parse_minusInfinityBigDecimalFalse() { 1981 // Regression test for HARMONY-106 1982 DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(); 1983 DecimalFormatSymbols symbols = new DecimalFormatSymbols(); 1984 Number number = format.parse("-" + symbols.getInfinity(), 1985 new ParsePosition(0)); 1986 assertTrue(number instanceof Double); 1987 assertTrue(Double.isInfinite(number.doubleValue())); 1988 } 1989 test_setDecimalFormatSymbolsAsNull()1990 public void test_setDecimalFormatSymbolsAsNull() { 1991 // Regression for HARMONY-1070 1992 DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(); 1993 format.setDecimalFormatSymbols(null); 1994 } 1995 test_formatToCharacterIterator_null()1996 public void test_formatToCharacterIterator_null() { 1997 try { 1998 // Regression for HARMONY-466 1999 new DecimalFormat().formatToCharacterIterator(null); 2000 fail("NullPointerException expected"); 2001 } catch (NullPointerException e) { 2002 // expected 2003 } 2004 } 2005 test_formatToCharacterIterator_original()2006 public void test_formatToCharacterIterator_original() { 2007 new Support_DecimalFormat( 2008 "test_formatToCharacterIteratorLjava_lang_Object") 2009 .t_formatToCharacterIterator(); 2010 } 2011 test_formatToCharacterIterator()2012 public void test_formatToCharacterIterator() throws Exception { 2013 AttributedCharacterIterator iterator; 2014 int[] runStarts; 2015 int[] runLimits; 2016 String result; 2017 char current; 2018 2019 // BigInteger. 2020 iterator = new DecimalFormat().formatToCharacterIterator(new BigInteger("123456789")); 2021 runStarts = new int[] { 0, 0, 0, 3, 4, 4, 4, 7, 8, 8, 8 }; 2022 runLimits = new int[] { 3, 3, 3, 4, 7, 7, 7, 8, 11, 11, 11 }; 2023 result = "123,456,789"; 2024 current = iterator.current(); 2025 for (int i = 0; i < runStarts.length; i++) { 2026 assertEquals("wrong start @" + i, runStarts[i], iterator.getRunStart()); 2027 assertEquals("wrong limit @" + i, runLimits[i], iterator.getRunLimit()); 2028 assertEquals("wrong char @" + i, result.charAt(i), current); 2029 current = iterator.next(); 2030 } 2031 assertEquals(0, iterator.getBeginIndex()); 2032 assertEquals(11, iterator.getEndIndex()); 2033 2034 // For BigDecimal with multiplier test. 2035 DecimalFormat df = new DecimalFormat(); 2036 df.setMultiplier(10); 2037 iterator = df.formatToCharacterIterator(new BigDecimal("12345678901234567890")); 2038 result = "123,456,789,012,345,678,900"; 2039 current = iterator.current(); 2040 for (int i = 0; i < result.length(); i++) { 2041 assertEquals("wrong char @" + i, result.charAt(i), current); 2042 current = iterator.next(); 2043 } 2044 2045 // For BigDecimal with multiplier test. 2046 df = new DecimalFormat(); 2047 df.setMultiplier(-1); 2048 df.setMaximumFractionDigits(20); 2049 iterator = df.formatToCharacterIterator(new BigDecimal("1.23456789012345678901")); 2050 result = "-1.23456789012345678901"; 2051 current = iterator.current(); 2052 for (int i = 0; i < result.length(); i++) { 2053 assertEquals("wrong char @" + i, result.charAt(i), current); 2054 current = iterator.next(); 2055 } 2056 2057 iterator = new DecimalFormat().formatToCharacterIterator(new BigDecimal("1.23456789E301")); 2058 runStarts = new int[] { 0, 0, 2, 3, 3, 3, 6, 7, 7, 7, 10, 11, 11, 11, 14 }; 2059 runLimits = new int[] { 2, 2, 3, 6, 6, 6, 7, 10, 10, 10, 11, 14, 14, 14, 15 }; 2060 result = "12,345,678,900,"; // 000,000,000,000.... 2061 current = iterator.current(); 2062 for (int i = 0; i < runStarts.length; i++) { 2063 assertEquals("wrong start @" + i, runStarts[i], iterator.getRunStart()); 2064 assertEquals("wrong limit @" + i, runLimits[i], iterator.getRunLimit()); 2065 assertEquals("wrong char @" + i, result.charAt(i), current); 2066 current = iterator.next(); 2067 } 2068 assertEquals(0, iterator.getBeginIndex()); 2069 assertEquals(402, iterator.getEndIndex()); 2070 2071 iterator = new DecimalFormat().formatToCharacterIterator(new BigDecimal("1.2345678E4")); 2072 runStarts = new int[] { 0, 0, 2, 3, 3, 3, 6, 7, 7, 7 }; 2073 runLimits = new int[] { 2, 2, 3, 6, 6, 6, 7, 10, 10, 10 }; 2074 result = "12,345.678"; 2075 current = iterator.current(); 2076 for (int i = 0; i < runStarts.length; i++) { 2077 assertEquals("wrong start @" + i, runStarts[i], iterator.getRunStart()); 2078 assertEquals("wrong limit @" + i, runLimits[i], iterator.getRunLimit()); 2079 assertEquals("wrong char @" + i, result.charAt(i), current); 2080 current = iterator.next(); 2081 } 2082 assertEquals(0, iterator.getBeginIndex()); 2083 assertEquals(10, iterator.getEndIndex()); 2084 } 2085 test_formatToCharacterIterator_veryLarge()2086 public void test_formatToCharacterIterator_veryLarge() throws Exception { 2087 AttributedCharacterIterator iterator; 2088 int[] runStarts; 2089 int[] runLimits; 2090 String result; 2091 char current; 2092 2093 Number number = new BigDecimal("1.23456789E1234"); 2094 assertEquals("1.23456789E+1234", number.toString()); 2095 iterator = new DecimalFormat().formatToCharacterIterator(number); 2096 runStarts = new int[] { 0, 0, 2, 3, 3, 3, 6, 7, 7, 7, 10, 11, 11, 11, 14 }; 2097 runLimits = new int[] { 2, 2, 3, 6, 6, 6, 7, 10, 10, 10, 11, 14, 14, 14, 15 }; 2098 result = "12,345,678,900,"; // 000,000,000,000.... 2099 current = iterator.current(); 2100 for (int i = 0; i < runStarts.length; i++) { 2101 assertEquals("wrong start @" + i, runStarts[i], iterator.getRunStart()); 2102 assertEquals("wrong limit @" + i, runLimits[i], iterator.getRunLimit()); 2103 assertEquals("wrong char @" + i, result.charAt(i), current); 2104 current = iterator.next(); 2105 } 2106 assertEquals(0, iterator.getBeginIndex()); 2107 assertEquals(1646, iterator.getEndIndex()); 2108 } 2109 test_formatToCharacterIterator_roundingUnnecessaryArithmeticException()2110 public void test_formatToCharacterIterator_roundingUnnecessaryArithmeticException() { 2111 DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getInstance(Locale.US); 2112 decimalFormat.setRoundingMode(RoundingMode.UNNECESSARY); 2113 decimalFormat.setMaximumFractionDigits(0); 2114 try { 2115 // when rounding is needed, but RoundingMode is set to RoundingMode.UNNECESSARY, 2116 // throw ArithmeticException 2117 decimalFormat.formatToCharacterIterator(new Double(1.5)); 2118 fail("ArithmeticException expected"); 2119 } catch (ArithmeticException e) { 2120 // expected 2121 } 2122 } 2123 test_formatDouble_roundingUnnecessaryArithmeticException()2124 public void test_formatDouble_roundingUnnecessaryArithmeticException() { 2125 DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getInstance(Locale.US); 2126 decimalFormat.setMaximumFractionDigits(0); 2127 decimalFormat.setRoundingMode(RoundingMode.UNNECESSARY); 2128 2129 try { 2130 // when rounding is needed, but RoundingMode is set to RoundingMode.UNNECESSARY, 2131 // throw ArithmeticException 2132 decimalFormat.format(11.5, new StringBuffer(), new FieldPosition(0)); 2133 fail("ArithmeticException expected"); 2134 } catch (ArithmeticException e) { 2135 // expected 2136 } 2137 } 2138 test_format_roundingUnnecessaryArithmeticException()2139 public void test_format_roundingUnnecessaryArithmeticException() { 2140 final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US); 2141 DecimalFormat decimalFormat = new DecimalFormat("00.0#E0", dfs); 2142 2143 decimalFormat.setRoundingMode(RoundingMode.UNNECESSARY); 2144 try { 2145 // when rounding is needed, but RoundingMode is set to RoundingMode.UNNECESSARY, 2146 // throw ArithmeticException 2147 decimalFormat.format(99999, new StringBuffer(), new FieldPosition(0)); 2148 fail("ArithmeticException expected"); 2149 } catch (ArithmeticException e) { 2150 // expected 2151 } 2152 } 2153 test_getRoundingMode()2154 public void test_getRoundingMode() { 2155 // get the default RoundingMode of this DecimalFormat 2156 DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getInstance(Locale.US); 2157 2158 // the default RoundingMode is HALF_EVEN 2159 assertEquals("Incorrect default RoundingMode", 2160 decimalFormat.getRoundingMode(), RoundingMode.HALF_EVEN); 2161 2162 // set RoundingMode.HALF_DOWN of this DecimalFormat 2163 decimalFormat.setRoundingMode(RoundingMode.HALF_DOWN); 2164 assertEquals("Returned incorrect RoundingMode", 2165 decimalFormat.getRoundingMode(), RoundingMode.HALF_DOWN); 2166 2167 } 2168 test_setRoundingMode_null()2169 public void test_setRoundingMode_null() { 2170 DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getInstance(Locale.US); 2171 try { 2172 // when the given RoundingMode is null, throw NullPointerException 2173 decimalFormat.setRoundingMode(null); 2174 fail("NullPointerException expected"); 2175 } catch (NullPointerException e) { 2176 // expected 2177 } 2178 } 2179 test_format_withRoundingMode()2180 public void test_format_withRoundingMode() { 2181 DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getInstance(Locale.US); 2182 // ignore the fraction part of a given value 2183 decimalFormat.setMaximumFractionDigits(0); 2184 2185 // set RoundingMode.HALF_DOWN of this DecimalFormat and test its 2186 // behavior 2187 decimalFormat.setRoundingMode(RoundingMode.HALF_DOWN); 2188 String result = decimalFormat.format(11.3); 2189 assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN", "11", result); 2190 2191 result = decimalFormat.format(11.5); 2192 assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN", "11", result); 2193 2194 result = decimalFormat.format(11.6); 2195 assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN", "12", result); 2196 2197 // set RoundingMode.CEILING of this DecimalFormat and test its 2198 // behavior 2199 decimalFormat.setRoundingMode(RoundingMode.CEILING); 2200 result = decimalFormat.format(11.3); 2201 assertEquals("Incorrect RoundingMode behavior: RoundingMode.CEILING", "12", result); 2202 2203 result = decimalFormat.format(-11.5); 2204 assertEquals("Incorrect RoundingMode behavior: RoundingMode.CEILING", "-11", result); 2205 2206 // set RoundingMode.DOWN of this DecimalFormat and test its 2207 // behavior 2208 decimalFormat.setRoundingMode(RoundingMode.DOWN); 2209 result = decimalFormat.format(11.3); 2210 assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN", "11", result); 2211 2212 result = decimalFormat.format(-11.5); 2213 assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN", "-11", result); 2214 2215 result = decimalFormat.format(0); 2216 assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN", "0", result); 2217 2218 // set RoundingMode.FLOOR of this DecimalFormat and test its 2219 // behavior 2220 decimalFormat.setRoundingMode(RoundingMode.FLOOR); 2221 result = decimalFormat.format(11.3); 2222 assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR", "11", result); 2223 2224 result = decimalFormat.format(-11.5); 2225 assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR", "-12", result); 2226 2227 result = decimalFormat.format(0); 2228 assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR", "0", result); 2229 2230 // set RoundingMode.HALF_EVEN of this DecimalFormat and test its 2231 // behavior 2232 decimalFormat.setRoundingMode(RoundingMode.HALF_EVEN); 2233 result = decimalFormat.format(5.5); 2234 assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN", "6", result); 2235 2236 result = decimalFormat.format(-5.5); 2237 assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN", "-6", result); 2238 2239 result = decimalFormat.format(0.2); 2240 assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN", "0", result); 2241 2242 // set RoundingMode.HALF_UP of this DecimalFormat and test its 2243 // behavior 2244 decimalFormat.setRoundingMode(RoundingMode.HALF_UP); 2245 result = decimalFormat.format(5.5); 2246 assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", "6", result); 2247 2248 result = decimalFormat.format(-5.5); 2249 assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", "-6", result); 2250 2251 result = decimalFormat.format(0.2); 2252 assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", "0", result); 2253 2254 result = decimalFormat.format(-0.2); 2255 assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", "-0", result); 2256 2257 // set RoundingMode.UP of this DecimalFormat and test its 2258 // behavior 2259 decimalFormat.setRoundingMode(RoundingMode.UP); 2260 result = decimalFormat.format(5.5); 2261 assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP", "6", result); 2262 2263 result = decimalFormat.format(-5.5); 2264 assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP", "-6", result); 2265 2266 result = decimalFormat.format(0.2); 2267 assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP", "1", result); 2268 2269 result = decimalFormat.format(-0.2); 2270 assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP", "-1", result); 2271 2272 // set RoundingMode.UNNECESSARY of this DecimalFormat and test its 2273 // behavior 2274 decimalFormat.setRoundingMode(RoundingMode.UNNECESSARY); 2275 2276 try { 2277 // when rounding is needed but RoundingMode is set to RoundingMode.UNNECESSARY, 2278 // throw ArithmeticException 2279 result = decimalFormat.format(5.5); 2280 fail("ArithmeticException expected: RoundingMode.UNNECESSARY"); 2281 } catch (ArithmeticException e) { 2282 // expected 2283 } 2284 2285 result = decimalFormat.format(1.0); 2286 assertEquals( 2287 "Incorrect RoundingMode behavior: RoundingMode.UNNECESSARY", "1", result); 2288 2289 result = decimalFormat.format(-1.0); 2290 assertEquals( 2291 "Incorrect RoundingMode behavior: RoundingMode.UNNECESSARY", "-1", result); 2292 2293 // set MaxFractionDigits to 3, test different DecimalFormat format 2294 // function with differnt RoundingMode 2295 decimalFormat.setMaximumFractionDigits(3); 2296 2297 // set RoundingMode.HALF_DOWN of this DecimalFormat and test its 2298 // behavior 2299 decimalFormat.setRoundingMode(RoundingMode.HALF_DOWN); 2300 result = decimalFormat.format(11.5653); 2301 assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN", "11.565", result); 2302 2303 result = decimalFormat.format(11.5655); 2304 assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN", "11.565", result); 2305 2306 result = decimalFormat.format(11.5656); 2307 assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN", "11.566", result); 2308 2309 // set RoundingMode.CEILING of this DecimalFormat and test its 2310 // behavior 2311 decimalFormat.setRoundingMode(RoundingMode.CEILING); 2312 result = decimalFormat.format(11.5653); 2313 assertEquals("Incorrect RoundingMode behavior: RoundingMode.CEILING", "11.566", result); 2314 2315 result = decimalFormat.format(-11.5653); 2316 assertEquals("Incorrect RoundingMode behavior: RoundingMode.CEILING", "-11.565", result); 2317 2318 // set RoundingMode.DOWN of this DecimalFormat and test its 2319 // behavior 2320 decimalFormat.setRoundingMode(RoundingMode.DOWN); 2321 result = decimalFormat.format(11.5653); 2322 assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN", "11.565", result); 2323 2324 result = decimalFormat.format(-11.5653); 2325 assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN", "-11.565", result); 2326 2327 result = decimalFormat.format(0); 2328 assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN", "0", result); 2329 2330 // set RoundingMode.FLOOR of this DecimalFormat and test its 2331 // behavior 2332 decimalFormat.setRoundingMode(RoundingMode.FLOOR); 2333 result = decimalFormat.format(11.5653); 2334 assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR", "11.565", result); 2335 2336 result = decimalFormat.format(-11.5655); 2337 assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR", "-11.566", result); 2338 2339 result = decimalFormat.format(0); 2340 assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR", "0", result); 2341 2342 // set RoundingMode.HALF_EVEN of this DecimalFormat and test its 2343 // behavior 2344 decimalFormat.setRoundingMode(RoundingMode.HALF_EVEN); 2345 result = decimalFormat.format(11.5653); 2346 assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN", "11.565", result); 2347 2348 result = decimalFormat.format(-11.5655); 2349 assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN", "-11.566", result); 2350 2351 result = decimalFormat.format(11.5656); 2352 assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN", "11.566", result); 2353 2354 // set RoundingMode.HALF_UP of this DecimalFormat and test its 2355 // behavior 2356 decimalFormat.setRoundingMode(RoundingMode.HALF_UP); 2357 result = decimalFormat.format(11.5653); 2358 assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", "11.565", result); 2359 2360 result = decimalFormat.format(-11.5655); 2361 assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", "-11.566", result); 2362 2363 result = decimalFormat.format(11.5656); 2364 assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", "11.566", result); 2365 2366 // set RoundingMode.UP of this DecimalFormat and test its 2367 // behavior 2368 decimalFormat.setRoundingMode(RoundingMode.UP); 2369 result = decimalFormat.format(11.5653); 2370 assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP", "11.566", result); 2371 2372 result = decimalFormat.format(-11.5655); 2373 assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP", "-11.566", result); 2374 2375 // set RoundingMode.UNNECESSARY of this DecimalFormat and test its 2376 // behavior 2377 decimalFormat.setRoundingMode(RoundingMode.UNNECESSARY); 2378 result = decimalFormat.format(-11.565); 2379 assertEquals("Incorrect RoundingMode behavior: RoundingMode.UNNECESSARY", 2380 "-11.565", result); 2381 2382 result = decimalFormat.format(11.565); 2383 assertEquals("Incorrect RoundingMode behavior: RoundingMode.UNNECESSARY", 2384 "11.565", result); 2385 2386 // when setting MaxFractionDigits to negative value -2, default it as 2387 // zero, test different DecimalFormat format 2388 // function with differnt RoundingMode 2389 decimalFormat.setMaximumFractionDigits(-2); 2390 2391 // set RoundingMode.HALF_DOWN of this DecimalFormat and test its 2392 // behavior 2393 decimalFormat.setRoundingMode(RoundingMode.HALF_DOWN); 2394 result = decimalFormat.format(11.3); 2395 assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN", "11", result); 2396 2397 result = decimalFormat.format(11.5); 2398 assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN", "11", result); 2399 2400 result = decimalFormat.format(11.6); 2401 assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN", "12", result); 2402 2403 // set RoundingMode.CEILING of this DecimalFormat and test its 2404 // behavior 2405 decimalFormat.setRoundingMode(RoundingMode.CEILING); 2406 result = decimalFormat.format(11.3); 2407 assertEquals("Incorrect RoundingMode behavior: RoundingMode.CEILING", "12", result); 2408 2409 result = decimalFormat.format(-11.5); 2410 assertEquals("Incorrect RoundingMode behavior: RoundingMode.CEILING", "-11", result); 2411 2412 // set RoundingMode.DOWN of this DecimalFormat and test its 2413 // behavior 2414 decimalFormat.setRoundingMode(RoundingMode.DOWN); 2415 result = decimalFormat.format(11.3); 2416 assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN", "11", result); 2417 2418 result = decimalFormat.format(-11.5); 2419 assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN", "-11", result); 2420 2421 result = decimalFormat.format(0); 2422 assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN", "0", result); 2423 2424 // set RoundingMode.FLOOR of this DecimalFormat and test its 2425 // behavior 2426 decimalFormat.setRoundingMode(RoundingMode.FLOOR); 2427 result = decimalFormat.format(11.3); 2428 assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR", "11", result); 2429 2430 result = decimalFormat.format(-11.5); 2431 assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR", "-12", result); 2432 2433 result = decimalFormat.format(0); 2434 assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR", "0", result); 2435 2436 // set RoundingMode.HALF_EVEN of this DecimalFormat and test its 2437 // behavior 2438 decimalFormat.setRoundingMode(RoundingMode.HALF_EVEN); 2439 result = decimalFormat.format(5.5); 2440 assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN", "6", result); 2441 2442 result = decimalFormat.format(-5.5); 2443 assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN", "-6", result); 2444 2445 result = decimalFormat.format(0.2); 2446 assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN", "0", result); 2447 2448 // set RoundingMode.HALF_UP of this DecimalFormat and test its 2449 // behavior 2450 decimalFormat.setRoundingMode(RoundingMode.HALF_UP); 2451 result = decimalFormat.format(5.5); 2452 assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", "6", result); 2453 2454 result = decimalFormat.format(-5.5); 2455 assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", "-6", result); 2456 2457 result = decimalFormat.format(0.2); 2458 assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", "0", result); 2459 2460 result = decimalFormat.format(-0.2); 2461 assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", "-0", result); 2462 2463 // set RoundingMode.UP of this DecimalFormat and test its 2464 // behavior 2465 decimalFormat.setRoundingMode(RoundingMode.UP); 2466 result = decimalFormat.format(5.5); 2467 assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP", "6", result); 2468 2469 result = decimalFormat.format(-5.5); 2470 assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP", "-6", result); 2471 2472 result = decimalFormat.format(0.2); 2473 assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP", "1", result); 2474 2475 result = decimalFormat.format(-0.2); 2476 assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP", "-1", result); 2477 2478 // set RoundingMode.UNNECESSARY of this DecimalFormat and test its 2479 // behavior 2480 decimalFormat.setRoundingMode(RoundingMode.UNNECESSARY); 2481 2482 result = decimalFormat.format(1.0); 2483 assertEquals( 2484 "Incorrect RoundingMode behavior: RoundingMode.UNNECESSARY", "1", result); 2485 2486 result = decimalFormat.format(-1.0); 2487 assertEquals( 2488 "Incorrect RoundingMode behavior: RoundingMode.UNNECESSARY", "-1", result); 2489 2490 // Regression for HARMONY-6485 2491 // Test with applyPattern call after setRoundingMode 2492 2493 // set RoundingMode.HALF_UP of this DecimalFormat and test its 2494 // behavior 2495 decimalFormat.setRoundingMode(RoundingMode.HALF_UP); 2496 decimalFormat.applyPattern(".##"); 2497 result = decimalFormat.format(0.125); 2498 assertEquals("Incorrect RoundingMode behavior after applyPattern", ".13", result); 2499 result = decimalFormat.format(0.255); 2500 assertEquals("Incorrect RoundingMode behavior after applyPattern", ".26", result); 2501 result = decimalFormat.format(0.732); 2502 assertEquals("Incorrect RoundingMode behavior after applyPattern", ".73", result); 2503 result = decimalFormat.format(0.467); 2504 assertEquals("Incorrect RoundingMode behavior after applyPattern", ".47", result); 2505 2506 // set RoundingMode.HALF_DOWN of this DecimalFormat and test its 2507 // behavior 2508 decimalFormat.setRoundingMode(RoundingMode.HALF_DOWN); 2509 decimalFormat.applyPattern(".##"); 2510 result = decimalFormat.format(0.125); 2511 assertEquals("Incorrect RoundingMode behavior after applyPattern", ".12", result); 2512 result = decimalFormat.format(0.255); 2513 assertEquals("Incorrect RoundingMode behavior after applyPattern", ".25", result); 2514 result = decimalFormat.format(0.732); 2515 assertEquals("Incorrect RoundingMode behavior after applyPattern", ".73", result); 2516 result = decimalFormat.format(0.467); 2517 assertEquals("Incorrect RoundingMode behavior after applyPattern", ".47", result); 2518 2519 // set RoundingMode.UP of this DecimalFormat and test its 2520 // behavior 2521 decimalFormat.setRoundingMode(RoundingMode.UP); 2522 decimalFormat.applyPattern(".##"); 2523 result = decimalFormat.format(0.125); 2524 assertEquals("Incorrect RoundingMode behavior after applyPattern", ".13", result); 2525 result = decimalFormat.format(0.255); 2526 assertEquals("Incorrect RoundingMode behavior after applyPattern", ".26", result); 2527 result = decimalFormat.format(0.732); 2528 assertEquals("Incorrect RoundingMode behavior after applyPattern", ".74", result); 2529 result = decimalFormat.format(0.467); 2530 assertEquals("Incorrect RoundingMode behavior after applyPattern", ".47", result); 2531 2532 // set RoundingMode.DOWN of this DecimalFormat and test its 2533 // behavior 2534 decimalFormat.setRoundingMode(RoundingMode.DOWN); 2535 decimalFormat.applyPattern(".##"); 2536 result = decimalFormat.format(0.125); 2537 assertEquals("Incorrect RoundingMode behavior after applyPattern", ".12", result); 2538 result = decimalFormat.format(0.255); 2539 assertEquals("Incorrect RoundingMode behavior after applyPattern", ".25", result); 2540 result = decimalFormat.format(0.732); 2541 assertEquals("Incorrect RoundingMode behavior after applyPattern", ".73", result); 2542 result = decimalFormat.format(0.467); 2543 assertEquals("Incorrect RoundingMode behavior after applyPattern", ".46", result); 2544 2545 // set RoundingMode.HALF_EVEN of this DecimalFormat and test its 2546 // behavior 2547 decimalFormat.setRoundingMode(RoundingMode.HALF_EVEN); 2548 decimalFormat.applyPattern(".##"); 2549 result = decimalFormat.format(0.125); 2550 assertEquals("Incorrect RoundingMode behavior after applyPattern", ".12", result); 2551 result = decimalFormat.format(0.255); 2552 assertEquals("Incorrect RoundingMode behavior after applyPattern", ".26", result); 2553 result = decimalFormat.format(0.732); 2554 assertEquals("Incorrect RoundingMode behavior after applyPattern", ".73", result); 2555 result = decimalFormat.format(0.467); 2556 assertEquals("Incorrect RoundingMode behavior after applyPattern", ".47", result); 2557 } 2558 2559 private static class FormatTester { 2560 private List<AssertionFailedError> failures = new ArrayList<AssertionFailedError>(); 2561 format(DecimalFormat format, String expected, double value)2562 public void format(DecimalFormat format, String expected, double value) { 2563 try { 2564 Assert.assertEquals(format.toPattern() + ": " + value, expected, 2565 format.format(value)); 2566 } catch (AssertionFailedError e) { 2567 failures.add(e); 2568 } 2569 } 2570 format(DecimalFormat format, String expected, int value)2571 public void format(DecimalFormat format, String expected, int value) { 2572 try { 2573 Assert.assertEquals(format.toPattern() + ": " + value, expected, 2574 format.format(value)); 2575 } catch (AssertionFailedError e) { 2576 failures.add(e); 2577 } 2578 } 2579 throwFailures()2580 public void throwFailures() throws AssertionFailedError { 2581 if (failures.isEmpty()) { 2582 return; 2583 } 2584 if (failures.size() == 1) { 2585 throw failures.get(0); 2586 } 2587 AssertionFailedError combined = new AssertionFailedError("Multiple format failures"); 2588 for (AssertionFailedError failure : failures) { 2589 combined.addSuppressed(failure); 2590 } 2591 throw combined; 2592 } 2593 } 2594 } 2595