1 /* 2 * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4018937 8008577 27 * @summary Confirm that methods which are newly added to support BigDecimal and BigInteger work as expected. 28 * @library /java/text/testlib 29 * @run main/othervm -Djava.locale.providers=COMPAT,SPI BigDecimalFormat 30 */ 31 32 package test.java.text.Format.NumberFormat; 33 34 import java.math.BigDecimal; 35 import java.math.BigInteger; 36 import java.text.*; 37 import java.util.*; 38 39 import test.java.text.testlib.IntlTest; 40 41 public class BigDecimalFormat extends IntlTest { 42 main(String[] args)43 public static void main(String[] args) throws Exception { 44 new BigDecimalFormat().run(args); 45 } 46 47 static final String nonsep_int = 48 "123456789012345678901234567890123456789012345678901234567890" + 49 "123456789012345678901234567890123456789012345678901234567890" + 50 "123456789012345678901234567890123456789012345678901234567890" + 51 "123456789012345678901234567890123456789012345678901234567890" + 52 "123456789012345678901234567890123456789012345678901234567890" + 53 "123456789012345678901234567890123456789012345678901234567890"; 54 55 static final String sep_int = 56 "123,456,789,012,345,678,901,234,567,890," + 57 "123,456,789,012,345,678,901,234,567,890," + 58 "123,456,789,012,345,678,901,234,567,890," + 59 "123,456,789,012,345,678,901,234,567,890," + 60 "123,456,789,012,345,678,901,234,567,890," + 61 "123,456,789,012,345,678,901,234,567,890," + 62 "123,456,789,012,345,678,901,234,567,890," + 63 "123,456,789,012,345,678,901,234,567,890," + 64 "123,456,789,012,345,678,901,234,567,890," + 65 "123,456,789,012,345,678,901,234,567,890," + 66 "123,456,789,012,345,678,901,234,567,890," + 67 "123,456,789,012,345,678,901,234,567,890"; 68 69 static final String nonsep_zero = 70 "000000000000000000000000000000000000000000000000000000000000" + 71 "000000000000000000000000000000000000000000000000000000000000" + 72 "000000000000000000000000000000000000000000000000000000000000" + 73 "000000000000000000000000000000000000000000000000000000000000" + 74 "000000000000000000000000000000000000000000000000000000000000" + 75 "000000000000000000000000000000000000000000000000000000000000"; 76 77 static final String sep_zero = 78 "000,000,000,000,000,000,000,000,000,000," + 79 "000,000,000,000,000,000,000,000,000,000," + 80 "000,000,000,000,000,000,000,000,000,000," + 81 "000,000,000,000,000,000,000,000,000,000," + 82 "000,000,000,000,000,000,000,000,000,000," + 83 "000,000,000,000,000,000,000,000,000,000," + 84 "000,000,000,000,000,000,000,000,000,000," + 85 "000,000,000,000,000,000,000,000,000,000," + 86 "000,000,000,000,000,000,000,000,000,000," + 87 "000,000,000,000,000,000,000,000,000,000," + 88 "000,000,000,000,000,000,000,000,000,000," + 89 "000,000,000,000,000,000,000,000,000,000"; 90 91 static final String fra = 92 "012345678901234567890123456789012345678901234567890123456789" + 93 "012345678901234567890123456789012345678901234567890123456789" + 94 "012345678901234567890123456789012345678901234567890123456789" + 95 "012345678901234567890123456789012345678901234567890123456789" + 96 "012345678901234567890123456789012345678901234567890123456789" + 97 "012345678901234567890123456789012345678901234567890123456789"; 98 99 100 StringBuffer formatted = new StringBuffer(1000); 101 FieldPosition fp; 102 103 /** 104 * Test for normal big numbers which have the fraction part 105 */ test_Format_in_NumberFormat_BigDecimal()106 void test_Format_in_NumberFormat_BigDecimal() { 107 String from, to; 108 109 NumberFormat nf = NumberFormat.getInstance(Locale.US); 110 ((DecimalFormat)nf).applyPattern("#,##0.###"); 111 setDigits(nf, Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 0); 112 113 // From: 0.000...789 114 // To: 0.000...789 (same as From) 115 formatted.setLength(0); 116 from = "0." + nonsep_zero + "123456789"; 117 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 118 checkFormat(from, formatted, from, ((DecimalFormat)nf).getMultiplier()); 119 120 /* ------------------------------------------------------------------ */ 121 122 // From: -0.000...789 123 // To: -0.000...789 (same as From) 124 // ~ : FieldPosition(SIGN) 125 fp = new FieldPosition(NumberFormat.Field.SIGN); 126 formatted.setLength(0); 127 from = "-0." + nonsep_zero + "123456789"; 128 nf.format(new BigDecimal(from), formatted, fp); 129 checkFormat(from, formatted, from, ((DecimalFormat)nf).getMultiplier()); 130 checkFieldPosition(from, fp, 0, 1); 131 132 /* ------------------------------------------------------------------ */ 133 134 // From: 1234...7890.012...789 135 // To: 123,4...7,890.012...789 136 // ~~~~~~~~~~~~~ : FieldPosition(INTEGER_FIELD) 137 fp = new FieldPosition(DecimalFormat.INTEGER_FIELD); 138 formatted.setLength(0); 139 from = nonsep_int + "." + fra; 140 to = sep_int + "." + fra; 141 nf.format(new BigDecimal(from), formatted, fp); 142 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 143 checkFieldPosition(from, fp, 0, 479); 144 145 /* ------------------------------------------------------------------ */ 146 147 // From: -1234...7890.012...789 148 // To: -123,4...7,890.012...789 149 // ~~~~~~~~~ : FieldPosition(FRACTION_FIELD) 150 fp = new FieldPosition(DecimalFormat.FRACTION_FIELD); 151 formatted.setLength(0); 152 from = "-" + nonsep_int + "." + fra; 153 to = "-" + sep_int + "." + fra; 154 nf.format(new BigDecimal(from), formatted, fp); 155 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 156 checkFieldPosition(from, fp, 481, 841); 157 158 /* ------------------------------------------------------------------ */ 159 160 // From: 1234...78900000...0000.000...789 161 // To: 123,4...7,890,000,0...0,000.000...789 162 formatted.setLength(0); 163 from = nonsep_int + nonsep_zero + "." + nonsep_zero + fra; 164 to = sep_int + "," + sep_zero + "." + nonsep_zero + fra; 165 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 166 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 167 168 /* ------------------------------------------------------------------ */ 169 170 // From: -1234...78900000...0000.000...789 171 // To: -123,4...7,890,000,0...0,000.000...789 172 formatted.setLength(0); 173 from = "-" + nonsep_int + nonsep_zero + "." + nonsep_zero + fra; 174 to = "-" + sep_int + "," + sep_zero + "." + nonsep_zero + fra; 175 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 176 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 177 178 /* ------------------------------------------------------------------ */ 179 180 // From: 1234...78900000...0000 181 // To: 123,4...7,890,000,0...0,000 182 formatted.setLength(0); 183 from = nonsep_int + nonsep_zero; 184 to = sep_int + "," + sep_zero; 185 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 186 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 187 188 /* ------------------------------------------------------------------ */ 189 190 // From: -1234...78900000...0000 191 // To: -123,4...7,890,000,0...0,000 192 formatted.setLength(0); 193 from = "-" + nonsep_int + nonsep_zero; 194 to = "-" + sep_int + "," + sep_zero; 195 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 196 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 197 198 /* ------------------------------------------------------------------ */ 199 200 // From: 1234...78900000...0000.0...0 201 // To: 1,234...7,890,000,0...0,000 202 formatted.setLength(0); 203 from = nonsep_int + nonsep_zero + "." + nonsep_zero; 204 to = sep_int + "," + sep_zero; 205 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 206 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 207 208 /* ------------------------------------------------------------------ */ 209 210 // From: -1234...78900000...0000.0...0 211 // To: -1,234...7,890,000,0...0,000 212 formatted.setLength(0); 213 from = "-" + nonsep_int + nonsep_zero + "." + nonsep_zero; 214 to = "-" + sep_int + "," + sep_zero; 215 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 216 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 217 218 /* ------------------------------------------------------------------ */ 219 220 // From: 000...0000 221 // To: 0 222 formatted.setLength(0); 223 from = nonsep_zero; 224 to = "0"; 225 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 226 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 227 228 /* ------------------------------------------------------------------ */ 229 230 // From: -000...0000 231 // To: 0 232 formatted.setLength(0); 233 from = "-" + nonsep_zero; 234 to = "0"; 235 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 236 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 237 238 /* ------------------------------------------------------------------ */ 239 240 // From: 000...00001234 241 // To: 1,234 242 formatted.setLength(0); 243 from = nonsep_zero + "1234"; 244 to = "1,234"; 245 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 246 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 247 248 /* ------------------------------------------------------------------ */ 249 250 // From: -000...00001234 251 // To: -1,234 252 // ~ : FieldPosition(GROUPING_SEPARATOR) 253 fp = new FieldPosition(NumberFormat.Field.GROUPING_SEPARATOR); 254 formatted.setLength(0); 255 from = "-" + nonsep_zero + "1234"; 256 to = "-1,234"; 257 nf.format(new BigDecimal(from), formatted, fp); 258 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 259 checkFieldPosition(from, fp, 2, 3); 260 261 /* ------------------------------------------------------------------ */ 262 263 // From: 000...0000.0...0 264 // To: 0 265 formatted.setLength(0); 266 from = nonsep_zero + "." + nonsep_zero; 267 to = "0"; 268 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 269 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 270 271 /* ------------------------------------------------------------------ */ 272 273 ((DecimalFormat)nf).applyPattern("#,##0.0"); 274 setDigits(nf, Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 1); 275 276 // From: -000...0000.0...0 277 // To: 0.0 278 formatted.setLength(0); 279 from = "-" + nonsep_zero + "." + nonsep_zero; 280 to = "0.0"; 281 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 282 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 283 284 /* ------------------------------------------------------------------ */ 285 286 // From: 1234...7890.012...7890...0 287 // To: 1,234...7,890.0123...789 288 formatted.setLength(0); 289 from = nonsep_int + "." + fra + nonsep_zero; 290 to = sep_int + "." + fra; 291 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 292 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 293 294 /* ------------------------------------------------------------------ */ 295 296 // From: -1234...7890.012...7890...0 297 // To: -1,234...7,890.0123...789 298 formatted.setLength(0); 299 from = "-" + nonsep_int + "." + fra + nonsep_zero; 300 to = "-" + sep_int + "." + fra; 301 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 302 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 303 304 /* ------------------------------------------------------------------ */ 305 306 ((DecimalFormat)nf).applyPattern("0.###E0"); 307 setDigits(nf, 1, 1, Integer.MAX_VALUE, 0); 308 309 // From: 1123...890.012...789 310 // To : 1.123...8900123...789E360 311 // ~~~ : FieldPosition(EXPONENT) 312 fp = new FieldPosition(NumberFormat.Field.EXPONENT); 313 formatted.setLength(0); 314 from = "1" + nonsep_int + "." + fra; 315 to = "1." + nonsep_int + fra + "E360"; 316 nf.format(new BigDecimal(from), formatted, fp); 317 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 318 checkFieldPosition(from, fp, 723, 726); 319 320 /* ------------------------------------------------------------------ */ 321 322 // From: -1123...890.012...789 323 // To : -1.123...8900123...789E360 324 formatted.setLength(0); 325 from = "-1" + nonsep_int + "." + fra; 326 to = "-1." + nonsep_int + fra + "E360"; 327 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 328 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 329 330 /* ------------------------------------------------------------------ */ 331 332 ((DecimalFormat)nf).applyPattern("0.###E0"); 333 setDigits(nf, 1, 1, Integer.MAX_VALUE, 0); 334 335 // From: 0.000...0001123...890.012...789 336 // To : 1.123...8900123...789E-360 337 formatted.setLength(0); 338 from = "0." + nonsep_zero + "1" + fra; 339 to = "1." + fra + "E-361"; 340 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 341 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 342 343 /* ------------------------------------------------------------------ */ 344 345 // From: -0.000...0001123...890.012...789 346 // To : -1.123...8900123...789E-360 347 formatted.setLength(0); 348 from = "-0." + nonsep_zero + "1" + fra; 349 to = "-1." + fra + "E-361"; 350 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 351 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 352 353 /* ------------------------------------------------------------------ */ 354 355 // From: 1123...890.012...789000...000 356 // To : 1.123...8900123...789E360 357 formatted.setLength(0); 358 from = "1" + nonsep_int + "." + fra + nonsep_zero; 359 to = "1." + nonsep_int + fra + "E360"; 360 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 361 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 362 363 /* ------------------------------------------------------------------ */ 364 365 // From: -1123...890.012...789000...000 366 // To : -1.123...8900123...789E360 367 // ~ : FieldPosition(EXPONENT_SYMBOL) 368 fp = new FieldPosition(NumberFormat.Field.EXPONENT_SYMBOL); 369 formatted.setLength(0); 370 from = "-1" + nonsep_int + "." + fra + nonsep_zero; 371 to = "-1." + nonsep_int + fra + "E360"; 372 nf.format(new BigDecimal(from), formatted, fp); 373 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 374 checkFieldPosition(from, fp, 723, 724); 375 376 /* ------------------------------------------------------------------ */ 377 378 // From: 0.000...0001123...890.012...789000...000 379 // To : 1.123...8900123...789E-360 380 // ~ : FieldPosition(EXPONENT_SIGN) 381 fp = new FieldPosition(NumberFormat.Field.EXPONENT_SIGN); 382 formatted.setLength(0); 383 from = "0." + nonsep_zero + "1" + fra + nonsep_zero; 384 to = "1." + fra + "E-361"; 385 nf.format(new BigDecimal(from), formatted, fp); 386 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 387 checkFieldPosition(from, fp, 363, 364); 388 389 /* ------------------------------------------------------------------ */ 390 391 // From: -0.000...0001123...890.012...789000...000 392 // To : -1.123...8900123...789E-360 393 formatted.setLength(0); 394 from = "-0." + nonsep_zero + "1" + fra + nonsep_zero; 395 to = "-1." + fra + "E-361"; 396 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 397 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 398 399 /* ------------------------------------------------------------------ */ 400 401 // From: ABC1123...890.012...789 402 // To : ABC1.123...890.0123...789 403 formatted = new StringBuffer("ABC"); 404 from = "1" + nonsep_int + "." + fra; 405 to = "ABC1." + nonsep_int + fra + "E360"; 406 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 407 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 408 409 /* ------------------------------------------------------------------ */ 410 411 // From: ABC-1123...890.012...789 412 // To : ABC-1.123...890.0123...789 413 // ~ : FieldPosition(DECIMAL_SEPARATOR) 414 fp = new FieldPosition(NumberFormat.Field.DECIMAL_SEPARATOR); 415 formatted = new StringBuffer("ABC"); 416 from = "-1" + nonsep_int + "." + fra; 417 to = "ABC-1." + nonsep_int + fra + "E360"; 418 nf.format(new BigDecimal(from), formatted, fp); 419 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 420 checkFieldPosition(from, fp, 5, 6); 421 422 /* ------------------------------------------------------------------ */ 423 424 ((DecimalFormat)nf).applyPattern("#,##0.###"); 425 setDigits(nf, Integer.MAX_VALUE, 1, 726, 0); 426 427 // From: 0.000...000012...7890123456789 428 // To: 0.000...000012...789012346 (Shorter than From) 429 formatted.setLength(0); 430 from = "0." + nonsep_zero + fra + fra; 431 to = "0." + nonsep_zero + fra + "012346"; 432 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 433 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 434 435 ((DecimalFormat)nf).applyPattern("#,##0.###"); 436 setDigits(nf, Integer.MAX_VALUE, 1, 723, 0); 437 438 /* ------------------------------------------------------------------ */ 439 440 // From: -0.000...000012...7890123456789 441 // To: -0.000...000012...789012 (Shorter than From) 442 formatted.setLength(0); 443 from = "-0." + nonsep_zero + fra + fra; 444 to = "-0." + nonsep_zero + fra + "012"; 445 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 446 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 447 448 /* ------------------------------------------------------------------ */ 449 450 ((DecimalFormat)nf).applyPattern("00000.###E0"); 451 setDigits(nf, 5, 5, 370, 0); 452 453 // From: 1234567890.012...78901234567890 454 // To: 12345.67890012...789012346E5 455 formatted.setLength(0); 456 from = "1234567890." + fra + "0123456789"; 457 to = "12345.67890" + fra + "01235E5"; 458 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 459 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 460 461 /* ------------------------------------------------------------------ */ 462 463 ((DecimalFormat)nf).applyPattern("0.###E0"); 464 setDigits(nf, 1, 1, 364, 0); 465 466 // From: -0.000...0001012...7890123456789 467 // To: -1.012...789012E-361 468 formatted.setLength(0); 469 from = "-0." + nonsep_zero + "1" + fra + "0123456789"; 470 to = "-1." + fra + "0123E-361"; 471 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 472 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 473 474 /* ------------------------------------------------------------------ */ 475 476 ((DecimalFormat)nf).applyPattern("0.###E0"); 477 setDigits(nf, 1, 1, 366, 0); 478 479 // From: 1012...78901234567890 480 // To: 1.012...789012346E370 481 formatted.setLength(0); 482 from = "1" + fra + "0123456789"; 483 to = "1." + fra + "012346E370"; 484 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 485 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 486 487 /* ------------------------------------------------------------------ */ 488 489 ((DecimalFormat)nf).applyPattern("0.###E0"); 490 setDigits(nf, 1, 1, 363, 0); 491 492 // From: -1012...7890123456789 493 // To: -1.012...789012E370 494 formatted.setLength(0); 495 from = "-1" + fra + "0123456789"; 496 to = "-1." + fra + "012E370"; 497 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 498 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 499 500 /* ------------------------------------------------------------------ */ 501 502 ((DecimalFormat)nf).applyPattern("#,##0.###"); 503 setDigits(nf, Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 720); 504 505 // From: 1234...78900000...0000.0...0 506 // To: 1,234...7,890,000,0...0,000.0...0 507 formatted.setLength(0); 508 from = nonsep_int + nonsep_zero + "." + nonsep_zero + nonsep_zero; 509 to = sep_int + "," + sep_zero + "." + nonsep_zero + nonsep_zero; 510 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 511 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 512 513 /* ------------------------------------------------------------------ */ 514 515 // From: -1234...78900000...0000.0...0 516 // To: -1,234...7,890,000,0...0,000.0...0 517 formatted.setLength(0); 518 from = "-" + nonsep_int + nonsep_zero + "." + nonsep_zero + nonsep_zero; 519 to = "-" + sep_int + "," + sep_zero + "." + nonsep_zero + nonsep_zero; 520 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 521 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 522 } 523 524 /** 525 * Test for normal big numbers which have the fraction part with multiplier 526 */ test_Format_in_NumberFormat_BigDecimal_usingMultiplier()527 void test_Format_in_NumberFormat_BigDecimal_usingMultiplier() { 528 String from, to; 529 530 NumberFormat nf = NumberFormat.getInstance(Locale.US); 531 ((DecimalFormat)nf).applyPattern("#,##0.###"); 532 setDigits(nf, Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 0); 533 ((DecimalFormat)nf).setMultiplier(250000000); 534 ((DecimalFormat)nf).setDecimalSeparatorAlwaysShown(true); 535 536 // From: 1000...0000.000...000 537 // To: 250,0...0,000. 538 formatted.setLength(0); 539 from = "1" + nonsep_zero + "." + nonsep_zero; 540 to = "250,000,000," + sep_zero + "."; 541 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 542 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 543 544 /* ------------------------------------------------------------------ */ 545 546 ((DecimalFormat)nf).setDecimalSeparatorAlwaysShown(false); 547 548 // From: -1000...0000.000...000 549 // To: -250,0...0,000 550 formatted.setLength(0); 551 from = "-1" + nonsep_zero + "." + nonsep_zero; 552 to = "-250,000,000," + sep_zero; 553 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 554 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 555 556 /* ------------------------------------------------------------------ */ 557 558 ((DecimalFormat)nf).applyPattern("#,##0.###"); 559 setDigits(nf, Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 0); 560 ((DecimalFormat)nf).setMultiplier(-250000000); 561 ((DecimalFormat)nf).setDecimalSeparatorAlwaysShown(true); 562 563 // From: 1000...0000.000...000 564 // To: -250,0...0,000. 565 formatted.setLength(0); 566 from = "1" + nonsep_zero + "." + nonsep_zero; 567 to = "-250,000,000," + sep_zero + "."; 568 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 569 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 570 571 /* ------------------------------------------------------------------ */ 572 573 ((DecimalFormat)nf).setDecimalSeparatorAlwaysShown(false); 574 575 // From: -1000...0000.000...000 576 // To: 250,0...0,000 577 formatted.setLength(0); 578 from = "-1" + nonsep_zero + "." + nonsep_zero; 579 to = "250,000,000," + sep_zero; 580 nf.format(new BigDecimal(from), formatted, new FieldPosition(0)); 581 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 582 } 583 584 /** 585 * Test for normal big numbers which don't have the fraction part 586 */ test_Format_in_NumberFormat_BigInteger()587 void test_Format_in_NumberFormat_BigInteger() { 588 String from, to; 589 590 NumberFormat nf = NumberFormat.getInstance(Locale.US); 591 if (!(nf instanceof DecimalFormat)) { 592 throw new RuntimeException("Couldn't get DecimalFormat instance."); 593 } 594 595 ((DecimalFormat)nf).applyPattern("#,##0.###"); 596 setDigits(nf, Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 0); 597 598 // From: 1234...7890 599 // To: 123,4...7,890 600 formatted.setLength(0); 601 from = nonsep_int; 602 to = sep_int; 603 nf.format(new BigInteger(from), formatted, new FieldPosition(0)); 604 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 605 606 /* ------------------------------------------------------------------ */ 607 608 // From: -1234...7890 609 // To: -123,4...7,890 610 // ~~~~~~~~~~~~~ : FieldPosition(INTEGER_FIELD) 611 fp = new FieldPosition(DecimalFormat.INTEGER_FIELD); 612 formatted.setLength(0); 613 from = "-" + nonsep_int; 614 to = "-" + sep_int; 615 nf.format(new BigInteger(from), formatted, fp); 616 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 617 checkFieldPosition(from, fp, 1, 480); 618 619 /* ------------------------------------------------------------------ */ 620 621 // From: 000...0001234...7890 622 // To: 123,4...7,890 623 formatted.setLength(0); 624 from = nonsep_zero + nonsep_int; 625 to = sep_int; 626 nf.format(new BigInteger(from), formatted, new FieldPosition(0)); 627 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 628 629 /* ------------------------------------------------------------------ */ 630 631 // From: -000...0001234...7890 632 // To: -123,4...7,890 633 // ~ : FieldPosition(SIGN) 634 fp = new FieldPosition(NumberFormat.Field.SIGN); 635 formatted.setLength(0); 636 from = "-" + nonsep_zero + nonsep_int; 637 to = "-" + sep_int; 638 nf.format(new BigInteger(from), formatted, fp); 639 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 640 checkFieldPosition(from, fp, 0, 1); 641 642 /* ------------------------------------------------------------------ */ 643 644 // From: 000...0000 645 // To: 0 646 formatted.setLength(0); 647 from = nonsep_zero; 648 to = "0"; 649 nf.format(new BigInteger(from), formatted, new FieldPosition(0)); 650 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 651 652 /* ------------------------------------------------------------------ */ 653 654 ((DecimalFormat)nf).applyPattern("#,##0.0"); 655 setDigits(nf, Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 1); 656 657 // From: -000...0000 658 // To: 0.0 659 fp = new FieldPosition(NumberFormat.Field.DECIMAL_SEPARATOR); 660 formatted.setLength(0); 661 from = "-" + nonsep_zero; 662 to = "0.0"; 663 nf.format(new BigInteger(from), formatted, fp); 664 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 665 checkFieldPosition(from, fp, 1, 2); 666 667 /* ------------------------------------------------------------------ */ 668 669 ((DecimalFormat)nf).applyPattern("0.###E0"); 670 setDigits(nf, 1, 1, Integer.MAX_VALUE, 0); 671 672 // From: 10123...789 673 // To : 1.0123...789E360 674 // ~~~ : FieldPosition(EXPONENT) 675 fp = new FieldPosition(NumberFormat.Field.EXPONENT); 676 formatted.setLength(0); 677 from = "1" + fra; 678 to = "1." + fra + "E360"; 679 nf.format(new BigInteger(from), formatted, fp); 680 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 681 checkFieldPosition(from, fp, 363, 366); 682 683 /* ------------------------------------------------------------------ */ 684 685 // From: -1012...789 686 // To : -1.012...789E360 687 formatted.setLength(0); 688 from = "-1" + fra; 689 to = "-1." + fra + "E360"; 690 nf.format(new BigInteger(from), formatted, new FieldPosition(0)); 691 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 692 693 /* ------------------------------------------------------------------ */ 694 695 ((DecimalFormat)nf).applyPattern("00000.###E0"); 696 setDigits(nf, 5, 5, Integer.MAX_VALUE, 720); 697 698 // From: 12345012...789000...000 699 // To : 12345.012...789000...000E720 700 // ~~~ : FieldPosition(EXPONENT) 701 fp = new FieldPosition(NumberFormat.Field.EXPONENT); 702 formatted.setLength(0); 703 from = "12345" + fra + nonsep_zero; 704 to = "12345." + fra + nonsep_zero + "E720"; 705 nf.format(new BigInteger(from), formatted, fp); 706 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 707 checkFieldPosition(from, fp, 727, 730); 708 709 /* ------------------------------------------------------------------ */ 710 711 ((DecimalFormat)nf).applyPattern("00000.###E0"); 712 setDigits(nf, 5, 5, Integer.MAX_VALUE, 365); 713 714 // From: -1234567890012...789000...000 715 // To : -12345.67890012...789E365 716 formatted.setLength(0); 717 from = "-1234567890" + fra; 718 to = "-12345.67890" + fra + "E365"; 719 nf.format(new BigInteger(from), formatted, new FieldPosition(0)); 720 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 721 } 722 723 /** 724 * Test for normal big numbers which don't have the fraction part with 725 * multiplier 726 */ test_Format_in_NumberFormat_BigInteger_usingMultiplier()727 void test_Format_in_NumberFormat_BigInteger_usingMultiplier() { 728 String from, to; 729 730 NumberFormat nf = NumberFormat.getInstance(Locale.US); 731 732 ((DecimalFormat)nf).applyPattern("#,##0.###"); 733 ((DecimalFormat)nf).setMultiplier(250000000); 734 setDigits(nf, Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 0); 735 736 // From: 1000...0000 737 // To: 250,0...0,000 738 formatted.setLength(0); 739 from = "1" + nonsep_zero; 740 to = "250,000,000," + sep_zero; 741 nf.format(new BigInteger(from), formatted, new FieldPosition(0)); 742 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 743 744 /* ------------------------------------------------------------------ */ 745 746 // From: -1000...0000 747 // To: -250,0...0,000 748 formatted.setLength(0); 749 from = "-1" + nonsep_zero; 750 to = "-250,000,000," + sep_zero; 751 nf.format(new BigInteger(from), formatted, new FieldPosition(0)); 752 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 753 /* ------------------------------------------------------------------ */ 754 755 ((DecimalFormat)nf).applyPattern("#,##0.###"); 756 ((DecimalFormat)nf).setMultiplier(-250000000); 757 setDigits(nf, Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 0); 758 759 // From: 1000...0000 760 // To: -250,0...0,000 761 formatted.setLength(0); 762 from = "1" + nonsep_zero; 763 to = "-250,000,000," + sep_zero; 764 nf.format(new BigInteger(from), formatted, new FieldPosition(0)); 765 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 766 767 /* ------------------------------------------------------------------ */ 768 769 // From: -1000...0000 770 // To: 250,0...0,000 771 formatted.setLength(0); 772 from = "-1" + nonsep_zero; 773 to = "250,000,000," + sep_zero; 774 nf.format(new BigInteger(from), formatted, new FieldPosition(0)); 775 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 776 } 777 778 /** 779 * Test for normal Long numbers when maximum and minimum digits are 780 * specified 781 */ test_Format_in_NumberFormat_Long_checkDigits()782 void test_Format_in_NumberFormat_Long_checkDigits() { 783 String from, to; 784 785 NumberFormat nf = NumberFormat.getInstance(Locale.US); 786 if (!(nf instanceof DecimalFormat)) { 787 throw new RuntimeException("Couldn't get DecimalFormat instance."); 788 } 789 790 ((DecimalFormat)nf).applyPattern("#,##0.###"); 791 setDigits(nf, Integer.MAX_VALUE, 360, Integer.MAX_VALUE, 0); 792 793 // From: 1234567890 794 // To: 000,0...0,000,123,456,789 795 // ------------- 796 // 300 zeros 797 formatted.setLength(0); 798 from = "123456789"; 799 to = sep_zero.substring(0, 399) + ",123,456,789"; 800 nf.format(123456789L, formatted, new FieldPosition(0)); 801 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 802 803 /* ------------------------------------------------------------------ */ 804 805 ((DecimalFormat)nf).applyPattern("##0.###"); 806 ((DecimalFormat)nf).setMultiplier(-1); 807 setDigits(nf, Integer.MAX_VALUE, 360, Integer.MAX_VALUE, 360); 808 809 // From: 1234567890 810 // To: -0000...0000123456789.000...000 811 // ------------- 812 // 300 zeros 813 formatted.setLength(0); 814 from = "123456789"; 815 to = "-" + nonsep_zero.substring(0, 300) + "123456789." + 816 nonsep_zero.substring(0, 340); 817 nf.format(123456789L, formatted, new FieldPosition(0)); 818 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 819 820 /* ------------------------------------------------------------------ */ 821 822 ((DecimalFormat)nf).applyPattern("#,##0.###"); 823 ((DecimalFormat)nf).setMultiplier(Integer.MAX_VALUE); 824 setDigits(nf, Integer.MAX_VALUE, 360, Integer.MAX_VALUE, 0); 825 826 // From: Long.MAX_VALUE 827 // To: 000,0...0,000,019,807,040,619,342,712,359,383,728,129 828 // --------------- 829 // 280 zeros 830 formatted.setLength(0); 831 from = Long.toString(Long.MAX_VALUE); 832 to = sep_zero.substring(0, 373) + 833 "19,807,040,619,342,712,359,383,728,129"; 834 nf.format(Long.MAX_VALUE, formatted, new FieldPosition(0)); 835 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 836 837 /* ------------------------------------------------------------------ */ 838 839 ((DecimalFormat)nf).applyPattern("0.###E0"); 840 ((DecimalFormat)nf).setMultiplier(Integer.MIN_VALUE); 841 setDigits(nf, 1, 1, Integer.MAX_VALUE, 360); 842 843 // From: Long.MAX_VALUE 844 // To: -1.9807040628566084396238503936000...000E28 845 // --------- 846 // 312 zeros 847 formatted.setLength(0); 848 from = Long.toString(Long.MAX_VALUE); 849 to = "-1.9807040628566084396238503936" + 850 nonsep_zero.substring(0, 312) + "E28"; 851 nf.format(Long.MAX_VALUE, formatted, new FieldPosition(0)); 852 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 853 854 /* ------------------------------------------------------------------ */ 855 856 ((DecimalFormat)nf).applyPattern("##0.###E0"); 857 ((DecimalFormat)nf).setMultiplier(Integer.MAX_VALUE); 858 setDigits(nf, Integer.MAX_VALUE, 360, Integer.MAX_VALUE, 360); 859 860 // From: Long.MIN_VALUE 861 // To: -198070406193427123615312117760000...0000.000...000E-280 862 // ----------- --------- 863 // 280 zeros 340 zeros 864 formatted.setLength(0); 865 from = Long.toString(Long.MIN_VALUE); 866 to = "-19807040619342712361531211776" + 867 nonsep_zero.substring(0, 280) + "." + 868 nonsep_zero.substring(0, 340) + "E-280"; 869 nf.format(Long.MIN_VALUE, formatted, new FieldPosition(0)); 870 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 871 872 /* ------------------------------------------------------------------ */ 873 874 ((DecimalFormat)nf).applyPattern("#,##0.###"); 875 ((DecimalFormat)nf).setMultiplier(Integer.MIN_VALUE); 876 setDigits(nf, Integer.MAX_VALUE, 360, Integer.MAX_VALUE, 360); 877 878 // From: Long.MIN_VALUE 879 // To: 000,0...0,000,019,807,040,628,566,084,398,385,987,584.000...000 880 // --------------- --------- 881 // 280 zeros 340 zeros 882 formatted.setLength(0); 883 from = Long.toString(Long.MIN_VALUE); 884 to = sep_zero.substring(0, 373) + 885 "19,807,040,628,566,084,398,385,987,584." + 886 nonsep_zero.substring(0, 340); 887 nf.format(Long.MIN_VALUE, formatted, new FieldPosition(0)); 888 checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier()); 889 } 890 891 /** 892 * Test for special numbers 893 * Double.NaN 894 * Double.POSITIVE_INFINITY 895 * Double.NEGATIVE_INFINITY 896 */ test_Format_in_NumberFormat_SpecialNumber()897 void test_Format_in_NumberFormat_SpecialNumber() { 898 String from, to; 899 900 NumberFormat nf = NumberFormat.getInstance(Locale.US); 901 if (!(nf instanceof DecimalFormat)) { 902 throw new RuntimeException("Couldn't get DecimalFormat instance."); 903 } 904 905 ((DecimalFormat)nf).applyPattern("#,##0.###"); 906 setDigits(nf, Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 0); 907 908 double[] numbers = { 909 -0.0, 0.0, Double.NaN, 910 Double.POSITIVE_INFINITY, 5.1, 5.0, 911 Double.NEGATIVE_INFINITY, -5.1, -5.0, 912 }; 913 int multipliers[] = {0, 5, -5}; 914 String[][] expected = { 915 {"-0", "0", "\ufffd", "\ufffd", "0", "0", "\ufffd", "-0", "-0"}, 916 {"-0", "0", "\ufffd", "\u221e", "25.5", "25", "-\u221e", "-25.5", 917 "-25"}, 918 {"0", "-0", "\ufffd", "-\u221e", "-25.5", "-25", "\u221e", "25.5", 919 "25"}, 920 }; 921 922 for (int i = 0; i < multipliers.length; i++) { 923 ((DecimalFormat)nf).setMultiplier(multipliers[i]); 924 for (int j = 0; j < numbers.length; j++) { 925 formatted.setLength(0); 926 from = String.valueOf(numbers[j]); 927 nf.format(numbers[j], formatted, new FieldPosition(0)); 928 checkFormat(from, formatted, expected[i][j], 929 ((DecimalFormat)nf).getMultiplier()); 930 } 931 } 932 } 933 934 /** 935 * Test for Long.MIN_VALUE 936 * (Formatting Long.MIN_VALUE w/ multiplier=-1 used to return a wrong 937 * number.) 938 */ test_Format_in_NumberFormat_Other()939 void test_Format_in_NumberFormat_Other() { 940 String from, to; 941 942 NumberFormat nf = NumberFormat.getInstance(Locale.US); 943 if (!(nf instanceof DecimalFormat)) { 944 throw new RuntimeException("Couldn't get DecimalFormat instance."); 945 } 946 947 long[] numbers = { 948 Long.MIN_VALUE, 949 }; 950 int multipliers[] = {1, -1}; 951 String[][] expected = { 952 {"-9,223,372,036,854,775,808"}, // Long.MIN_VALUE 953 {"9,223,372,036,854,775,808"}, // Long.MIN_VALUE * (-1) 954 }; 955 956 for (int i = 0; i < multipliers.length; i++) { 957 ((DecimalFormat)nf).setMultiplier(multipliers[i]); 958 for (int j = 0; j < numbers.length; j++) { 959 formatted.setLength(0); 960 from = String.valueOf(numbers[j]); 961 nf.format(numbers[j], formatted, new FieldPosition(0)); 962 checkFormat(from, formatted, expected[i][j], 963 ((DecimalFormat)nf).getMultiplier()); 964 } 965 } 966 } 967 968 /** 969 * Test for MessageFormat 970 */ test_Format_in_MessageFormat()971 void test_Format_in_MessageFormat() { 972 MessageFormat mf = new MessageFormat( 973 " {0, number}\n" + 974 " {0, number, integer}\n" + 975 " {0, number, currency}\n" + 976 " {0, number, percent}\n" + 977 " {0, number,0.###########E0}\n" + 978 979 " {1, number}\n" + 980 " {1, number, integer}\n" + 981 " {1, number, currency}\n" + 982 " {1, number, percent}\n" + 983 " {1, number,0.#######E0}\n", 984 Locale.US 985 ); 986 Object[] testArgs = { 987 new BigInteger("9876543210987654321098765432109876543210"), 988 new BigDecimal("-12345678901234567890.98765432109876543210987654321"), 989 }; 990 String expected = 991 " 9,876,543,210,987,654,321,098,765,432,109,876,543,210\n" + 992 " 9,876,543,210,987,654,321,098,765,432,109,876,543,210\n" + 993 " $9,876,543,210,987,654,321,098,765,432,109,876,543,210.00\n" + 994 " 987,654,321,098,765,432,109,876,543,210,987,654,321,000%\n" + 995 " 9.87654321099E39\n" + 996 997 " -12,345,678,901,234,567,890.988\n" + 998 " -12,345,678,901,234,567,891\n" + 999 " ($12,345,678,901,234,567,890.99)\n" + 1000 " -1,234,567,890,123,456,789,099%\n" + 1001 " -1.2345679E19\n" 1002 ; 1003 1004 if (!expected.equals(mf.format(testArgs))) { 1005 errln("Wrong format.\n got:\n" + mf.format(testArgs) + 1006 " expected:\n" + expected); 1007 } 1008 } 1009 setDigits(NumberFormat nf, int i_max, int i_min, int f_max, int f_min)1010 private void setDigits(NumberFormat nf, 1011 int i_max, int i_min, int f_max, int f_min) { 1012 nf.setMaximumIntegerDigits(i_max); 1013 nf.setMinimumIntegerDigits(i_min); 1014 nf.setMaximumFractionDigits(f_max); 1015 nf.setMinimumFractionDigits(f_min); 1016 } 1017 checkFormat(String orig, StringBuffer got, String expected, int multiplier)1018 private void checkFormat(String orig, StringBuffer got, String expected, 1019 int multiplier) { 1020 if (!expected.equals(new String(got))) { 1021 errln("Formatting... failed." + 1022 "\n original: " + orig + 1023 "\n multiplier: " + multiplier + 1024 "\n formatted: " + got + 1025 "\n expected: " + expected + "\n"); 1026 } 1027 } 1028 checkFieldPosition(String orig, FieldPosition fp, int begin, int end)1029 private void checkFieldPosition(String orig, FieldPosition fp, int begin, 1030 int end) { 1031 int position; 1032 1033 if ((position = fp.getBeginIndex()) != begin) { 1034 errln("Formatting... wrong Begin index returned for " + 1035 fp.getFieldAttribute() + "." + 1036 "\n original: " + orig + 1037 "\n got: " + position + 1038 "\n expected: " + begin + "\n"); 1039 } 1040 if ((position = fp.getEndIndex()) != end) { 1041 errln("Formatting... wrong End index returned for " + 1042 fp.getFieldAttribute() + "." + 1043 "\n original: " + orig + 1044 "\n got: " + position + 1045 "\n expected: " + end + "\n"); 1046 } 1047 } 1048 } 1049