Skip navigation links
Java™ Platform
Standard Ed. 8
compact1, compact2, compact3
java.text

Class ChoiceFormat

    • Constructor Detail

      • ChoiceFormat

        public ChoiceFormat(String newPattern)
        Constructs with limits and corresponding formats based on the pattern.
        Parameters:
        newPattern - the new pattern string
        See Also:
        applyPattern(java.lang.String)
      • ChoiceFormat

        public ChoiceFormat(double[] limits,
                            String[] formats)
        Constructs with the limits and the corresponding formats.
        Parameters:
        limits - limits in ascending order
        formats - corresponding format strings
        See Also:
        setChoices(double[], java.lang.String[])
    • Method Detail

      • applyPattern

        public void applyPattern(String newPattern)
        Sets the pattern.
        Parameters:
        newPattern - See the class description.
      • toPattern

        public String toPattern()
        Gets the pattern.
        Returns:
        the pattern string
      • setChoices

        public void setChoices(double[] limits,
                               String[] formats)
        Set the choices to be used in formatting.
        Parameters:
        limits - contains the top value that you want parsed with that format, and should be in ascending sorted order. When formatting X, the choice will be the i, where limit[i] ≤ X < limit[i+1]. If the limit array is not in ascending order, the results of formatting will be incorrect.
        formats - are the formats you want to use for each limit. They can be either Format objects or Strings. When formatting with object Y, if the object is a NumberFormat, then ((NumberFormat) Y).format(X) is called. Otherwise Y.toString() is called.
      • getLimits

        public double[] getLimits()
        Get the limits passed in the constructor.
        Returns:
        the limits.
      • getFormats

        public Object[] getFormats()
        Get the formats passed in the constructor.
        Returns:
        the formats.
      • format

        public StringBuffer format(long number,
                                   StringBuffer toAppendTo,
                                   FieldPosition status)
        Specialization of format. This method really calls format(double, StringBuffer, FieldPosition) thus the range of longs that are supported is only equal to the range that can be stored by double. This will never be a practical limitation.
        Specified by:
        format in class NumberFormat
        Parameters:
        number - the long number to format
        toAppendTo - the StringBuffer to which the formatted text is to be appended
        status - the field position
        Returns:
        the formatted StringBuffer
        See Also:
        Format.format(java.lang.Object)
      • parse

        public Number parse(String text,
                            ParsePosition status)
        Parses a Number from the input text.
        Specified by:
        parse in class NumberFormat
        Parameters:
        text - the source text.
        status - an input-output parameter. On input, the status.index field indicates the first character of the source text that should be parsed. On exit, if no error occurred, status.index is set to the first unparsed character in the source text. On exit, if an error did occur, status.index is unchanged and status.errorIndex is set to the first index of the character that caused the parse to fail.
        Returns:
        A Number representing the value of the number parsed.
        See Also:
        NumberFormat.isParseIntegerOnly(), Format.parseObject(java.lang.String, java.text.ParsePosition)
      • nextDouble

        public static final double nextDouble(double d)
        Finds the least double greater than d. If NaN, returns same value.

        Used to make half-open intervals.

        Parameters:
        d - the reference value
        Returns:
        the least double value greather than d
        See Also:
        previousDouble(double)
      • previousDouble

        public static final double previousDouble(double d)
        Finds the greatest double less than d. If NaN, returns same value.
        Parameters:
        d - the reference value
        Returns:
        the greatest double value less than d
        See Also:
        nextDouble(double)
      • equals

        public boolean equals(Object obj)
        Equality comparision between two
        Overrides:
        equals in class NumberFormat
        Parameters:
        obj - the reference object with which to compare.
        Returns:
        true if this object is the same as the obj argument; false otherwise.
        See Also:
        Object.hashCode(), HashMap
      • nextDouble

        public static double nextDouble(double d,
                                        boolean positive)
        Finds the least double greater than d (if positive is true), or the greatest double less than d (if positive is false). If NaN, returns same value. Does not affect floating-point flags, provided these member functions do not: Double.longBitsToDouble(long) Double.doubleToLongBits(double) Double.isNaN(double)
        Parameters:
        d - the reference value
        positive - true if the least double is desired; false otherwise
        Returns:
        the least or greater double value
Java™ Platform
Standard Ed. 8

Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2025, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.