The resources contain three predefined formatters for each locale: spellout, which 100 * spells out a value in words (123 is "one hundred twenty-three"); ordinal, which 101 * appends an ordinal suffix to the end of a numeral (123 is "123rd"); and 102 * duration, which shows a duration in seconds as hours, minutes, and seconds (123 is 103 * "2:03"). The client can also define more specialized RuleBasedNumberFormats 104 * by supplying programmer-defined rule sets.
The behavior of a RuleBasedNumberFormat is specified by a textual description 107 * that is either passed to the constructor as a String or loaded from a resource 108 * bundle. In its simplest form, the description consists of a semicolon-delimited list of rules. 109 * Each rule has a string of output text and a value or range of values it is applicable to. 110 * In a typical spellout rule set, the first twenty rules are the words for the numbers from 111 * 0 to 19:
zero; one; two; three; four; five; six; seven; eight; nine; 114 * ten; eleven; twelve; thirteen; fourteen; fifteen; sixteen; seventeen; eighteen; nineteen;
For larger numbers, we can use the preceding set of rules to format the ones place, and 117 * we only have to supply the words for the multiples of 10:
20: twenty[->>]; 120 * 30: thirty[->>]; 121 * 40: forty[->>]; 122 * 50: fifty[->>]; 123 * 60: sixty[->>]; 124 * 70: seventy[->>]; 125 * 80: eighty[->>]; 126 * 90: ninety[->>];
In these rules, the base value is spelled out explicitly and set off from the 129 * rule's output text with a colon. The rules are in a sorted list, and a rule is applicable 130 * to all numbers from its own base value to one less than the next rule's base value. The 131 * ">>" token is called a substitution and tells the formatter to 132 * isolate the number's ones digit, format it using this same set of rules, and place the 133 * result at the position of the ">>" token. Text in brackets is omitted if 134 * the number being formatted is an even multiple of 10 (the hyphen is a literal hyphen; 24 135 * is "twenty-four," not "twenty four").
For even larger numbers, we can actually look up several parts of the number in the 138 * list:
100: << hundred[ >>];
The "<<" represents a new kind of substitution. The << isolates 143 * the hundreds digit (and any digits to its left), formats it using this same rule set, and 144 * places the result where the "<<" was. Notice also that the meaning of 145 * >> has changed: it now refers to both the tens and the ones digits. The meaning of 146 * both substitutions depends on the rule's base value. The base value determines the rule's divisor, 147 * which is the highest power of 10 that is less than or equal to the base value (the user 148 * can change this). To fill in the substitutions, the formatter divides the number being 149 * formatted by the divisor. The integral quotient is used to fill in the << 150 * substitution, and the remainder is used to fill in the >> substitution. The meaning 151 * of the brackets changes similarly: text in brackets is omitted if the value being 152 * formatted is an even multiple of the rule's divisor. The rules are applied recursively, so 153 * if a substitution is filled in with text that includes another substitution, that 154 * substitution is also filled in.
This rule covers values up to 999, at which point we add another rule:
1000: << thousand[ >>];
Again, the meanings of the brackets and substitution tokens shift because the rule's 161 * base value is a higher power of 10, changing the rule's divisor. This rule can actually be 162 * used all the way up to 999,999. This allows us to finish out the rules as follows:
1,000,000: << million[ >>]; 165 * 1,000,000,000: << billion[ >>]; 166 * 1,000,000,000,000: << trillion[ >>]; 167 * 1,000,000,000,000,000: OUT OF RANGE!;
Commas, periods, and spaces can be used in the base values to improve legibility and 170 * are ignored by the rule parser. The last rule in the list is customarily treated as an 171 * "overflow rule," applying to everything from its base value on up, and often (as 172 * in this example) being used to print out an error message or default representation. 173 * Notice also that the size of the major groupings in large numbers is controlled by the 174 * spacing of the rules: because in English we group numbers by thousand, the higher rules 175 * are separated from each other by a factor of 1,000.
To see how these rules actually work in practice, consider the following example: 178 * Formatting 25,430 with this rule set would work like this:
The above syntax suffices only to format positive integers. To format negative numbers, 209 * we add a special rule:
-x: minus >>;
This is called a negative-number rule, and is identified by "-x" 214 * where the base value would be. This rule is used to format all negative numbers. the 215 * >> token here means "find the number's absolute value, format it with these 216 * rules, and put the result here."
We also add a special rule called a fraction rule for numbers with fractional 219 * parts:
x.x: << point >>;
This rule is used for all positive non-integers (negative non-integers pass through the 224 * negative-number rule first and then through this rule). Here, the << token refers to 225 * the number's integral part, and the >> to the number's fractional part. The 226 * fractional part is formatted as a series of single-digit numbers (e.g., 123.456 would be 227 * formatted as "one hundred twenty-three point four five six").
To see how this rule syntax is applied to various languages, examine the resource data.
There is actually much more flexibility built into the rule language than the 232 * description above shows. A formatter may own multiple rule sets, which can be selected by 233 * the caller, and which can use each other to fill in their substitutions. Substitutions can 234 * also be filled in with digits, using a DecimalFormat object. There is syntax that can be 235 * used to alter a rule's divisor in various ways. And there is provision for much more 236 * flexible fraction handling. A complete description of the rule syntax follows:
The description of a RuleBasedNumberFormat's behavior consists of one or more rule 241 * sets. Each rule set consists of a name, a colon, and a list of rules. A rule 242 * set name must begin with a % sign. Rule sets with names that begin with a single % sign 243 * are public: the caller can specify that they be used to format and parse numbers. 244 * Rule sets with names that begin with %% are private: they exist only for the use 245 * of other rule sets. If a formatter only has one rule set, the name may be omitted.
The user can also specify a special "rule set" named %%lenient-parse. 248 * The body of %%lenient-parse isn't a set of number-formatting rules, but a RuleBasedCollator 249 * description which is used to define equivalences for lenient parsing. For more information 250 * on the syntax, see RuleBasedCollator. For more information on lenient parsing, 251 * see setLenientParse(). Note: symbols that have syntactic meaning 252 * in collation rules, such as '&', have no particular meaning when appearing outside 253 * of the lenient-parse rule set.
The body of a rule set consists of an ordered, semicolon-delimited list of rules. 256 * Internally, every rule has a base value, a divisor, rule text, and zero, one, or two substitutions. 257 * These parameters are controlled by the description syntax, which consists of a rule 258 * descriptor, a colon, and a rule body.
A rule descriptor can take one of the following forms (text in italics is the 261 * name of a token):
A rule set may be either a regular rule set or a fraction rule set, depending 347 * on whether it is used to format a number's integral part (or the whole number) or a 348 * number's fractional part. Using a rule set to format a rule's fractional part makes it a 349 * fraction rule set.
Which rule is used to format a number is defined according to one of the following 352 * algorithms: If the rule set is a regular rule set, do the following: 353 * 354 *
If the rule set is a fraction rule set, do the following: 370 * 371 *
A rule's body consists of a string of characters terminated by a semicolon. The rule 387 * may include zero, one, or two substitution tokens, and a range of text in 388 * brackets. The brackets denote optional text (and may also include one or both 389 * substitutions). The exact meanings of the substitution tokens, and under what conditions 390 * optional text is omitted, depend on the syntax of the substitution token and the context. 391 * The rest of the text in a rule body is literal text that is output when the rule matches 392 * the number being formatted.
A substitution token begins and ends with a token character. The token 395 * character and the context together specify a mathematical operation to be performed on the 396 * number being formatted. An optional substitution descriptor specifies how the 397 * value resulting from that operation is used to fill in the substitution. The position of 398 * the substitution token in the rule body specifies the location of the resultant text in 399 * the original rule text.
The meanings of the substitution token characters are as follows:
The substitution descriptor (i.e., the text between the token characters) may take one 514 * of three forms:
Whitespace is ignored between a rule set name and a rule set body, between a rule 543 * descriptor and a rule body, or between rules. If a rule body begins with an apostrophe, 544 * the apostrophe is ignored, but all text after it becomes significant (this is how you can 545 * have a rule's rule text begin with whitespace). There is no escape function: the semicolon 546 * is not allowed in rule set names or in rule text, and the colon is not allowed in rule set 547 * names. The characters beginning a substitution token are always treated as the beginning 548 * of a substitution token.
See the resource data and the demo program for annotated examples of real rule sets 551 * using these features.
User subclasses are not supported. While clients may write 554 * subclasses, such code will not necessarily work and will not be 555 * guaranteed to work stably from release to release. 556 * 557 *
Localizations
Constructors are available that allow the specification of localizations for the 559 * public rule sets (and also allow more control over what public rule sets are available). 560 * Localization data is represented as a textual description. The description represents 561 * an array of arrays of string. The first element is an array of the public rule set names, 562 * each of these must be one of the public rule set names that appear in the rules. Only 563 * names in this array will be treated as public rule set names by the API. Each subsequent 564 * element is an array of localizations of these names. The first element of one of these 565 * subarrays is the locale name, and the remaining elements are localizations of the 566 * public rule set names, in the same order as they were listed in the first array.
In the syntax, angle brackets '<', '>' are used to delimit the arrays, and comma ',' is used 568 * to separate elements of an array. Whitespace is ignored, unless quoted.
For example:
570 * < < %foo, %bar, %baz >, 571 * < en, Foo, Bar, Baz >, 572 * < fr, 'le Foo', 'le Bar', 'le Baz' > 573 * < zh, \\u7532, \\u4e59, \\u4e19 > > 574 *
605 * The localizations data provides information about the public 606 * rule sets and their localized display names for different 607 * locales. The first element in the list is an array of the names 608 * of the public rule sets. The first element in this array is 609 * the initial default ruleset. The remaining elements in the 610 * list are arrays of localizations of the names of the public 611 * rule sets. Each of these is one longer than the initial array, 612 * with the first String being the ULocale ID, and the remaining 613 * Strings being the localizations of the rule set names, in the 614 * same order as the initial array. Arrays are nullptr-terminated. 615 * @param rules A description of the formatter's desired behavior. 616 * See the class documentation for a complete explanation of the description 617 * syntax. 618 * @param localizations the localization information. 619 * names in the description. These will be copied by the constructor. 620 * @param perror The parse error if an error was encountered. 621 * @param status The status indicating whether the constructor succeeded. 622 * @stable ICU 3.2 623 */ 624 RuleBasedNumberFormat(const UnicodeString& rules, const UnicodeString& localizations, 625 UParseError& perror, UErrorCode& status); 626 627 /** 628 * Creates a RuleBasedNumberFormat that behaves according to the rules 629 * passed in. The formatter uses the specified locale to determine the 630 * characters to use when formatting numerals, and to define equivalences 631 * for lenient parsing. 632 * @param rules The formatter rules. 633 * See the class documentation for a complete explanation of the rule 634 * syntax. 635 * @param locale A locale that governs which characters are used for 636 * formatting values in numerals and which characters are equivalent in 637 * lenient parsing. 638 * @param perror The parse error if an error was encountered. 639 * @param status The status indicating whether the constructor succeeded. 640 * @stable ICU 2.0 641 */ 642 RuleBasedNumberFormat(const UnicodeString& rules, const Locale& locale, 643 UParseError& perror, UErrorCode& status); 644 645 /** 646 * Creates a RuleBasedNumberFormat that behaves according to the description 647 * passed in. The formatter uses the default locale. 648 *
649 * The localizations data provides information about the public 650 * rule sets and their localized display names for different 651 * locales. The first element in the list is an array of the names 652 * of the public rule sets. The first element in this array is 653 * the initial default ruleset. The remaining elements in the 654 * list are arrays of localizations of the names of the public 655 * rule sets. Each of these is one longer than the initial array, 656 * with the first String being the ULocale ID, and the remaining 657 * Strings being the localizations of the rule set names, in the 658 * same order as the initial array. Arrays are nullptr-terminated. 659 * @param rules A description of the formatter's desired behavior. 660 * See the class documentation for a complete explanation of the description 661 * syntax. 662 * @param localizations a list of localizations for the rule set 663 * names in the description. These will be copied by the constructor. 664 * @param locale A locale that governs which characters are used for 665 * formatting values in numerals and which characters are equivalent in 666 * lenient parsing. 667 * @param perror The parse error if an error was encountered. 668 * @param status The status indicating whether the constructor succeeded. 669 * @stable ICU 3.2 670 */ 671 RuleBasedNumberFormat(const UnicodeString& rules, const UnicodeString& localizations, 672 const Locale& locale, UParseError& perror, UErrorCode& status); 673 674 /** 675 * Creates a RuleBasedNumberFormat from a predefined ruleset. The selector 676 * code chose among three possible predefined formats: spellout, ordinal, 677 * and duration. 678 * @param tag A selector code specifying which kind of formatter to create for that 679 * locale. There are four legal values: URBNF_SPELLOUT, which creates a formatter that 680 * spells out a value in words in the desired language, URBNF_ORDINAL, which attaches 681 * an ordinal suffix from the desired language to the end of a number (e.g. "123rd"), 682 * URBNF_DURATION, which formats a duration in seconds as hours, minutes, and seconds always rounding down, 683 * and URBNF_NUMBERING_SYSTEM, which is used to invoke rules for alternate numbering 684 * systems such as the Hebrew numbering system, or for Roman Numerals, etc. 685 * NOTE: If you use URBNF_NUMBERING_SYSTEM, you must also call setDefaultRuleSet() to 686 * specify the exact numbering system you want to use. If you want the default numbering system 687 * for the locale, call NumberFormat::createInstance() instead of creating a RuleBasedNumberFormat directly. 688 * @param locale The locale for the formatter. 689 * @param status The status indicating whether the constructor succeeded. 690 * @stable ICU 2.0 691 */ 692 RuleBasedNumberFormat(URBNFRuleSetTag tag, const Locale& locale, UErrorCode& status); 693 694 //----------------------------------------------------------------------- 695 // boilerplate 696 //----------------------------------------------------------------------- 697 698 /** 699 * Copy constructor 700 * @param rhs the object to be copied from. 701 * @stable ICU 2.6 702 */ 703 RuleBasedNumberFormat(const RuleBasedNumberFormat& rhs); 704 705 /** 706 * Assignment operator 707 * @param rhs the object to be copied from. 708 * @stable ICU 2.6 709 */ 710 RuleBasedNumberFormat& operator=(const RuleBasedNumberFormat& rhs); 711 712 /** 713 * Release memory allocated for a RuleBasedNumberFormat when you are finished with it. 714 * @stable ICU 2.6 715 */ 716 virtual ~RuleBasedNumberFormat(); 717 718 /** 719 * Clone this object polymorphically. The caller is responsible 720 * for deleting the result when done. 721 * @return A copy of the object. 722 * @stable ICU 2.6 723 */ 724 virtual RuleBasedNumberFormat* clone() const override; 725 726 /** 727 * Return true if the given Format objects are semantically equal. 728 * Objects of different subclasses are considered unequal. 729 * @param other the object to be compared with. 730 * @return true if the given Format objects are semantically equal. 731 * @stable ICU 2.6 732 */ 733 virtual bool operator==(const Format& other) const override; 734 735 //----------------------------------------------------------------------- 736 // public API functions 737 //----------------------------------------------------------------------- 738 739 /** 740 * return the rules that were provided to the RuleBasedNumberFormat. 741 * @return the result String that was passed in 742 * @stable ICU 2.0 743 */ 744 virtual UnicodeString getRules() const; 745 746 /** 747 * Return the number of public rule set names. 748 * @return the number of public rule set names. 749 * @stable ICU 2.0 750 */ 751 virtual int32_t getNumberOfRuleSetNames() const; 752 753 /** 754 * Return the name of the index'th public ruleSet. If index is not valid, 755 * the function returns null. 756 * @param index the index of the ruleset 757 * @return the name of the index'th public ruleSet. 758 * @stable ICU 2.0 759 */ 760 virtual UnicodeString getRuleSetName(int32_t index) const; 761 762 /** 763 * Return the number of locales for which we have localized rule set display names. 764 * @return the number of locales for which we have localized rule set display names. 765 * @stable ICU 3.2 766 */ 767 virtual int32_t getNumberOfRuleSetDisplayNameLocales(void) const; 768 769 /** 770 * Return the index'th display name locale. 771 * @param index the index of the locale 772 * @param status set to a failure code when this function fails 773 * @return the locale 774 * @see #getNumberOfRuleSetDisplayNameLocales 775 * @stable ICU 3.2 776 */ 777 virtual Locale getRuleSetDisplayNameLocale(int32_t index, UErrorCode& status) const; 778 779 /** 780 * Return the rule set display names for the provided locale. These are in the same order 781 * as those returned by getRuleSetName. The locale is matched against the locales for 782 * which there is display name data, using normal fallback rules. If no locale matches, 783 * the default display names are returned. (These are the internal rule set names minus 784 * the leading '%'.) 785 * @param index the index of the rule set 786 * @param locale the locale (returned by getRuleSetDisplayNameLocales) for which the localized 787 * display name is desired 788 * @return the display name for the given index, which might be bogus if there is an error 789 * @see #getRuleSetName 790 * @stable ICU 3.2 791 */ 792 virtual UnicodeString getRuleSetDisplayName(int32_t index, 793 const Locale& locale = Locale::getDefault()); 794 795 /** 796 * Return the rule set display name for the provided rule set and locale. 797 * The locale is matched against the locales for which there is display name data, using 798 * normal fallback rules. If no locale matches, the default display name is returned. 799 * @return the display name for the rule set 800 * @stable ICU 3.2 801 * @see #getRuleSetDisplayName 802 */ 803 virtual UnicodeString getRuleSetDisplayName(const UnicodeString& ruleSetName, 804 const Locale& locale = Locale::getDefault()); 805 806 807 using NumberFormat::format; 808 809 /** 810 * Formats the specified 32-bit number using the default ruleset. 811 * @param number The number to format. 812 * @param toAppendTo the string that will hold the (appended) result 813 * @param pos the fieldposition 814 * @return A textual representation of the number. 815 * @stable ICU 2.0 816 */ 817 virtual UnicodeString& format(int32_t number, 818 UnicodeString& toAppendTo, 819 FieldPosition& pos) const override; 820 821 /** 822 * Formats the specified 64-bit number using the default ruleset. 823 * @param number The number to format. 824 * @param toAppendTo the string that will hold the (appended) result 825 * @param pos the fieldposition 826 * @return A textual representation of the number. 827 * @stable ICU 2.1 828 */ 829 virtual UnicodeString& format(int64_t number, 830 UnicodeString& toAppendTo, 831 FieldPosition& pos) const override; 832 /** 833 * Formats the specified number using the default ruleset. 834 * @param number The number to format. 835 * @param toAppendTo the string that will hold the (appended) result 836 * @param pos the fieldposition 837 * @return A textual representation of the number. 838 * @stable ICU 2.0 839 */ 840 virtual UnicodeString& format(double number, 841 UnicodeString& toAppendTo, 842 FieldPosition& pos) const override; 843 844 /** 845 * Formats the specified number using the named ruleset. 846 * @param number The number to format. 847 * @param ruleSetName The name of the rule set to format the number with. 848 * This must be the name of a valid public rule set for this formatter. 849 * @param toAppendTo the string that will hold the (appended) result 850 * @param pos the fieldposition 851 * @param status the status 852 * @return A textual representation of the number. 853 * @stable ICU 2.0 854 */ 855 virtual UnicodeString& format(int32_t number, 856 const UnicodeString& ruleSetName, 857 UnicodeString& toAppendTo, 858 FieldPosition& pos, 859 UErrorCode& status) const; 860 /** 861 * Formats the specified 64-bit number using the named ruleset. 862 * @param number The number to format. 863 * @param ruleSetName The name of the rule set to format the number with. 864 * This must be the name of a valid public rule set for this formatter. 865 * @param toAppendTo the string that will hold the (appended) result 866 * @param pos the fieldposition 867 * @param status the status 868 * @return A textual representation of the number. 869 * @stable ICU 2.1 870 */ 871 virtual UnicodeString& format(int64_t number, 872 const UnicodeString& ruleSetName, 873 UnicodeString& toAppendTo, 874 FieldPosition& pos, 875 UErrorCode& status) const; 876 /** 877 * Formats the specified number using the named ruleset. 878 * @param number The number to format. 879 * @param ruleSetName The name of the rule set to format the number with. 880 * This must be the name of a valid public rule set for this formatter. 881 * @param toAppendTo the string that will hold the (appended) result 882 * @param pos the fieldposition 883 * @param status the status 884 * @return A textual representation of the number. 885 * @stable ICU 2.0 886 */ 887 virtual UnicodeString& format(double number, 888 const UnicodeString& ruleSetName, 889 UnicodeString& toAppendTo, 890 FieldPosition& pos, 891 UErrorCode& status) const; 892 893 protected: 894 /** 895 * Format a decimal number. 896 * The number is a DigitList wrapper onto a floating point decimal number. 897 * The default implementation in NumberFormat converts the decimal number 898 * to a double and formats that. Subclasses of NumberFormat that want 899 * to specifically handle big decimal numbers must override this method. 900 * class DecimalFormat does so. 901 * 902 * @param number The number, a DigitList format Decimal Floating Point. 903 * @param appendTo Output parameter to receive result. 904 * Result is appended to existing contents. 905 * @param pos On input: an alignment field, if desired. 906 * On output: the offsets of the alignment field. 907 * @param status Output param filled with success/failure status. 908 * @return Reference to 'appendTo' parameter. 909 * @internal 910 */ 911 virtual UnicodeString& format(const number::impl::DecimalQuantity &number, 912 UnicodeString& appendTo, 913 FieldPosition& pos, 914 UErrorCode& status) const override; 915 public: 916 917 using NumberFormat::parse; 918 919 /** 920 * Parses the specified string, beginning at the specified position, according 921 * to this formatter's rules. This will match the string against all of the 922 * formatter's public rule sets and return the value corresponding to the longest 923 * parseable substring. This function's behavior is affected by the lenient 924 * parse mode. 925 * @param text The string to parse 926 * @param result the result of the parse, either a double or a long. 927 * @param parsePosition On entry, contains the position of the first character 928 * in "text" to examine. On exit, has been updated to contain the position 929 * of the first character in "text" that wasn't consumed by the parse. 930 * @see #setLenient 931 * @stable ICU 2.0 932 */ 933 virtual void parse(const UnicodeString& text, 934 Formattable& result, 935 ParsePosition& parsePosition) const override; 936 937 #if !UCONFIG_NO_COLLATION 938 939 /** 940 * Turns lenient parse mode on and off. 941 * 942 * When in lenient parse mode, the formatter uses a Collator for parsing the text. 943 * Only primary differences are treated as significant. This means that case 944 * differences, accent differences, alternate spellings of the same letter 945 * (e.g., ae and a-umlaut in German), ignorable characters, etc. are ignored in 946 * matching the text. In many cases, numerals will be accepted in place of words 947 * or phrases as well. 948 * 949 * For example, all of the following will correctly parse as 255 in English in 950 * lenient-parse mode: 951 * "two hundred fifty-five" 952 * "two hundred fifty five" 953 * "TWO HUNDRED FIFTY-FIVE" 954 * "twohundredfiftyfive" 955 * "2 hundred fifty-5" 956 * 957 * The Collator used is determined by the locale that was 958 * passed to this object on construction. The description passed to this object 959 * on construction may supply additional collation rules that are appended to the 960 * end of the default collator for the locale, enabling additional equivalences 961 * (such as adding more ignorable characters or permitting spelled-out version of 962 * symbols; see the demo program for examples). 963 * 964 * It's important to emphasize that even strict parsing is relatively lenient: it 965 * will accept some text that it won't produce as output. In English, for example, 966 * it will correctly parse "two hundred zero" and "fifteen hundred". 967 * 968 * @param enabled If true, turns lenient-parse mode on; if false, turns it off. 969 * @see RuleBasedCollator 970 * @stable ICU 2.0 971 */ 972 virtual void setLenient(UBool enabled) override; 973 974 /** 975 * Returns true if lenient-parse mode is turned on. Lenient parsing is off 976 * by default. 977 * @return true if lenient-parse mode is turned on. 978 * @see #setLenient 979 * @stable ICU 2.0 980 */ 981 virtual inline UBool isLenient(void) const override; 982 983 #endif 984 985 /** 986 * Override the default rule set to use. If ruleSetName is null, reset 987 * to the initial default rule set. If the rule set is not a public rule set name, 988 * U_ILLEGAL_ARGUMENT_ERROR is returned in status. 989 * @param ruleSetName the name of the rule set, or null to reset the initial default. 990 * @param status set to failure code when a problem occurs. 991 * @stable ICU 2.6 992 */ 993 virtual void setDefaultRuleSet(const UnicodeString& ruleSetName, UErrorCode& status); 994 995 /** 996 * Return the name of the current default rule set. If the current rule set is 997 * not public, returns a bogus (and empty) UnicodeString. 998 * @return the name of the current default rule set 999 * @stable ICU 3.0 1000 */ 1001 virtual UnicodeString getDefaultRuleSetName() const; 1002 1003 /** 1004 * Set a particular UDisplayContext value in the formatter, such as 1005 * UDISPCTX_CAPITALIZATION_FOR_STANDALONE. Note: For getContext, see 1006 * NumberFormat. 1007 * @param value The UDisplayContext value to set. 1008 * @param status Input/output status. If at entry this indicates a failure 1009 * status, the function will do nothing; otherwise this will be 1010 * updated with any new status from the function. 1011 * @stable ICU 53 1012 */ 1013 virtual void setContext(UDisplayContext value, UErrorCode& status) override; 1014 1015 /** 1016 * Get the rounding mode. 1017 * @return A rounding mode 1018 * @stable ICU 60 1019 */ 1020 virtual ERoundingMode getRoundingMode(void) const override; 1021 1022 /** 1023 * Set the rounding mode. 1024 * @param roundingMode A rounding mode 1025 * @stable ICU 60 1026 */ 1027 virtual void setRoundingMode(ERoundingMode roundingMode) override; 1028 1029 public: 1030 /** 1031 * ICU "poor man's RTTI", returns a UClassID for this class. 1032 * 1033 * @stable ICU 2.8 1034 */ 1035 static UClassID U_EXPORT2 getStaticClassID(void); 1036 1037 /** 1038 * ICU "poor man's RTTI", returns a UClassID for the actual class. 1039 * 1040 * @stable ICU 2.8 1041 */ 1042 virtual UClassID getDynamicClassID(void) const override; 1043 1044 /** 1045 * Sets the decimal format symbols, which is generally not changed 1046 * by the programmer or user. The formatter takes ownership of 1047 * symbolsToAdopt; the client must not delete it. 1048 * 1049 * @param symbolsToAdopt DecimalFormatSymbols to be adopted. 1050 * @stable ICU 49 1051 */ 1052 virtual void adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt); 1053 1054 /** 1055 * Sets the decimal format symbols, which is generally not changed 1056 * by the programmer or user. A clone of the symbols is created and 1057 * the symbols is _not_ adopted; the client is still responsible for 1058 * deleting it. 1059 * 1060 * @param symbols DecimalFormatSymbols. 1061 * @stable ICU 49 1062 */ 1063 virtual void setDecimalFormatSymbols(const DecimalFormatSymbols& symbols); 1064 1065 private: 1066 RuleBasedNumberFormat() = delete; // default constructor not implemented 1067 1068 // this will ref the localizations if they are not nullptr 1069 // caller must deref to get adoption 1070 RuleBasedNumberFormat(const UnicodeString& description, LocalizationInfo* localizations, 1071 const Locale& locale, UParseError& perror, UErrorCode& status); 1072 1073 void init(const UnicodeString& rules, LocalizationInfo* localizations, UParseError& perror, UErrorCode& status); 1074 void initCapitalizationContextInfo(const Locale& thelocale); 1075 void dispose(); 1076 void stripWhitespace(UnicodeString& src); 1077 void initDefaultRuleSet(); 1078 NFRuleSet* findRuleSet(const UnicodeString& name, UErrorCode& status) const; 1079 1080 /* friend access */ 1081 friend class NFSubstitution; 1082 friend class NFRule; 1083 friend class NFRuleSet; 1084 friend class FractionalPartSubstitution; 1085 1086 inline NFRuleSet * getDefaultRuleSet() const; 1087 const RuleBasedCollator * getCollator() const; 1088 DecimalFormatSymbols * initializeDecimalFormatSymbols(UErrorCode &status); 1089 const DecimalFormatSymbols * getDecimalFormatSymbols() const; 1090 NFRule * initializeDefaultInfinityRule(UErrorCode &status); 1091 const NFRule * getDefaultInfinityRule() const; 1092 NFRule * initializeDefaultNaNRule(UErrorCode &status); 1093 const NFRule * getDefaultNaNRule() const; 1094 PluralFormat *createPluralFormat(UPluralType pluralType, const UnicodeString &pattern, UErrorCode& status) const; 1095 UnicodeString& adjustForCapitalizationContext(int32_t startPos, UnicodeString& currentResult, UErrorCode& status) const; 1096 UnicodeString& format(int64_t number, NFRuleSet *ruleSet, UnicodeString& toAppendTo, UErrorCode& status) const; 1097 void format(double number, NFRuleSet& rs, UnicodeString& toAppendTo, UErrorCode& status) const; 1098 1099 private: 1100 NFRuleSet **fRuleSets; 1101 UnicodeString* ruleSetDescriptions; 1102 int32_t numRuleSets; 1103 NFRuleSet *defaultRuleSet; 1104 Locale locale; 1105 RuleBasedCollator* collator; 1106 DecimalFormatSymbols* decimalFormatSymbols; 1107 NFRule *defaultInfinityRule; 1108 NFRule *defaultNaNRule; 1109 ERoundingMode fRoundingMode; 1110 UBool lenient; 1111 UnicodeString* lenientParseRules; 1112 LocalizationInfo* localizations; 1113 UnicodeString originalDescription; 1114 UBool capitalizationInfoSet; 1115 UBool capitalizationForUIListMenu; 1116 UBool capitalizationForStandAlone; 1117 BreakIterator* capitalizationBrkIter; 1118 }; 1119 1120 // --------------- 1121 1122 #if !UCONFIG_NO_COLLATION 1123 1124 inline UBool 1125 RuleBasedNumberFormat::isLenient(void) const { 1126 return lenient; 1127 } 1128 1129 #endif 1130 1131 inline NFRuleSet* 1132 RuleBasedNumberFormat::getDefaultRuleSet() const { 1133 return defaultRuleSet; 1134 } 1135 1136 U_NAMESPACE_END 1137 1138 /* U_HAVE_RBNF */ 1139 #endif 1140 1141 #endif /* U_SHOW_CPLUSPLUS_API */ 1142 1143 /* RBNF_H */ 1144 #endif