463 * @see java.text.NumberFormat#getPercentInstance 464 * @see java.text.DecimalFormatSymbols 465 */ 466 public DecimalFormat (String pattern, DecimalFormatSymbols symbols) { 467 // Always applyPattern after the symbols are set 468 this.symbols = (DecimalFormatSymbols)symbols.clone(); 469 applyPattern(pattern, false); 470 } 471 472 473 // Overrides 474 /** 475 * Formats a number and appends the resulting text to the given string 476 * buffer. 477 * The number can be of any subclass of {@link java.lang.Number}. 478 * <p> 479 * This implementation uses the maximum precision permitted. 480 * @param number the number to format 481 * @param toAppendTo the <code>StringBuffer</code> to which the formatted 482 * text is to be appended 483 * @param pos On input: an alignment field, if desired. 484 * On output: the offsets of the alignment field. 485 * @return the value passed in as <code>toAppendTo</code> 486 * @exception IllegalArgumentException if <code>number</code> is 487 * null or not an instance of <code>Number</code>. 488 * @exception NullPointerException if <code>toAppendTo</code> or 489 * <code>pos</code> is null 490 * @exception ArithmeticException if rounding is needed with rounding 491 * mode being set to RoundingMode.UNNECESSARY 492 * @see java.text.FieldPosition 493 */ 494 @Override 495 public final StringBuffer format(Object number, 496 StringBuffer toAppendTo, 497 FieldPosition pos) { 498 if (number instanceof Long || number instanceof Integer || 499 number instanceof Short || number instanceof Byte || 500 number instanceof AtomicInteger || 501 number instanceof AtomicLong || 502 (number instanceof BigInteger && 503 ((BigInteger)number).bitLength () < 64)) { 504 return format(((Number)number).longValue(), toAppendTo, pos); 505 } else if (number instanceof BigDecimal) { 506 return format((BigDecimal)number, toAppendTo, pos); 507 } else if (number instanceof BigInteger) { 508 return format((BigInteger)number, toAppendTo, pos); 509 } else if (number instanceof Number) { 510 return format(((Number)number).doubleValue(), toAppendTo, pos); 511 } else { 512 throw new IllegalArgumentException("Cannot format given Object as a Number"); 513 } 514 } 515 516 /** 517 * Formats a double to produce a string. 518 * @param number The double to format 519 * @param result where the text is to be appended 520 * @param fieldPosition On input: an alignment field, if desired. 521 * On output: the offsets of the alignment field. 522 * @exception NullPointerException if {@code result} or 523 * {@code fieldPosition} is {@code null} 524 * @exception ArithmeticException if rounding is needed with rounding 525 * mode being set to RoundingMode.UNNECESSARY 526 * @return The formatted number string 527 * @see java.text.FieldPosition 528 */ 529 @Override 530 public StringBuffer format(double number, StringBuffer result, 531 FieldPosition fieldPosition) { 532 // If fieldPosition is a DontCareFieldPosition instance we can 533 // try to go to fast-path code. 534 boolean tryFastPath = false; 535 if (fieldPosition == DontCareFieldPosition.INSTANCE) 536 tryFastPath = true; 537 else { 538 fieldPosition.setBeginIndex(0); 539 fieldPosition.setEndIndex(0); 540 } 541 620 assert(number >= 0 && !Double.isInfinite(number)); 621 622 synchronized(digitList) { 623 int maxIntDigits = super.getMaximumIntegerDigits(); 624 int minIntDigits = super.getMinimumIntegerDigits(); 625 int maxFraDigits = super.getMaximumFractionDigits(); 626 int minFraDigits = super.getMinimumFractionDigits(); 627 628 digitList.set(isNegative, number, useExponentialNotation ? 629 maxIntDigits + maxFraDigits : maxFraDigits, 630 !useExponentialNotation); 631 return subformat(result, delegate, isNegative, false, 632 maxIntDigits, minIntDigits, maxFraDigits, minFraDigits); 633 } 634 } 635 636 /** 637 * Format a long to produce a string. 638 * @param number The long to format 639 * @param result where the text is to be appended 640 * @param fieldPosition On input: an alignment field, if desired. 641 * On output: the offsets of the alignment field. 642 * @exception NullPointerException if {@code result} or 643 * {@code fieldPosition} is {@code null} 644 * @exception ArithmeticException if rounding is needed with rounding 645 * mode being set to RoundingMode.UNNECESSARY 646 * @return The formatted number string 647 * @see java.text.FieldPosition 648 */ 649 @Override 650 public StringBuffer format(long number, StringBuffer result, 651 FieldPosition fieldPosition) { 652 fieldPosition.setBeginIndex(0); 653 fieldPosition.setEndIndex(0); 654 655 return format(number, result, fieldPosition.getFieldDelegate()); 656 } 657 658 /** 659 * Format a long to produce a string. 660 * @param number The long to format 661 * @param result where the text is to be appended 710 } 711 712 synchronized(digitList) { 713 int maxIntDigits = super.getMaximumIntegerDigits(); 714 int minIntDigits = super.getMinimumIntegerDigits(); 715 int maxFraDigits = super.getMaximumFractionDigits(); 716 int minFraDigits = super.getMinimumFractionDigits(); 717 718 digitList.set(isNegative, number, 719 useExponentialNotation ? maxIntDigits + maxFraDigits : 0); 720 721 return subformat(result, delegate, isNegative, true, 722 maxIntDigits, minIntDigits, maxFraDigits, minFraDigits); 723 } 724 } 725 726 /** 727 * Formats a BigDecimal to produce a string. 728 * @param number The BigDecimal to format 729 * @param result where the text is to be appended 730 * @param fieldPosition On input: an alignment field, if desired. 731 * On output: the offsets of the alignment field. 732 * @return The formatted number string 733 * @exception ArithmeticException if rounding is needed with rounding 734 * mode being set to RoundingMode.UNNECESSARY 735 * @see java.text.FieldPosition 736 */ 737 private StringBuffer format(BigDecimal number, StringBuffer result, 738 FieldPosition fieldPosition) { 739 fieldPosition.setBeginIndex(0); 740 fieldPosition.setEndIndex(0); 741 return format(number, result, fieldPosition.getFieldDelegate()); 742 } 743 744 /** 745 * Formats a BigDecimal to produce a string. 746 * @param number The BigDecimal to format 747 * @param result where the text is to be appended 748 * @param delegate notified of locations of sub fields 749 * @exception ArithmeticException if rounding is needed with rounding 750 * mode being set to RoundingMode.UNNECESSARY 751 * @return The formatted number string 763 synchronized(digitList) { 764 int maxIntDigits = getMaximumIntegerDigits(); 765 int minIntDigits = getMinimumIntegerDigits(); 766 int maxFraDigits = getMaximumFractionDigits(); 767 int minFraDigits = getMinimumFractionDigits(); 768 int maximumDigits = maxIntDigits + maxFraDigits; 769 770 digitList.set(isNegative, number, useExponentialNotation ? 771 ((maximumDigits < 0) ? Integer.MAX_VALUE : maximumDigits) : 772 maxFraDigits, !useExponentialNotation); 773 774 return subformat(result, delegate, isNegative, false, 775 maxIntDigits, minIntDigits, maxFraDigits, minFraDigits); 776 } 777 } 778 779 /** 780 * Format a BigInteger to produce a string. 781 * @param number The BigInteger to format 782 * @param result where the text is to be appended 783 * @param fieldPosition On input: an alignment field, if desired. 784 * On output: the offsets of the alignment field. 785 * @return The formatted number string 786 * @exception ArithmeticException if rounding is needed with rounding 787 * mode being set to RoundingMode.UNNECESSARY 788 * @see java.text.FieldPosition 789 */ 790 private StringBuffer format(BigInteger number, StringBuffer result, 791 FieldPosition fieldPosition) { 792 fieldPosition.setBeginIndex(0); 793 fieldPosition.setEndIndex(0); 794 795 return format(number, result, fieldPosition.getFieldDelegate(), false); 796 } 797 798 /** 799 * Format a BigInteger to produce a string. 800 * @param number The BigInteger to format 801 * @param result where the text is to be appended 802 * @param delegate notified of locations of sub fields 803 * @return The formatted number string 804 * @exception ArithmeticException if rounding is needed with rounding | 463 * @see java.text.NumberFormat#getPercentInstance 464 * @see java.text.DecimalFormatSymbols 465 */ 466 public DecimalFormat (String pattern, DecimalFormatSymbols symbols) { 467 // Always applyPattern after the symbols are set 468 this.symbols = (DecimalFormatSymbols)symbols.clone(); 469 applyPattern(pattern, false); 470 } 471 472 473 // Overrides 474 /** 475 * Formats a number and appends the resulting text to the given string 476 * buffer. 477 * The number can be of any subclass of {@link java.lang.Number}. 478 * <p> 479 * This implementation uses the maximum precision permitted. 480 * @param number the number to format 481 * @param toAppendTo the <code>StringBuffer</code> to which the formatted 482 * text is to be appended 483 * @param pos keeps track on the position of the field within the 484 * returned string. For example, for formatting a number 485 * {@code 1234567.89} in {@code Locale.US} locale, 486 * if the given {@code fieldPosition} is 487 * {@link NumberFormat#INTEGER_FIELD}, the begin index 488 * and end index of {@code fieldPosition} will be set 489 * to 0 and 9, respectively for the output string 490 * {@code 1,234,567.89}. 491 * @return the value passed in as <code>toAppendTo</code> 492 * @exception IllegalArgumentException if <code>number</code> is 493 * null or not an instance of <code>Number</code>. 494 * @exception NullPointerException if <code>toAppendTo</code> or 495 * <code>pos</code> is null 496 * @exception ArithmeticException if rounding is needed with rounding 497 * mode being set to RoundingMode.UNNECESSARY 498 * @see java.text.FieldPosition 499 */ 500 @Override 501 public final StringBuffer format(Object number, 502 StringBuffer toAppendTo, 503 FieldPosition pos) { 504 if (number instanceof Long || number instanceof Integer || 505 number instanceof Short || number instanceof Byte || 506 number instanceof AtomicInteger || 507 number instanceof AtomicLong || 508 (number instanceof BigInteger && 509 ((BigInteger)number).bitLength () < 64)) { 510 return format(((Number)number).longValue(), toAppendTo, pos); 511 } else if (number instanceof BigDecimal) { 512 return format((BigDecimal)number, toAppendTo, pos); 513 } else if (number instanceof BigInteger) { 514 return format((BigInteger)number, toAppendTo, pos); 515 } else if (number instanceof Number) { 516 return format(((Number)number).doubleValue(), toAppendTo, pos); 517 } else { 518 throw new IllegalArgumentException("Cannot format given Object as a Number"); 519 } 520 } 521 522 /** 523 * Formats a double to produce a string. 524 * @param number The double to format 525 * @param result where the text is to be appended 526 * @param fieldPosition keeps track on the position of the field within 527 * the returned string. For example, for formatting 528 * a number {@code 1234567.89} in {@code Locale.US} 529 * locale, if the given {@code fieldPosition} is 530 * {@link NumberFormat#INTEGER_FIELD}, the begin index 531 * and end index of {@code fieldPosition} will be set 532 * to 0 and 9, respectively for the output string 533 * {@code 1,234,567.89}. 534 * @exception NullPointerException if {@code result} or 535 * {@code fieldPosition} is {@code null} 536 * @exception ArithmeticException if rounding is needed with rounding 537 * mode being set to RoundingMode.UNNECESSARY 538 * @return The formatted number string 539 * @see java.text.FieldPosition 540 */ 541 @Override 542 public StringBuffer format(double number, StringBuffer result, 543 FieldPosition fieldPosition) { 544 // If fieldPosition is a DontCareFieldPosition instance we can 545 // try to go to fast-path code. 546 boolean tryFastPath = false; 547 if (fieldPosition == DontCareFieldPosition.INSTANCE) 548 tryFastPath = true; 549 else { 550 fieldPosition.setBeginIndex(0); 551 fieldPosition.setEndIndex(0); 552 } 553 632 assert(number >= 0 && !Double.isInfinite(number)); 633 634 synchronized(digitList) { 635 int maxIntDigits = super.getMaximumIntegerDigits(); 636 int minIntDigits = super.getMinimumIntegerDigits(); 637 int maxFraDigits = super.getMaximumFractionDigits(); 638 int minFraDigits = super.getMinimumFractionDigits(); 639 640 digitList.set(isNegative, number, useExponentialNotation ? 641 maxIntDigits + maxFraDigits : maxFraDigits, 642 !useExponentialNotation); 643 return subformat(result, delegate, isNegative, false, 644 maxIntDigits, minIntDigits, maxFraDigits, minFraDigits); 645 } 646 } 647 648 /** 649 * Format a long to produce a string. 650 * @param number The long to format 651 * @param result where the text is to be appended 652 * @param fieldPosition keeps track on the position of the field within 653 * the returned string. For example, for formatting 654 * a number {@code 123456789} in {@code Locale.US} 655 * locale, if the given {@code fieldPosition} is 656 * {@link NumberFormat#INTEGER_FIELD}, the begin index 657 * and end index of {@code fieldPosition} will be set 658 * to 0 and 11, respectively for the output string 659 * {@code 123,456,789}. 660 * @exception NullPointerException if {@code result} or 661 * {@code fieldPosition} is {@code null} 662 * @exception ArithmeticException if rounding is needed with rounding 663 * mode being set to RoundingMode.UNNECESSARY 664 * @return The formatted number string 665 * @see java.text.FieldPosition 666 */ 667 @Override 668 public StringBuffer format(long number, StringBuffer result, 669 FieldPosition fieldPosition) { 670 fieldPosition.setBeginIndex(0); 671 fieldPosition.setEndIndex(0); 672 673 return format(number, result, fieldPosition.getFieldDelegate()); 674 } 675 676 /** 677 * Format a long to produce a string. 678 * @param number The long to format 679 * @param result where the text is to be appended 728 } 729 730 synchronized(digitList) { 731 int maxIntDigits = super.getMaximumIntegerDigits(); 732 int minIntDigits = super.getMinimumIntegerDigits(); 733 int maxFraDigits = super.getMaximumFractionDigits(); 734 int minFraDigits = super.getMinimumFractionDigits(); 735 736 digitList.set(isNegative, number, 737 useExponentialNotation ? maxIntDigits + maxFraDigits : 0); 738 739 return subformat(result, delegate, isNegative, true, 740 maxIntDigits, minIntDigits, maxFraDigits, minFraDigits); 741 } 742 } 743 744 /** 745 * Formats a BigDecimal to produce a string. 746 * @param number The BigDecimal to format 747 * @param result where the text is to be appended 748 * @param fieldPosition keeps track on the position of the field within 749 * the returned string. For example, for formatting 750 * a number {@code 1234567.89} in {@code Locale.US} 751 * locale, if the given {@code fieldPosition} is 752 * {@link NumberFormat#INTEGER_FIELD}, the begin index 753 * and end index of {@code fieldPosition} will be set 754 * to 0 and 9, respectively for the output string 755 * {@code 1,234,567.89}. 756 * @return The formatted number string 757 * @exception ArithmeticException if rounding is needed with rounding 758 * mode being set to RoundingMode.UNNECESSARY 759 * @see java.text.FieldPosition 760 */ 761 private StringBuffer format(BigDecimal number, StringBuffer result, 762 FieldPosition fieldPosition) { 763 fieldPosition.setBeginIndex(0); 764 fieldPosition.setEndIndex(0); 765 return format(number, result, fieldPosition.getFieldDelegate()); 766 } 767 768 /** 769 * Formats a BigDecimal to produce a string. 770 * @param number The BigDecimal to format 771 * @param result where the text is to be appended 772 * @param delegate notified of locations of sub fields 773 * @exception ArithmeticException if rounding is needed with rounding 774 * mode being set to RoundingMode.UNNECESSARY 775 * @return The formatted number string 787 synchronized(digitList) { 788 int maxIntDigits = getMaximumIntegerDigits(); 789 int minIntDigits = getMinimumIntegerDigits(); 790 int maxFraDigits = getMaximumFractionDigits(); 791 int minFraDigits = getMinimumFractionDigits(); 792 int maximumDigits = maxIntDigits + maxFraDigits; 793 794 digitList.set(isNegative, number, useExponentialNotation ? 795 ((maximumDigits < 0) ? Integer.MAX_VALUE : maximumDigits) : 796 maxFraDigits, !useExponentialNotation); 797 798 return subformat(result, delegate, isNegative, false, 799 maxIntDigits, minIntDigits, maxFraDigits, minFraDigits); 800 } 801 } 802 803 /** 804 * Format a BigInteger to produce a string. 805 * @param number The BigInteger to format 806 * @param result where the text is to be appended 807 * @param fieldPosition keeps track on the position of the field within 808 * the returned string. For example, for formatting 809 * a number {@code 123456789} in {@code Locale.US} 810 * locale, if the given {@code fieldPosition} is 811 * {@link NumberFormat#INTEGER_FIELD}, the begin index 812 * and end index of {@code fieldPosition} will be set 813 * to 0 and 11, respectively for the output string 814 * {@code 123,456,789}. 815 * @return The formatted number string 816 * @exception ArithmeticException if rounding is needed with rounding 817 * mode being set to RoundingMode.UNNECESSARY 818 * @see java.text.FieldPosition 819 */ 820 private StringBuffer format(BigInteger number, StringBuffer result, 821 FieldPosition fieldPosition) { 822 fieldPosition.setBeginIndex(0); 823 fieldPosition.setEndIndex(0); 824 825 return format(number, result, fieldPosition.getFieldDelegate(), false); 826 } 827 828 /** 829 * Format a BigInteger to produce a string. 830 * @param number The BigInteger to format 831 * @param result where the text is to be appended 832 * @param delegate notified of locations of sub fields 833 * @return The formatted number string 834 * @exception ArithmeticException if rounding is needed with rounding |