Class FormattedNumber

  • All Implemented Interfaces:
    FormattedValue, java.lang.CharSequence

    public class FormattedNumber
    extends java.lang.Object
    implements FormattedValue
    The result of a number formatting operation. This class allows the result to be exported in several data types, including a String, an AttributedCharacterIterator, and a BigDecimal. Instances of this class are immutable and thread-safe.
    See Also:
    NumberFormatter
    • Method Summary

      All Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      <A extends java.lang.Appendable>
      A
      appendTo​(A appendable)
      Appends the formatted string to an Appendable.
      char charAt​(int index)
      boolean equals​(java.lang.Object other)
      PluralRules.IFixedDecimal getFixedDecimal()
      Deprecated.
      This API is ICU internal only.
      int hashCode()
      int length()
      boolean nextFieldPosition​(java.text.FieldPosition fieldPosition)
      Determines the start (inclusive) and end (exclusive) indices of the next occurrence of the given field in the output string.
      boolean nextPosition​(ConstrainedFieldPosition cfpos)
      Iterates over field positions in the FormattedValue.
      java.lang.CharSequence subSequence​(int start, int end)
      java.math.BigDecimal toBigDecimal()
      Export the formatted number as a BigDecimal.
      java.text.AttributedCharacterIterator toCharacterIterator()
      Exports the formatted number as an AttributedCharacterIterator.
      java.lang.String toString()
      Returns the formatted string as a Java String.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.CharSequence

        chars, codePoints
    • Method Detail

      • toString

        public java.lang.String toString()
        Returns the formatted string as a Java String. Consider using FormattedValue.appendTo(A) for greater efficiency.
        Specified by:
        toString in interface java.lang.CharSequence
        Specified by:
        toString in interface FormattedValue
        Overrides:
        toString in class java.lang.Object
        Returns:
        The formatted string.
      • length

        public int length()
        Specified by:
        length in interface java.lang.CharSequence
      • charAt

        public char charAt​(int index)
        Specified by:
        charAt in interface java.lang.CharSequence
      • subSequence

        public java.lang.CharSequence subSequence​(int start,
                                                  int end)
        Specified by:
        subSequence in interface java.lang.CharSequence
      • appendTo

        public <A extends java.lang.Appendable> A appendTo​(A appendable)
        Appends the formatted string to an Appendable.

        If an IOException occurs when appending to the Appendable, an unchecked ICUUncheckedIOException is thrown instead.

        Specified by:
        appendTo in interface FormattedValue
        Parameters:
        appendable - The Appendable to which to append the string output.
        Returns:
        The same Appendable, for chaining.
      • nextPosition

        public boolean nextPosition​(ConstrainedFieldPosition cfpos)
        Iterates over field positions in the FormattedValue. This lets you determine the position of specific types of substrings, like a month or a decimal separator. To loop over all field positions:
             ConstrainableFieldPosition cfpos = new ConstrainableFieldPosition();
             while (fmtval.nextPosition(cfpos)) {
                 // handle the field position; get information from cfpos
             }
         
        Specified by:
        nextPosition in interface FormattedValue
        Parameters:
        cfpos - The object used for iteration state. This can provide constraints to iterate over only one specific field; see ConstrainedFieldPosition.constrainField(java.text.Format.Field).
        Returns:
        true if a new occurrence of the field was found; false otherwise.
      • nextFieldPosition

        public boolean nextFieldPosition​(java.text.FieldPosition fieldPosition)
        Determines the start (inclusive) and end (exclusive) indices of the next occurrence of the given field in the output string. This allows you to determine the locations of, for example, the integer part, fraction part, or symbols.

        This is a simpler but less powerful alternative to nextPosition(com.ibm.icu.text.ConstrainedFieldPosition).

        If a field occurs just once, calling this method will find that occurrence and return it. If a field occurs multiple times, this method may be called repeatedly with the following pattern:

         FieldPosition fpos = new FieldPosition(NumberFormat.Field.GROUPING_SEPARATOR);
         while (formattedNumber.nextFieldPosition(fpos, status)) {
             // do something with fpos.
         }
         

        This method is useful if you know which field to query. If you want all available field position information, use nextPosition(com.ibm.icu.text.ConstrainedFieldPosition) or toCharacterIterator().

        Parameters:
        fieldPosition - Input+output variable. On input, the "field" property determines which field to look up, and the "beginIndex" and "endIndex" properties determine where to begin the search. On output, the "beginIndex" is set to the beginning of the first occurrence of the field with either begin or end indices after the input indices, "endIndex" is set to the end of that occurrence of the field (exclusive index). If a field position is not found, the method returns FALSE and the FieldPosition may or may not be changed.
        Returns:
        true if a new occurrence of the field was found; false otherwise.
        See Also:
        NumberFormat.Field, NumberFormatter
      • toBigDecimal

        public java.math.BigDecimal toBigDecimal()
        Export the formatted number as a BigDecimal. This endpoint is useful for obtaining the exact number being printed after scaling and rounding have been applied by the number formatting pipeline.
        Returns:
        A BigDecimal representation of the formatted number.
        See Also:
        NumberFormatter
      • getFixedDecimal

        @Deprecated
        public PluralRules.IFixedDecimal getFixedDecimal()
        Deprecated.
        This API is ICU internal only.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object other)
        Overrides:
        equals in class java.lang.Object