Class FormattedNumber


  • public class FormattedNumber
    extends java.lang.Object
    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.
    See Also:
    NumberFormatter
    • Method Detail

      • toString

        public java.lang.String toString()
        Creates a String representation of the the formatted number.
        Overrides:
        toString in class java.lang.Object
        Returns:
        a String containing the localized number.
        See Also:
        NumberFormatter
      • appendTo

        public <A extends java.lang.Appendable> A appendTo​(A appendable)
        Append the formatted number to an Appendable, such as a StringBuilder. This may be slightly more efficient than creating a String.

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

        Parameters:
        appendable - The Appendable to which to append the formatted number string.
        Returns:
        The same Appendable, for chaining.
        See Also:
        Appendable, NumberFormatter
      • populateFieldPosition

        @Deprecated
        public void populateFieldPosition​(java.text.FieldPosition fieldPosition)
        Deprecated.
        ICU 62 Use nextFieldPosition(java.text.FieldPosition) instead. This method will be removed in a future release. See http://bugs.icu-project.org/trac/ticket/13746
        Determine the start and end indices of the first occurrence of the given field in the output string. This allows you to determine the locations of the integer part, fraction part, and sign.

        If multiple different field attributes are needed, this method can be called repeatedly, or if all field attributes are needed, consider using getFieldIterator().

        If a field occurs multiple times in an output string, such as a grouping separator, this method will only ever return the first occurrence. Use getFieldIterator() to access all occurrences of an attribute.

        Parameters:
        fieldPosition - The FieldPosition to populate with the start and end indices of the desired field.
        See Also:
        NumberFormat.Field, NumberFormatter
      • nextFieldPosition

        public boolean nextFieldPosition​(java.text.FieldPosition fieldPosition)
        Determines the start and end 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.

        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 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
      • getFieldIterator

        @Deprecated
        public java.text.AttributedCharacterIterator getFieldIterator()
        Deprecated.
        ICU 62 Use toCharacterIterator() instead. This method will be removed in a future release. See http://bugs.icu-project.org/trac/ticket/13746
        Export the formatted number as an AttributedCharacterIterator. This allows you to determine which characters in the output string correspond to which fields, such as the integer part, fraction part, and sign.

        If information on only one field is needed, consider using populateFieldPosition() instead.

        Returns:
        An AttributedCharacterIterator, containing information on the field attributes of the number string.
        See Also:
        NumberFormat.Field, AttributedCharacterIterator, NumberFormatter
      • toCharacterIterator

        public java.text.AttributedCharacterIterator toCharacterIterator()
        Export the formatted number as an AttributedCharacterIterator. This allows you to determine which characters in the output string correspond to which fields, such as the integer part, fraction part, and sign.

        If information on only one field is needed, use nextFieldPosition(FieldPosition) instead.

        Returns:
        An AttributedCharacterIterator, containing information on the field attributes of the number string.
        See Also:
        NumberFormat.Field, AttributedCharacterIterator, 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