A ChoiceFormat allows you to attach a format to a range of numbers
A ChoiceFormat allows you to attach a format to a range of numbers. It is generally used in a MessageFormat for doing things like plurals. The choice is specified with an ascending list of doubles, where each item specifies a half-open interval up to the next item:. X matches j if and only if limit[j] <= X < limit[j+1]If there is no match, then either the first or last index is used, depending on whether the number is too low or too high. The length of the array of formats must be the same as the length of the array of limits. For example,. {1,2,3,4,5,6,7}, . {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"} . {0, 1, ChoiceFormat::nextDouble(1)}, . {"no files", "one file", "many files"}(nextDouble can be used to get the next higher double, to make the half-open interval.)Here is a simple example that shows formatting and parsing:
. void SimpleChoiceExample( void ) . { . double limits[] = {1,2,3,4,5,6,7}; . UnicodeString monthNames[] = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"}; . ChoiceFormat* form = new ChoiceFormat(limits, monthNames, 7 ); . ParsePosition* status = new ParsePosition(0); . UnicodeString str; . FieldPosition f1(0), f2(0); . for (double i = 0.0; i <= 8.0; ++i) { . status->setIndex(0); . Formattable parseResult; . str.remove(); . cout << i << " -> " << form->format(i,str, f1) . << " -> " << parseResult << endl; . } . delete form; . delete status; . cout << endl; . }Here is a more complex example, with a pattern format.. void ComplexChoiceExample( void ) . { . double filelimits[] = {0,1,2}; . UnicodeString filepart[] = {"are no files","is one file","are {2} files"}; . ChoiceFormat* fileform = new ChoiceFormat(filelimits, filepart, 3 ); . UErrorCode success = U_ZERO_ERROR; . const Format* testFormats[] = { fileform, NULL, NumberFormat::createInstance(success) }; . MessageFormat* pattform = new MessageFormat("There {0} on {1}", success ); . pattform->setFormats( testFormats, 3 ); . Formattable testArgs[] = {0L, "Disk_A", 0L}; . FieldPosition fp(0); . UnicodeString str; . for (int32_t i = 0; i < 4; ++i) { . Formattable fInt(i); . testArgs[0] = fInt; . testArgs[2] = testArgs[0]; . str.remove(); . pattform->format(testArgs, 3, str, fp, success ); . cout << "Output for i=" << i << " : " << str << endl; . } . delete pattform; . cout << endl; . }ChoiceFormat objects may be converted to and from patterns. The syntax of these patterns is [TODO fill in this section with detail]. Here is an example of a ChoiceFormat pattern:You can either do this programmatically, as in the above example, or by using a pattern (see ChoiceFormat for more information) as in:
. "0#are no files|1#is one file|1<are many files"Here the notation is:. <number> "#" Specifies a limit value. . <number> "<" Specifies a limit of nextDouble(<number>). . <number> ">" Specifies a limit of previousDouble(<number>).Each limit value is followed by a string, which is terminated by a vertical bar character ("|"), except for the last string, which is terminated by the end of the string.
If no object can be parsed, parsePosition is unchanged, and NULL is
returned.
ChoiceFormat(const double* limits, const UnicodeString* formats, int32_t count )
formats - Array of formats.
count - Size of 'limits' and 'formats' arrays. ChoiceFormat(const ChoiceFormat&)
const ChoiceFormat& operator=(const ChoiceFormat&)
virtual ~ChoiceFormat()
virtual Format* clone(void) const
virtual bool_t operator==(const Format& other) const
virtual void applyPattern(const UnicodeString& pattern, UErrorCode& status)
status - Output param set to success/failure code on
exit. If the pattern is invalid, this will be
set to a failure result. virtual UnicodeString& toPattern(UnicodeString &pattern) const
virtual void adoptChoices(double* limitsToAdopt, UnicodeString* formatsToAdopt, int32_t count )
formatsToAdopt - The format strings you want to use for each limit.
count - The size of the above arrays. virtual void setChoices(const double* limitsToCopy, const UnicodeString* formatsToCopy, int32_t count )
formatsToCopy - The format strings you want to use for each limit.
count - The size of the above arrays. virtual const double* getLimits(int32_t& count) const
virtual const UnicodeString* getFormats(int32_t& count) const
virtual UnicodeString& format(double number, UnicodeString& toAppendTo, FieldPosition& pos) const
toAppendTo - The string to append the formatted string to.
This is an output parameter.
pos - On input: an alignment field, if desired.
On output: the offsets of the alignment field.
UnicodeString& format(const Formattable& obj, UnicodeString& result, UErrorCode& status) const
UnicodeString& format( double number, UnicodeString& output) const
UnicodeString& format( int32_t number, UnicodeString& output) const
virtual void parse(const UnicodeString& text, Formattable& result, ParsePosition& parsePosition) const
result - Formattable to be set to the parse result.
If parse fails, return contents are undefined.
parsePosition - The position to start parsing at on input.
On output, moved to after the last successfully
parse character. On parse failure, does not change.
virtual UClassID getDynamicClassID(void) const
static UClassID getStaticClassID(void)
. Base* polymorphic_pointer = createPolymorphicObject();
. if (polymorphic_pointer->getDynamicClassID() ==
. Derived::getStaticClassID()) ...
static double nextDouble(double d )
static double previousDouble(double d )
alphabetic index hierarchy of classes
this page has been generated automatically by doc++
(c)opyright by Malte Zöckler, Roland Wunderling
contact: doc++@zib.de