Udostępnij za pośrednictwem


ChoiceFormat Class

Definition

A ChoiceFormat allows you to attach a format to a range of numbers.

[Android.Runtime.Register("java/text/ChoiceFormat", DoNotGenerateAcw=true)]
public class ChoiceFormat : Java.Text.NumberFormat
[<Android.Runtime.Register("java/text/ChoiceFormat", DoNotGenerateAcw=true)>]
type ChoiceFormat = class
    inherit NumberFormat
Inheritance
Attributes

Remarks

A ChoiceFormat allows you to attach a format to a range of numbers. It is generally used in a MessageFormat for handling plurals. The choice is specified with an ascending list of doubles, where each item specifies a half-open interval up to the next item: <blockquote>

X matches j if and only if limit[j] &le; X &lt; limit[j+1]

</blockquote> If there is no match, then either the first or last index is used, depending on whether the number (X) is too low or too high. If the limit array is not in ascending order, the results of formatting will be incorrect. ChoiceFormat also accepts &#92;u221E as equivalent to infinity(INF).

<strong>Note:</strong> ChoiceFormat differs from the other Format classes in that you create a ChoiceFormat object with a constructor (not with a getInstance style factory method). The factory methods aren't necessary because ChoiceFormat doesn't require any complex setup for a given locale. In fact, ChoiceFormat doesn't implement any locale specific behavior.

When creating a ChoiceFormat, you must specify an array of formats and an array of limits. The length of these arrays must be the same. For example, <ul> <li> <em>limits</em> = {1,2,3,4,5,6,7}<br> <em>formats</em> = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"}<li> <em>limits</em> = {0, 1, ChoiceFormat.nextDouble(1)}<br> <em>formats</em> = {"no files", "one file", "many files"}<br> (nextDouble can be used to get the next higher double, to make the half-open interval.) </ul>

Here is a simple example that shows formatting and parsing: <blockquote>

{@code
            double[] limits = {1,2,3,4,5,6,7};
            String[] dayOfWeekNames = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};
            ChoiceFormat form = new ChoiceFormat(limits, dayOfWeekNames);
            ParsePosition status = new ParsePosition(0);
            for (double i = 0.0; i <= 8.0; ++i) {
                status.setIndex(0);
                System.out.println(i + " -> " + form.format(i) + " -> "
                                         + form.parse(form.format(i),status));
            }
            }

</blockquote> Here is a more complex example, with a pattern format: <blockquote>

{@code
            double[] filelimits = {0,1,2};
            String[] filepart = {"are no files","is one file","are {2} files"};
            ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
            Format[] testFormats = {fileform, null, NumberFormat.getInstance()};
            MessageFormat pattform = new MessageFormat("There {0} on {1}");
            pattform.setFormats(testFormats);
            Object[] testArgs = {null, "ADisk", null};
            for (int i = 0; i < 4; ++i) {
                testArgs[0] = new Integer(i);
                testArgs[2] = testArgs[0];
                System.out.println(pattform.format(testArgs));
            }
            }

</blockquote>

Specifying a pattern for ChoiceFormat objects is fairly straightforward. For example: <blockquote>

{@code
            ChoiceFormat fmt = new ChoiceFormat(
                 "-1#is negative| 0#is zero or fraction | 1#is one |1.0<is 1+ |2#is two |2<is more than 2.");
            System.out.println("Formatter Pattern : " + fmt.toPattern());

            System.out.println("Format with -INF : " + fmt.format(Double.NEGATIVE_INFINITY));
            System.out.println("Format with -1.0 : " + fmt.format(-1.0));
            System.out.println("Format with 0 : " + fmt.format(0));
            System.out.println("Format with 0.9 : " + fmt.format(0.9));
            System.out.println("Format with 1.0 : " + fmt.format(1));
            System.out.println("Format with 1.5 : " + fmt.format(1.5));
            System.out.println("Format with 2 : " + fmt.format(2));
            System.out.println("Format with 2.1 : " + fmt.format(2.1));
            System.out.println("Format with NaN : " + fmt.format(Double.NaN));
            System.out.println("Format with +INF : " + fmt.format(Double.POSITIVE_INFINITY));
            }

</blockquote> And the output result would be like the following: <blockquote>

{@code
            Format with -INF : is negative
            Format with -1.0 : is negative
            Format with 0 : is zero or fraction
            Format with 0.9 : is zero or fraction
            Format with 1.0 : is one
            Format with 1.5 : is 1+
            Format with 2 : is two
            Format with 2.1 : is more than 2.
            Format with NaN : is negative
            Format with +INF : is more than 2.
            }

</blockquote>

<h2>"synchronization">Synchronization</h2>

Choice formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.

Added in 1.1.

Java documentation for java.text.ChoiceFormat.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

ChoiceFormat(Double[], String[])

Constructs with the limits and the corresponding formats.

ChoiceFormat(IntPtr, JniHandleOwnership)

A constructor used when creating managed representations of JNI objects; called by the runtime.

ChoiceFormat(String)

Constructs with limits and corresponding formats based on the pattern.

Fields

FractionField

Field constant used to construct a FieldPosition object.

(Inherited from NumberFormat)
IntegerField

Field constant used to construct a FieldPosition object.

(Inherited from NumberFormat)

Properties

Class

Returns the runtime class of this Object.

(Inherited from Object)
Currency

Gets the currency used by this number format when formatting currency values. -or- Sets the currency used by this number format when formatting currency values.

(Inherited from NumberFormat)
GroupingUsed

Returns true if grouping is used in this format. -or- Set whether or not grouping will be used in this format.

(Inherited from NumberFormat)
Handle

The handle to the underlying Android instance.

(Inherited from Object)
JniIdentityHashCode (Inherited from Object)
JniPeerMembers
MaximumFractionDigits

Returns the maximum number of digits allowed in the fraction portion of a number. -or- Sets the maximum number of digits allowed in the fraction portion of a number.

(Inherited from NumberFormat)
MaximumIntegerDigits

Returns the maximum number of digits allowed in the integer portion of a number. -or- Sets the maximum number of digits allowed in the integer portion of a number.

(Inherited from NumberFormat)
MinimumFractionDigits

Returns the minimum number of digits allowed in the fraction portion of a number. -or- Sets the minimum number of digits allowed in the fraction portion of a number.

(Inherited from NumberFormat)
MinimumIntegerDigits

Returns the minimum number of digits allowed in the integer portion of a number. -or- Sets the minimum number of digits allowed in the integer portion of a number.

(Inherited from NumberFormat)
ParseIntegerOnly

Returns true if this format will parse numbers as integers only. -or- Sets whether or not numbers should be parsed as integers only.

(Inherited from NumberFormat)
PeerReference (Inherited from Object)
RoundingMode

Gets the java.math.RoundingMode used in this NumberFormat. -or- Sets the java.math.RoundingMode used in this NumberFormat.

(Inherited from NumberFormat)
ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Methods

ApplyPattern(String)

Sets the pattern.

Clone()

Creates and returns a copy of this object.

(Inherited from _Format)
Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
Format(Double, StringBuffer, FieldPosition)

Returns pattern with formatted double.

Format(Double)

Specialization of format.

(Inherited from NumberFormat)
Format(Int64, StringBuffer, FieldPosition)

Specialization of format.

Format(Int64)

Specialization of format.

(Inherited from NumberFormat)
Format(Object, StringBuffer, FieldPosition)

Formats a number and appends the resulting text to the given string buffer.

(Inherited from NumberFormat)
Format(Object)

Formats an object to produce a string.

(Inherited from _Format)
FormatToCharacterIterator(Object)

Formats an Object producing an AttributedCharacterIterator.

(Inherited from _Format)
GetFormats()

Returns the strings associated with the ranges of this ChoiceFormat.

GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
GetLimits()

Returns the limits of this ChoiceFormat.

JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
NextDouble(Double, Boolean)

Finds the least double greater than d (if positive is true), or the greatest double less than d (if positive is false).

NextDouble(Double)

Finds the least double greater than d.

Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
Parse(String, ParsePosition)

Parses a Number from the input text.

Parse(String)

Parses text from the beginning of the given string to produce a number.

(Inherited from NumberFormat)
ParseObject(String, ParsePosition)

Parses text from a string to produce a Number.

(Inherited from NumberFormat)
ParseObject(String)

Parses text from the beginning of the given string to produce an object.

(Inherited from _Format)
PreviousDouble(Double)

Finds the greatest double less than d.

SetChoices(Double[], String[])

Set the choices to be used in formatting.

SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
ToArray<T>() (Inherited from Object)
ToPattern()

Gets the pattern.

ToString()

Returns a string representation of the object.

(Inherited from Object)
UnregisterFromRuntime() (Inherited from Object)
Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)

Explicit Interface Implementations

IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)

Applies to