2
*******************************************************************************
3
* Copyright (C) 1997-2001, International Business Machines Corporation and others. All Rights Reserved.
4
*******************************************************************************
10
#include "unicode/coll.h"
11
#include "unicode/dcfmtsym.h"
12
#include "unicode/fmtable.h"
13
#include "unicode/locid.h"
14
#include "unicode/numfmt.h"
15
#include "unicode/unistr.h"
16
#include "unicode/utypes.h"
22
/** Tags for the predefined rulesets. */
23
enum URBNFRuleSetTag {
31
* \brief C++ API: RuleBasedNumberFormat
33
* <h2> Rule Based Number Format C++ API </h2>
35
* <p>A class that formats numbers according to a set of rules. This number formatter is
36
* typically used for spelling out numeric values in words (e.g., 25,3476 as
37
* "twenty-five thousand three hundred seventy-six" or "vingt-cinq mille trois
38
* cents soixante-seize" or
39
* "fünfundzwanzigtausenddreihundertsechsundsiebzig"), but can also be used for
40
* other complicated formatting tasks, such as formatting a number of seconds as hours,
41
* minutes and seconds (e.g., 3,730 as "1:02:10").</p>
43
* <p>The resources contain three predefined formatters for each locale: spellout, which
44
* spells out a value in words (123 is "one hundred twenty-three"); ordinal, which
45
* appends an ordinal suffix to the end of a numeral (123 is "123rd"); and
46
* duration, which shows a duration in seconds as hours, minutes, and seconds (123 is
47
* "2:03"). The client can also define more specialized <tt>RuleBasedNumberFormat</tt>s
48
* by supplying programmer-defined rule sets.</p>
50
* <p>The behavior of a <tt>RuleBasedNumberFormat</tt> is specified by a textual description
51
* that is either passed to the constructor as a <tt>String</tt> or loaded from a resource
52
* bundle. In its simplest form, the description consists of a semicolon-delimited list of <em>rules.</em>
53
* Each rule has a string of output text and a value or range of values it is applicable to.
54
* In a typical spellout rule set, the first twenty rules are the words for the numbers from
57
* <pre>zero; one; two; three; four; five; six; seven; eight; nine;
58
* ten; eleven; twelve; thirteen; fourteen; fifteen; sixteen; seventeen; eighteen; nineteen;</pre>
60
* <p>For larger numbers, we can use the preceding set of rules to format the ones place, and
61
* we only have to supply the words for the multiples of 10:</p>
63
* <pre> 20: twenty[->>];
64
* 30: thirty[->>];
65
* 40: forty[->>];
66
* 50: fifty[->>];
67
* 60: sixty[->>];
68
* 70: seventy[->>];
69
* 80: eighty[->>];
70
* 90: ninety[->>];</pre>
72
* <p>In these rules, the <em>base value</em> is spelled out explicitly and set off from the
73
* rule's output text with a colon. The rules are in a sorted list, and a rule is applicable
74
* to all numbers from its own base value to one less than the next rule's base value. The
75
* ">>" token is called a <em>substitution</em> and tells the fomatter to
76
* isolate the number's ones digit, format it using this same set of rules, and place the
77
* result at the position of the ">>" token. Text in brackets is omitted if
78
* the number being formatted is an even multiple of 10 (the hyphen is a literal hyphen; 24
79
* is "twenty-four," not "twenty four").</p>
81
* <p>For even larger numbers, we can actually look up several parts of the number in the
84
* <pre>100: << hundred[ >>];</pre>
86
* <p>The "<<" represents a new kind of substitution. The << isolates
87
* the hundreds digit (and any digits to its left), formats it using this same rule set, and
88
* places the result where the "<<" was. Notice also that the meaning of
89
* >> has changed: it now refers to both the tens and the ones digits. The meaning of
90
* both substitutions depends on the rule's base value. The base value determines the rule's <em>divisor,</em>
91
* which is the highest power of 10 that is less than or equal to the base value (the user
92
* can change this). To fill in the substitutions, the formatter divides the number being
93
* formatted by the divisor. The integral quotient is used to fill in the <<
94
* substitution, and the remainder is used to fill in the >> substitution. The meaning
95
* of the brackets changes similarly: text in brackets is omitted if the value being
96
* formatted is an even multiple of the rule's divisor. The rules are applied recursively, so
97
* if a substitution is filled in with text that includes another substitution, that
98
* substitution is also filled in.</p>
100
* <p>This rule covers values up to 999, at which point we add another rule:</p>
102
* <pre>1000: << thousand[ >>];</pre>
104
* <p>Again, the meanings of the brackets and substitution tokens shift because the rule's
105
* base value is a higher power of 10, changing the rule's divisor. This rule can actually be
106
* used all the way up to 999,999. This allows us to finish out the rules as follows:</p>
108
* <pre> 1,000,000: << million[ >>];
109
* 1,000,000,000: << billion[ >>];
110
* 1,000,000,000,000: << trillion[ >>];
111
* 1,000,000,000,000,000: OUT OF RANGE!;</pre>
113
* <p>Commas, periods, and spaces can be used in the base values to improve legibility and
114
* are ignored by the rule parser. The last rule in the list is customarily treated as an
115
* "overflow rule," applying to everything from its base value on up, and often (as
116
* in this example) being used to print out an error message or default representation.
117
* Notice also that the size of the major groupings in large numbers is controlled by the
118
* spacing of the rules: because in English we group numbers by thousand, the higher rules
119
* are separated from each other by a factor of 1,000.</p>
121
* <p>To see how these rules actually work in practice, consider the following example:
122
* Formatting 25,430 with this rule set would work like this:</p>
124
* <table border="0" width="630">
126
* <td width="21"></td>
127
* <td width="257" valign="top"><strong><< thousand >></strong></td>
128
* <td width="340" valign="top">[the rule whose base value is 1,000 is applicable to 25,340]</td>
131
* <td width="21"></td>
132
* <td width="257" valign="top"><strong>twenty->></strong> thousand >></td>
133
* <td width="340" valign="top">[25,340 over 1,000 is 25. The rule for 20 applies.]</td>
136
* <td width="21"></td>
137
* <td width="257" valign="top">twenty-<strong>five</strong> thousand >></td>
138
* <td width="340" valign="top">[25 mod 10 is 5. The rule for 5 is "five."</td>
141
* <td width="21"></td>
142
* <td width="257" valign="top">twenty-five thousand <strong><< hundred >></strong></td>
143
* <td width="340" valign="top">[25,340 mod 1,000 is 340. The rule for 100 applies.]</td>
146
* <td width="21"></td>
147
* <td width="257" valign="top">twenty-five thousand <strong>three</strong> hundred >></td>
148
* <td width="340" valign="top">[340 over 100 is 3. The rule for 3 is "three."]</td>
151
* <td width="21"></td>
152
* <td width="257" valign="top">twenty-five thousand three hundred <strong>forty</strong></td>
153
* <td width="340" valign="top">[340 mod 100 is 40. The rule for 40 applies. Since 40 divides
154
* evenly by 10, the hyphen and substitution in the brackets are omitted.]</td>
158
* <p>The above syntax suffices only to format positive integers. To format negative numbers,
159
* we add a special rule:</p>
161
* <pre>-x: minus >>;</pre>
163
* <p>This is called a <em>negative-number rule,</em> and is identified by "-x"
164
* where the base value would be. This rule is used to format all negative numbers. the
165
* >> token here means "find the number's absolute value, format it with these
166
* rules, and put the result here."</p>
168
* <p>We also add a special rule called a <em>fraction rule </em>for numbers with fractional
171
* <pre>x.x: << point >>;</pre>
173
* <p>This rule is used for all positive non-integers (negative non-integers pass through the
174
* negative-number rule first and then through this rule). Here, the << token refers to
175
* the number's integral part, and the >> to the number's fractional part. The
176
* fractional part is formatted as a series of single-digit numbers (e.g., 123.456 would be
177
* formatted as "one hundred twenty-three point four five six").</p>
179
* <p>To see how this rule syntax is applied to various languages, examine the resource data.</p>
181
* <p>There is actually much more flexibility built into the rule language than the
182
* description above shows. A formatter may own multiple rule sets, which can be selected by
183
* the caller, and which can use each other to fill in their substitutions. Substitutions can
184
* also be filled in with digits, using a DecimalFormat object. There is syntax that can be
185
* used to alter a rule's divisor in various ways. And there is provision for much more
186
* flexible fraction handling. A complete description of the rule syntax follows:</p>
190
* <p>The description of a <tt>RuleBasedNumberFormat</tt>'s behavior consists of one or more <em>rule
191
* sets.</em> Each rule set consists of a name, a colon, and a list of <em>rules.</em> A rule
192
* set name must begin with a % sign. Rule sets with names that begin with a single % sign
193
* are <em>public:</em> the caller can specify that they be used to format and parse numbers.
194
* Rule sets with names that begin with %% are <em>private:</em> they exist only for the use
195
* of other rule sets. If a formatter only has one rule set, the name may be omitted.</p>
197
* <p>The user can also specify a special "rule set" named <tt>%%lenient-parse</tt>.
198
* The body of <tt>%%lenient-parse</tt> isn't a set of number-formatting rules, but a <tt>RuleBasedCollator</tt>
199
* description which is used to define equivalences for lenient parsing. For more information
200
* on the syntax, see <tt>RuleBasedCollator</tt>. For more information on lenient parsing,
201
* see <tt>setLenientParse()</tt>.</p>
203
* <p>The body of a rule set consists of an ordered, semicolon-delimited list of <em>rules.</em>
204
* Internally, every rule has a base value, a divisor, rule text, and zero, one, or two <em>substitutions.</em>
205
* These parameters are controlled by the description syntax, which consists of a <em>rule
206
* descriptor,</em> a colon, and a <em>rule body.</em></p>
208
* <p>A rule descriptor can take one of the following forms (text in <em>italics</em> is the
209
* name of a token):</p>
211
* <table border="0" width="100%">
213
* <td width="5%" valign="top"></td>
214
* <td width="8%" valign="top"><em>bv</em>:</td>
215
* <td valign="top"><em>bv</em> specifies the rule's base value. <em>bv</em> is a decimal
216
* number expressed using ASCII digits. <em>bv</em> may contain spaces, period, and commas,
217
* which are ignored. The rule's divisor is the highest power of 10 less than or equal to
218
* the base value.</td>
221
* <td width="5%" valign="top"></td>
222
* <td width="8%" valign="top"><em>bv</em>/<em>rad</em>:</td>
223
* <td valign="top"><em>bv</em> specifies the rule's base value. The rule's divisor is the
224
* highest power of <em>rad</em> less than or equal to the base value.</td>
227
* <td width="5%" valign="top"></td>
228
* <td width="8%" valign="top"><em>bv</em>>:</td>
229
* <td valign="top"><em>bv</em> specifies the rule's base value. To calculate the divisor,
230
* let the radix be 10, and the exponent be the highest exponent of the radix that yields a
231
* result less than or equal to the base value. Every > character after the base value
232
* decreases the exponent by 1. If the exponent is positive or 0, the divisor is the radix
233
* raised to the power of the exponent; otherwise, the divisor is 1.</td>
236
* <td width="5%" valign="top"></td>
237
* <td width="8%" valign="top"><em>bv</em>/<em>rad</em>>:</td>
238
* <td valign="top"><em>bv</em> specifies the rule's base value. To calculate the divisor,
239
* let the radix be <em>rad</em>, and the exponent be the highest exponent of the radix that
240
* yields a result less than or equal to the base value. Every > character after the radix
241
* decreases the exponent by 1. If the exponent is positive or 0, the divisor is the radix
242
* raised to the power of the exponent; otherwise, the divisor is 1.</td>
245
* <td width="5%" valign="top"></td>
246
* <td width="8%" valign="top">-x:</td>
247
* <td valign="top">The rule is a negative-number rule.</td>
250
* <td width="5%" valign="top"></td>
251
* <td width="8%" valign="top">x.x:</td>
252
* <td valign="top">The rule is an <em>improper fraction rule.</em></td>
255
* <td width="5%" valign="top"></td>
256
* <td width="8%" valign="top">0.x:</td>
257
* <td valign="top">The rule is a <em>proper fraction rule.</em></td>
260
* <td width="5%" valign="top"></td>
261
* <td width="8%" valign="top">x.0:</td>
262
* <td valign="top">The rule is a <em>master rule.</em></td>
265
* <td width="5%" valign="top"></td>
266
* <td width="8%" valign="top"><em>nothing</em></td>
267
* <td valign="top">If the rule's rule descriptor is left out, the base value is one plus the
268
* preceding rule's base value (or zero if this is the first rule in the list) in a normal
269
* rule set. In a fraction rule set, the base value is the same as the preceding rule's
274
* <p>A rule set may be either a regular rule set or a <em>fraction rule set,</em> depending
275
* on whether it is used to format a number's integral part (or the whole number) or a
276
* number's fractional part. Using a rule set to format a rule's fractional part makes it a
277
* fraction rule set.</p>
279
* <p>Which rule is used to format a number is defined according to one of the following
280
* algorithms: If the rule set is a regular rule set, do the following:
283
* <li>If the rule set includes a master rule (and the number was passed in as a <tt>double</tt>),
284
* use the master rule. (If the number being formatted was passed in as a <tt>long</tt>,
285
* the master rule is ignored.)</li>
286
* <li>If the number is negative, use the negative-number rule.</li>
287
* <li>If the number has a fractional part and is greater than 1, use the improper fraction
289
* <li>If the number has a fractional part and is between 0 and 1, use the proper fraction
291
* <li>Binary-search the rule list for the rule with the highest base value less than or equal
292
* to the number. If that rule has two substitutions, its base value is not an even multiple
293
* of its divisor, and the number <em>is</em> an even multiple of the rule's divisor, use the
294
* rule that precedes it in the rule list. Otherwise, use the rule itself.</li>
297
* <p>If the rule set is a fraction rule set, do the following:
300
* <li>Ignore negative-number and fraction rules.</li>
301
* <li>For each rule in the list, multiply the number being formatted (which will always be
302
* between 0 and 1) by the rule's base value. Keep track of the distance between the result
303
* the nearest integer.</li>
304
* <li>Use the rule that produced the result closest to zero in the above calculation. In the
305
* event of a tie or a direct hit, use the first matching rule encountered. (The idea here is
306
* to try each rule's base value as a possible denominator of a fraction. Whichever
307
* denominator produces the fraction closest in value to the number being formatted wins.) If
308
* the rule following the matching rule has the same base value, use it if the numerator of
309
* the fraction is anything other than 1; if the numerator is 1, use the original matching
310
* rule. (This is to allow singular and plural forms of the rule text without a lot of extra
314
* <p>A rule's body consists of a string of characters terminated by a semicolon. The rule
315
* may include zero, one, or two <em>substitution tokens,</em> and a range of text in
316
* brackets. The brackets denote optional text (and may also include one or both
317
* substitutions). The exact meanings of the substitution tokens, and under what conditions
318
* optional text is omitted, depend on the syntax of the substitution token and the context.
319
* The rest of the text in a rule body is literal text that is output when the rule matches
320
* the number being formatted.</p>
322
* <p>A substitution token begins and ends with a <em>token character.</em> The token
323
* character and the context together specify a mathematical operation to be performed on the
324
* number being formatted. An optional <em>substitution descriptor </em>specifies how the
325
* value resulting from that operation is used to fill in the substitution. The position of
326
* the substitution token in the rule body specifies the location of the resultant text in
327
* the original rule text.</p>
329
* <p>The meanings of the substitution token characters are as follows:</p>
331
* <table border="0" width="100%">
333
* <td width="37"></td>
334
* <td width="23">>></td>
335
* <td width="165" valign="top">in normal rule</td>
336
* <td>Divide the number by the rule's divisor and format the remainder</td>
339
* <td width="37"></td>
340
* <td width="23"></td>
341
* <td width="165" valign="top">in negative-number rule</td>
342
* <td>Find the absolute value of the number and format the result</td>
345
* <td width="37"></td>
346
* <td width="23"></td>
347
* <td width="165" valign="top">in fraction or master rule</td>
348
* <td>Isolate the number's fractional part and format it.</td>
351
* <td width="37"></td>
352
* <td width="23"></td>
353
* <td width="165" valign="top">in rule in fraction rule set</td>
354
* <td>Not allowed.</td>
357
* <td width="37"></td>
358
* <td width="23">>>></td>
359
* <td width="165" valign="top">in normal rule</td>
360
* <td>Divide the number by the rule's divisor and format the remainder,
361
* but bypass the normal rule-selection process and just use the
362
* rule that precedes this one in this rule list.</td>
365
* <td width="37"></td>
366
* <td width="23"></td>
367
* <td width="165" valign="top">in all other rules</td>
368
* <td>Not allowed.</td>
371
* <td width="37"></td>
372
* <td width="23"><<</td>
373
* <td width="165" valign="top">in normal rule</td>
374
* <td>Divide the number by the rule's divisor and format the quotient</td>
377
* <td width="37"></td>
378
* <td width="23"></td>
379
* <td width="165" valign="top">in negative-number rule</td>
380
* <td>Not allowed.</td>
383
* <td width="37"></td>
384
* <td width="23"></td>
385
* <td width="165" valign="top">in fraction or master rule</td>
386
* <td>Isolate the number's integral part and format it.</td>
389
* <td width="37"></td>
390
* <td width="23"></td>
391
* <td width="165" valign="top">in rule in fraction rule set</td>
392
* <td>Multiply the number by the rule's base value and format the result.</td>
395
* <td width="37"></td>
396
* <td width="23">==</td>
397
* <td width="165" valign="top">in all rule sets</td>
398
* <td>Format the number unchanged</td>
401
* <td width="37"></td>
402
* <td width="23">[]</td>
403
* <td width="165" valign="top">in normal rule</td>
404
* <td>Omit the optional text if the number is an even multiple of the rule's divisor</td>
407
* <td width="37"></td>
408
* <td width="23"></td>
409
* <td width="165" valign="top">in negative-number rule</td>
410
* <td>Not allowed.</td>
413
* <td width="37"></td>
414
* <td width="23"></td>
415
* <td width="165" valign="top">in improper-fraction rule</td>
416
* <td>Omit the optional text if the number is between 0 and 1 (same as specifying both an
417
* x.x rule and a 0.x rule)</td>
420
* <td width="37"></td>
421
* <td width="23"></td>
422
* <td width="165" valign="top">in master rule</td>
423
* <td>Omit the optional text if the number is an integer (same as specifying both an x.x
424
* rule and an x.0 rule)</td>
427
* <td width="37"></td>
428
* <td width="23"></td>
429
* <td width="165" valign="top">in proper-fraction rule</td>
430
* <td>Not allowed.</td>
433
* <td width="37"></td>
434
* <td width="23"></td>
435
* <td width="165" valign="top">in rule in fraction rule set</td>
436
* <td>Omit the optional text if multiplying the number by the rule's base value yields 1.</td>
440
* <p>The substitution descriptor (i.e., the text between the token characters) may take one
441
* of three forms:</p>
443
* <table border="0" width="100%">
445
* <td width="42"></td>
446
* <td width="166" valign="top">a rule set name</td>
447
* <td>Perform the mathematical operation on the number, and format the result using the
448
* named rule set.</td>
451
* <td width="42"></td>
452
* <td width="166" valign="top">a DecimalFormat pattern</td>
453
* <td>Perform the mathematical operation on the number, and format the result using a
454
* DecimalFormat with the specified pattern. The pattern must begin with 0 or #.</td>
457
* <td width="42"></td>
458
* <td width="166" valign="top">nothing</td>
459
* <td>Perform the mathematical operation on the number, and format the result using the rule
460
* set containing the current rule, except:<ul>
461
* <li>You can't have an empty substitution descriptor with a == substitution.</li>
462
* <li>If you omit the substitution descriptor in a >> substitution in a fraction rule,
463
* format the result one digit at a time using the rule set containing the current rule.</li>
464
* <li>If you omit the substitution descriptor in a << substitution in a rule in a
465
* fraction rule set, format the result using the default rule set for this formatter.</li>
471
* <p>Whitespace is ignored between a rule set name and a rule set body, between a rule
472
* descriptor and a rule body, or between rules. If a rule body begins with an apostrophe,
473
* the apostrophe is ignored, but all text after it becomes significant (this is how you can
474
* have a rule's rule text begin with whitespace). There is no escape function: the semicolon
475
* is not allowed in rule set names or in rule text, and the colon is not allowed in rule set
476
* names. The characters beginning a substitution token are always treated as the beginning
477
* of a substitution token.</p>
479
* <p>See the resource data and the demo program for annotated examples of real rule sets
480
* using these features.</p>
482
* @author Richard Gillam
487
class U_I18N_API RuleBasedNumberFormat : public NumberFormat {
490
//-----------------------------------------------------------------------
492
//-----------------------------------------------------------------------
495
* Creates a RuleBasedNumberFormat that behaves according to the rules
496
* passed in. The formatter uses the specified locale to determine the
497
* characters to use when formatting numerals, and to define equivalences
498
* for lenient parsing.
499
* @param rules The formatter rules.
500
* See the class documentation for a complete explanation of the rule
502
* @param locale A locale, that governs which characters are used for
503
* formatting values in numerals, and which characters are equivalent in
505
* @param perror The parse error if an error was encountered.
506
* @param status The status indicating whether the constructor succeeded.
509
RuleBasedNumberFormat(const UnicodeString& rules, const Locale& locale,
510
UParseError& perror, UErrorCode& status);
513
* Creates a RuleBasedNumberFormat from a predefined ruleset. The selector
514
* code choosed among three possible predefined formats: spellout, ordinal,
516
* @param tag A selector code specifying which kind of formatter to create for that
517
* locale. There are three legal values: URBNF_SPELLOUT, which creates a formatter that
518
* spells out a value in words in the desired language, URBNF_ORDINAL, which attaches
519
* an ordinal suffix from the desired language to the end of a number (e.g. "123rd"),
520
* and URBNF_DURATION, which formats a duration in seconds as hours, minutes, and seconds.
521
* @param locale The locale for the formatter.
522
* @param status The status indicating whether the constructor succeeded.
525
RuleBasedNumberFormat(URBNFRuleSetTag tag, const Locale& locale, UErrorCode& status);
527
//-----------------------------------------------------------------------
529
//-----------------------------------------------------------------------
534
RuleBasedNumberFormat(const RuleBasedNumberFormat& rhs);
537
* Assignment operator
539
RuleBasedNumberFormat& operator=(const RuleBasedNumberFormat& rhs);
542
* Release memory allocated for a RuleBasedNumberFormat when you are finished with it.
544
virtual ~RuleBasedNumberFormat();
547
* Clone this object polymorphically. The caller is responsible
548
* for deleting the result when done.
550
virtual Format* clone(void) const;
553
* Return true if the given Format objects are semantically equal.
554
* Objects of different subclasses are considered unequal.
556
virtual UBool operator==(const Format& other) const;
558
//-----------------------------------------------------------------------
559
// public API functions
560
//-----------------------------------------------------------------------
563
* @return the rules that were provided to the RuleBasedNumberFormat.
564
* @return the result String that was passed in
567
virtual UnicodeString getRules() const;
570
* Return the name of the index'th public ruleSet. If index is not valid,
571
* the function returns null.
572
* @param index the index of the ruleset
573
* @return the name of the index'th public ruleSet.
576
virtual UnicodeString getRuleSetName(int32_t index) const;
579
* Return the number of public rule set names.
580
* @return the number of public rule set names.
583
virtual int32_t getNumberOfRuleSetNames() const;
586
* Formats the specified number using the default ruleset.
587
* @param number The number to format.
588
* @param toAppendTo the string that will hold the (appended) result
589
* @param pos the fieldposition
590
* @return A textual representation of the number.
593
virtual UnicodeString& format(int32_t number,
594
UnicodeString& toAppendTo,
595
FieldPosition& pos) const;
597
* Formats the specified number using the default ruleset.
598
* @param number The number to format.
599
* @param toAppendTo the string that will hold the (appended) result
600
* @param pos the fieldposition
601
* @return A textual representation of the number.
604
virtual UnicodeString& format(double number,
605
UnicodeString& toAppendTo,
606
FieldPosition& pos) const;
609
* Formats the specified number using the default ruleset.
610
* @param number The number to format.
611
* @param ruleSetName The name of the rule set to format the number with.
612
* This must be the name of a valid public rule set for this formatter.
613
* @param toAppendTo the string that will hold the (appended) result
614
* @param pos the fieldposition
615
* @param status the status
616
* @return A textual representation of the number.
619
virtual UnicodeString& format(int32_t number,
620
const UnicodeString& ruleSetName,
621
UnicodeString& toAppendTo,
623
UErrorCode& status) const;
625
* Formats the specified number using the default ruleset.
626
* @param number The number to format.
627
* @param ruleSetName The name of the rule set to format the number with.
628
* This must be the name of a valid public rule set for this formatter.
629
* @param toAppendTo the string that will hold the (appended) result
630
* @param pos the fieldposition
631
* @param status the status
632
* @return A textual representation of the number.
635
virtual UnicodeString& format(double number,
636
const UnicodeString& ruleSetName,
637
UnicodeString& toAppendTo,
639
UErrorCode& status) const;
642
* Formats the specified number using the default ruleset.
643
* @param obj The number to format.
644
* @param toAppendTo the string that will hold the (appended) result
645
* @param pos the fieldposition
646
* @param status the status
647
* @return A textual representation of the number.
650
virtual UnicodeString& format(const Formattable& obj,
651
UnicodeString& toAppendTo,
653
UErrorCode& status) const;
655
* Redeclared Format method.
658
UnicodeString& format(const Formattable& obj,
659
UnicodeString& result,
660
UErrorCode& status) const;
663
* Redeclared NumberFormat method.
666
UnicodeString& format(double number,
667
UnicodeString& output) const;
670
* Redeclared NumberFormat method.
673
UnicodeString& format(int32_t number,
674
UnicodeString& output) const;
677
* Parses the specfied string, beginning at the specified position, according
678
* to this formatter's rules. This will match the string against all of the
679
* formatter's public rule sets and return the value corresponding to the longest
680
* parseable substring. This function's behavior is affected by the lenient
682
* @param text The string to parse
683
* @param result the result of the parse, either a double or a long.
684
* @param parsePosition On entry, contains the position of the first character
685
* in "text" to examine. On exit, has been updated to contain the position
686
* of the first character in "text" that wasn't consumed by the parse.
687
* @see #setLenientParseMode
690
virtual void parse(const UnicodeString& text,
692
ParsePosition& parsePosition) const;
696
* Redeclared Format method.
699
virtual inline void parse(const UnicodeString& text,
701
UErrorCode& status) const;
705
* Turns lenient parse mode on and off.
707
* When in lenient parse mode, the formatter uses a Collator for parsing the text.
708
* Only primary differences are treated as significant. This means that case
709
* differences, accent differences, alternate spellings of the same letter
710
* (e.g., ae and a-umlaut in German), ignorable characters, etc. are ignored in
711
* matching the text. In many cases, numerals will be accepted in place of words
712
* or phrases as well.
714
* For example, all of the following will correctly parse as 255 in English in
715
* lenient-parse mode:
716
* <br>"two hundred fifty-five"
717
* <br>"two hundred fifty five"
718
* <br>"TWO HUNDRED FIFTY-FIVE"
719
* <br>"twohundredfiftyfive"
720
* <br>"2 hundred fifty-5"
722
* The Collator used is determined by the locale that was
723
* passed to this object on construction. The description passed to this object
724
* on construction may supply additional collation rules that are appended to the
725
* end of the default collator for the locale, enabling additional equivalences
726
* (such as adding more ignorable characters or permitting spelled-out version of
727
* symbols; see the demo program for examples).
729
* It's important to emphasize that even strict parsing is relatively lenient: it
730
* will accept some text that it won't produce as output. In English, for example,
731
* it will correctly parse "two hundred zero" and "fifteen hundred".
733
* @param enabled If true, turns lenient-parse mode on; if false, turns it off.
734
* @see RuleBasedCollator
737
virtual void setLenient(UBool enabled);
740
* Returns true if lenient-parse mode is turned on. Lenient parsing is off
742
* @return true if lenient-parse mode is turned on.
743
* @see #setLenientParseMode
746
virtual inline UBool isLenient(void) const;
749
void init(const UnicodeString& rules, UParseError& perror, UErrorCode& status);
751
void stripWhitespace(UnicodeString& src);
752
void setDefaultRuleSet();
753
void format(double number, NFRuleSet& ruleSet);
754
NFRuleSet* findRuleSet(const UnicodeString& name, UErrorCode& status) const;
757
friend class NFSubstitution;
759
friend class FractionalPartSubstitution;
761
inline NFRuleSet * getDefaultRuleSet() const;
762
Collator * getCollator() const;
763
DecimalFormatSymbols * getDecimalFormatSymbols() const;
766
static const char fgClassID;
769
static UClassID getStaticClassID(void) { return (UClassID)&fgClassID; }
770
virtual UClassID getDynamicClassID(void) const { return getStaticClassID(); }
773
NFRuleSet **ruleSets;
774
NFRuleSet *defaultRuleSet;
777
DecimalFormatSymbols* decimalFormatSymbols;
779
UnicodeString* lenientParseRules;
784
inline UnicodeString&
785
RuleBasedNumberFormat::format(const Formattable& obj,
786
UnicodeString& result,
787
UErrorCode& status) const
789
// Don't use Format:: - use immediate base class only,
790
// in case immediate base modifies behavior later.
791
// dlf - the above comment is bogus, if there were a reason to modify
792
// it, it would be virtual, and there's no reason because it is
793
// a one-line macro in NumberFormat anyway, just like this one.
794
return NumberFormat::format(obj, result, status);
797
inline UnicodeString&
798
RuleBasedNumberFormat::format(double number, UnicodeString& output) const {
799
FieldPosition pos(0);
800
return format(number, output, pos);
803
inline UnicodeString&
804
RuleBasedNumberFormat::format(int32_t number, UnicodeString& output) const {
805
FieldPosition pos(0);
806
return format(number, output, pos);
810
RuleBasedNumberFormat::parse(const UnicodeString& text, Formattable& result, UErrorCode& status) const
812
NumberFormat::parse(text, result, status);
816
RuleBasedNumberFormat::isLenient(void) const {
821
RuleBasedNumberFormat::getDefaultRuleSet() const {
822
return defaultRuleSet;