| java.lang.Object | |||
| ↳ | java.text.Format | ||
| ↳ | java.text.NumberFormat | ||
| ↳ | java.text.ChoiceFormat | ||
Returns a fixed string based on a numeric value. The class can be used in
conjunction with the
If there is no match, then either the first or last index is used. The first
or last index is used depending on whether the number is too low or too high.
The length of the format array must be the same as the length of the limits
array.
ChoiceFormat.nextDouble(double) allows to get the double following the one
passed to the method. This is used to create half open intervals.
where:
MessageFormat class to handle plurals in
messages. ChoiceFormat enables users to attach a format to a range of
numbers. The choice is specified with an ascending list of doubles, where
each item specifies a half-open interval up to the next item as in the
following: X matches j if and only if limit[j] <_ x="x" _="_" limitj1="limitj1" code="code">.
Examples:
double[] limits = {1, 2, 3, 4, 5, 6, 7};
String[] fmts = {"Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat"};
double[] limits2 = {0, 1, ChoiceFormat.nextDouble(1)};
String[] fmts2 = {"no files", "one file", "many files"};
ChoiceFormat objects also may be converted to and from patterns.
The conversion can be done programmatically, as in the example above, or
by using a pattern like the following:
"1#Sun|2#Mon|3#Tue|4#Wed|5#Thur|6#Fri|7#Sat"
"0#are no files|1#is one file|1<are many files"
Summary
[Expand]
Inherited Constants | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| |||||||||||
Public Constructors | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| Constructs a new
| ||||||||||
| Constructs a new
| ||||||||||
Public Methods | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| Parses the pattern to determine new strings and ranges for this
| ||||||||||
| Returns a new instance of
| ||||||||||
| Compares the specified object with this
| ||||||||||
| Appends the string associated with the range in which the specified long
value fits to the specified string buffer.
| ||||||||||
| Appends the string associated with the range in which the specified
double value fits to the specified string buffer.
| ||||||||||
| Returns the strings associated with the ranges of this
| ||||||||||
| Returns the limits of this
| ||||||||||
| Returns an integer hash code for the receiver.
| ||||||||||
| Returns the double value which is closest to the specified double but
either larger or smaller as specified.
| ||||||||||
| Returns the double value which is closest to the specified double but
larger.
| ||||||||||
| Parses a double from the specified string starting at the index specified
by
| ||||||||||
| Returns the double value which is closest to the specified double but
smaller.
| ||||||||||
| Sets the double values and associated strings of this ChoiceFormat.
| ||||||||||
| Returns the pattern of this
| ||||||||||
[Expand]
Inherited Methods | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| |||||||||||
| |||||||||||
| |||||||||||
Public Constructors
public
ChoiceFormat
(double[] limits, String[] formats)
Since: API Level 1
The length of the Constructs a new ChoiceFormat with the specified double values
and associated strings. When calling
format with a double
value d, then the element i in formats is
selected where i fulfills limits[i] <_ d="d" _="_" limitsi1="limitsi1" code="code">.
limits and formats arrays must be the
same.
Parameters
| |
|---|---|
| |
public
ChoiceFormat
(String template)
Since: API Level 1
Constructs a new ChoiceFormat with the strings and limits parsed
from the specified pattern.
Parameters
| |
|---|
Public Methods
public
void
applyPattern
(String template)
Since: API Level 1
Parses the pattern to determine new strings and ranges for this
ChoiceFormat.
Parameters
| |
|---|
public
Object
clone
()
Since: API Level 1
Returns a new instance of ChoiceFormat with the same ranges and
strings as this ChoiceFormat.
Returns
a shallow copy of this ChoiceFormat.
public
boolean
equals
(Object object)
Since: API Level 1
Compares the specified object with this ChoiceFormat. The object
must be an instance of ChoiceFormat and have the same limits and
formats to be equal to this instance.
Parameters
| |
|---|
Returns
true if the specified object is equal to this instance;
false otherwise.
public
StringBuffer
format
(long value, StringBuffer buffer, FieldPosition field)
Since: API Level 1
Appends the string associated with the range in which the specified long
value fits to the specified string buffer.
Parameters
| |
|---|---|
| |
| |
Returns
the string buffer.
public
StringBuffer
format
(double value, StringBuffer buffer, FieldPosition field)
Since: API Level 1
Appends the string associated with the range in which the specified
double value fits to the specified string buffer.
Parameters
| |
|---|---|
| |
| |
Returns
the string buffer.
public
Object[]
getFormats
()
Since: API Level 1
Returns the strings associated with the ranges of this ChoiceFormat.
Returns
an array of format strings.
public
double[]
getLimits
()
Since: API Level 1
Returns the limits of this ChoiceFormat.
Returns
the array of doubles which make up the limits of this ChoiceFormat.
public
int
hashCode
()
Since: API Level 1
Returns an integer hash code for the receiver. Objects which are equal
return the same value for this method.
Returns
the receiver's hash.
public
static
double
nextDouble
(double value, boolean increment)
Since: API Level 1
Returns the double value which is closest to the specified double but
either larger or smaller as specified.
Parameters
| |
|---|---|
| |
Returns
the next larger or smaller double value.
public
static
final
double
nextDouble
(double value)
Since: API Level 1
Returns the double value which is closest to the specified double but
larger.
Parameters
| |
|---|
Returns
the next larger double value.
public
Number
parse
(String string, ParsePosition position)
Since: API Level 1
Parses a double from the specified string starting at the index specified
by position. The string is compared to the strings of this
ChoiceFormat and if a match occurs then the lower bound of the
corresponding range in the limits array is returned. If the string is
successfully parsed then the index of the ParsePosition passed to
this method is updated to the index following the parsed text.
If one of the format strings of this ChoiceFormat instance is
found in string starting at position.getIndex() then
the index in position is set to the index following the
parsed text;
the Double corresponding to the format
string is returned.
If none of the format strings is found in string then
the error index in position is set to the current index in
position; Double.NaN is returned.
Parameters
| |
|---|---|
| |
Returns
a Double resulting from the parse, or Double.NaN if there is an
error
public
static
final
double
previousDouble
(double value)
Since: API Level 1
Returns the double value which is closest to the specified double but
smaller.
Parameters
| |
|---|
Returns
the next smaller double value.
public
void
setChoices
(double[] limits, String[] formats)
Since: API Level 1
The length of the Sets the double values and associated strings of this ChoiceFormat. When
calling format with
a double value d, then the element i in formats
is selected where i fulfills
limits[i] <_ d="d" _="_" limitsi1="limitsi1" code="code">.
limits and formats arrays must be the
same.
Parameters
| |
|---|---|
| |
public
String
toPattern
()
Since: API Level 1
Returns the pattern of this ChoiceFormat which specifies the
ranges and their associated strings.
Returns
the pattern.