Patterns 157 * 158 *
A DecimalFormat consists of a pattern and a set of 159 * symbols. The pattern may be set directly using 160 * applyPattern(), or indirectly using other API methods which 161 * manipulate aspects of the pattern, such as the minimum number of integer 162 * digits. The symbols are stored in a DecimalFormatSymbols 163 * object. When using the NumberFormat factory methods, the 164 * pattern and symbols are read from ICU's locale data. 165 * 166 *
Special Pattern Characters 167 * 168 *
Many characters in a pattern are taken literally; they are matched during 169 * parsing and output unchanged during formatting. Special characters, on the 170 * other hand, stand for other characters, strings, or classes of characters. 171 * For example, the '#' character is replaced by a localized digit. Often the 172 * replacement character is the same as the pattern character; in the U.S. locale, 173 * the ',' grouping character is replaced by ','. However, the replacement is 174 * still happening, and if the symbols are modified, the grouping character 175 * changes. Some special characters affect the behavior of the formatter by 176 * their presence; for example, if the percent character is seen, then the 177 * value is multiplied by 100 before being displayed. 178 * 179 *
To insert a special character in a pattern as a literal, that is, without 180 * any special meaning, the character must be quoted. There are some exceptions to 181 * this which are noted below. 182 * 183 *
The characters listed here are used in non-localized patterns. Localized 184 * patterns use the corresponding characters taken from this formatter's 185 * DecimalFormatSymbols object instead, and these characters lose 186 * their special status. Two exceptions are the currency sign and quote, which 187 * are not localized. 188 * 189 *
0
1-9
\htmlonly@\endhtmlonly
#
.
-
,
E
+
;
\%
\\u2030
\htmlonly¤\endhtmlonly
\\u00A4
'
"'#'#"
"#123"
"# o''clock"
*
A DecimalFormat pattern contains a positive and negative 283 * subpattern, for example, "#,##0.00;(#,##0.00)". Each subpattern has a 284 * prefix, a numeric part, and a suffix. If there is no explicit negative 285 * subpattern, the negative subpattern is the localized minus sign prefixed to the 286 * positive subpattern. That is, "0.00" alone is equivalent to "0.00;-0.00". If there 287 * is an explicit negative subpattern, it serves only to specify the negative 288 * prefix and suffix; the number of digits, minimal digits, and other 289 * characteristics are ignored in the negative subpattern. That means that 290 * "#,##0.0#;(#)" has precisely the same result as "#,##0.0#;(#,##0.0#)". 291 * 292 *
The prefixes, suffixes, and various symbols used for infinity, digits, 293 * thousands separators, decimal separators, etc. may be set to arbitrary 294 * values, and they will appear properly during formatting. However, care must 295 * be taken that the symbols and strings do not conflict, or parsing will be 296 * unreliable. For example, either the positive and negative prefixes or the 297 * suffixes must be distinct for parse() to be able 298 * to distinguish positive from negative values. Another example is that the 299 * decimal separator and thousands separator should be distinct characters, or 300 * parsing will be impossible. 301 * 302 *
The grouping separator is a character that separates clusters of 303 * integer digits to make large numbers more legible. It commonly used for 304 * thousands, but in some locales it separates ten-thousands. The grouping 305 * size is the number of digits between the grouping separators, such as 3 306 * for "100,000,000" or 4 for "1 0000 0000". There are actually two different 307 * grouping sizes: One used for the least significant integer digits, the 308 * primary grouping size, and one used for all others, the 309 * secondary grouping size. In most locales these are the same, but 310 * sometimes they are different. For example, if the primary grouping interval 311 * is 3, and the secondary is 2, then this corresponds to the pattern 312 * "#,##,##0", and the number 123456789 is formatted as "12,34,56,789". If a 313 * pattern contains multiple grouping separators, the interval between the last 314 * one and the end of the integer defines the primary grouping size, and the 315 * interval between the last two defines the secondary grouping size. All others 316 * are ignored, so "#,##,###,####" == "###,###,####" == "##,#,###,####". 317 * 318 *
Illegal patterns, such as "#.#.#" or "#.###,###", will cause 319 * DecimalFormat to set a failing UErrorCode. 320 * 321 *
Pattern BNF 322 * 323 *
324 * pattern := subpattern (';' subpattern)? 325 * subpattern := prefix? number exponent? suffix? 326 * number := (integer ('.' fraction)?) | sigDigits 327 * prefix := '\\u0000'..'\\uFFFD' - specialCharacters 328 * suffix := '\\u0000'..'\\uFFFD' - specialCharacters 329 * integer := '#'* '0'* '0' 330 * fraction := '0'* '#'* 331 * sigDigits := '#'* '@' '@'* '#'* 332 * exponent := 'E' '+'? '0'* '0' 333 * padSpec := '*' padChar 334 * padChar := '\\u0000'..'\\uFFFD' - quote 335 * 336 * Notation: 337 * X* 0 or more instances of X 338 * X? 0 or 1 instances of X 339 * X|Y either X or Y 340 * C..D any character from C up to D, inclusive 341 * S-T characters in S, except those in T 342 *
Not indicated in the BNF syntax above: 347 * 348 *
padSpec
Parsing 369 * 370 *
DecimalFormat parses all Unicode characters that represent 371 * decimal digits, as defined by u_charDigitValue(). In addition, 372 * DecimalFormat also recognizes as digits the ten consecutive 373 * characters starting with the localized zero digit defined in the 374 * DecimalFormatSymbols object. During formatting, the 375 * DecimalFormatSymbols-based digits are output. 376 * 377 *
During parsing, grouping separators are ignored if in lenient mode; 378 * otherwise, if present, they must be in appropriate positions. 379 * 380 *
For currency parsing, the formatter is able to parse every currency 381 * style formats no matter which style the formatter is constructed with. 382 * For example, a formatter instance gotten from 383 * NumberFormat.getInstance(ULocale, NumberFormat.CURRENCYSTYLE) can parse 384 * formats such as "USD1.00" and "3.00 US dollars". 385 * 386 *
If parse(UnicodeString&,Formattable&,ParsePosition&) 387 * fails to parse a string, it leaves the parse position unchanged. 388 * The convenience method parse(UnicodeString&,Formattable&,UErrorCode&) 389 * indicates parse failure by setting a failing 390 * UErrorCode. 391 * 392 *
Formatting 393 * 394 *
Formatting is guided by several parameters, all of which can be 395 * specified either using a pattern or using the API. The following 396 * description applies to formats that do not use scientific 397 * notation or significant digits. 398 * 399 *
Special Values 427 * 428 *
NaN is represented as a single character, typically 429 * \\uFFFD. This character is determined by the 430 * DecimalFormatSymbols object. This is the only value for which 431 * the prefixes and suffixes are not used. 432 * 433 *
NaN
\\uFFFD
Infinity is represented as a single character, typically 434 * \\u221E, with the positive or negative prefixes and suffixes 435 * applied. The infinity character is determined by the 436 * DecimalFormatSymbols object. 437 * 438 * Scientific Notation 439 * 440 *
\\u221E
Numbers in scientific notation are expressed as the product of a mantissa 441 * and a power of ten, for example, 1234 can be expressed as 1.234 x 103. The 442 * mantissa is typically in the half-open interval [1.0, 10.0) or sometimes [0.0, 1.0), 443 * but it need not be. DecimalFormat supports arbitrary mantissas. 444 * DecimalFormat can be instructed to use scientific 445 * notation through the API or through the pattern. In a pattern, the exponent 446 * character immediately followed by one or more digit characters indicates 447 * scientific notation. Example: "0.###E0" formats the number 1234 as 448 * "1.234E3". 449 * 450 *
DecimalFormat
\@\@\@
12300
0.123
\@\@##
3.142
1.23
'@'
'#'
"@@@"
"@##"
"0.12"
'0'
"@00"
"@.###"
"#,#@#"
getMinimumSignificantDigits() - 1
getMaximumSignificantDigits() - 1
"@@###E0"
"0.0###E0"
Padding 590 * 591 *
DecimalFormat supports padding the result of 592 * format() to a specific width. Padding may be specified either 593 * through the API or through the pattern syntax. In a pattern the pad escape 594 * character, followed by a single pad character, causes padding to be parsed 595 * and formatted. The pad escape character is '*' in unlocalized patterns, and 596 * can be localized using DecimalFormatSymbols::setSymbol() with a 597 * DecimalFormatSymbols::kPadEscapeSymbol 598 * selector. For example, "$*x#,##0.00" formats 123 to 599 * "$xx123.00", and 1234 to "$1,234.00". 600 * 601 *
"$*x#,##0.00"
"$xx123.00"
"$1,234.00"
"* #0 o''clock"
Rounding 630 * 631 *
DecimalFormat supports rounding to a specific increment. For 632 * example, 1230 rounded to the nearest 50 is 1250. 1.234 rounded to the 633 * nearest 0.65 is 1.3. The rounding increment may be specified through the API 634 * or in a pattern. To specify a rounding increment in a pattern, include the 635 * increment in the pattern itself. "#,#50" specifies a rounding increment of 636 * 50. "#,##0.05" specifies a rounding increment of 0.05. 637 * 638 *
In the absence of an explicit rounding increment numbers are 639 * rounded to their formatted width. 640 * 641 *
Synchronization 658 * 659 *
DecimalFormat objects are not synchronized. Multiple 660 * threads should not access one formatter concurrently. 661 * 662 *
Subclassing 663 * 664 *
User subclasses are not supported. While clients may write 665 * subclasses, such code will not necessarily work and will not be 666 * guaranteed to work stably from release to release. 667 */ 668 class U_I18N_API DecimalFormat : public NumberFormat { 669 public: 670 /** 671 * Pad position. 672 * @stable ICU 2.4 673 */ 674 enum EPadPosition { 675 kPadBeforePrefix, kPadAfterPrefix, kPadBeforeSuffix, kPadAfterSuffix 676 }; 677 678 /** 679 * Create a DecimalFormat using the default pattern and symbols 680 * for the default locale. This is a convenient way to obtain a 681 * DecimalFormat when internationalization is not the main concern. 682 *
683 * To obtain standard formats for a given locale, use the factory methods 684 * on NumberFormat such as createInstance. These factories will 685 * return the most appropriate sub-class of NumberFormat for a given 686 * locale. 687 *
688 * NOTE: New users are strongly encouraged to use 689 * #icu::number::NumberFormatter instead of DecimalFormat. 690 * @param status Output param set to success/failure code. If the 691 * pattern is invalid this will be set to a failure code. 692 * @stable ICU 2.0 693 */ 694 DecimalFormat(UErrorCode& status); 695 696 /** 697 * Create a DecimalFormat from the given pattern and the symbols 698 * for the default locale. This is a convenient way to obtain a 699 * DecimalFormat when internationalization is not the main concern. 700 *
701 * To obtain standard formats for a given locale, use the factory methods 702 * on NumberFormat such as createInstance. These factories will 703 * return the most appropriate sub-class of NumberFormat for a given 704 * locale. 705 *
706 * NOTE: New users are strongly encouraged to use 707 * #icu::number::NumberFormatter instead of DecimalFormat. 708 * @param pattern A non-localized pattern string. 709 * @param status Output param set to success/failure code. If the 710 * pattern is invalid this will be set to a failure code. 711 * @stable ICU 2.0 712 */ 713 DecimalFormat(const UnicodeString& pattern, UErrorCode& status); 714 715 /** 716 * Create a DecimalFormat from the given pattern and symbols. 717 * Use this constructor when you need to completely customize the 718 * behavior of the format. 719 *
720 * To obtain standard formats for a given 721 * locale, use the factory methods on NumberFormat such as 722 * createInstance or createCurrencyInstance. If you need only minor adjustments 723 * to a standard format, you can modify the format returned by 724 * a NumberFormat factory method. 725 *
726 * NOTE: New users are strongly encouraged to use 727 * #icu::number::NumberFormatter instead of DecimalFormat. 728 * 729 * @param pattern a non-localized pattern string 730 * @param symbolsToAdopt the set of symbols to be used. The caller should not 731 * delete this object after making this call. 732 * @param status Output param set to success/failure code. If the 733 * pattern is invalid this will be set to a failure code. 734 * @stable ICU 2.0 735 */ 736 DecimalFormat(const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt, UErrorCode& status); 737 738 #ifndef U_HIDE_INTERNAL_API 739 740 /** 741 * This API is for ICU use only. 742 * Create a DecimalFormat from the given pattern, symbols, and style. 743 * 744 * @param pattern a non-localized pattern string 745 * @param symbolsToAdopt the set of symbols to be used. The caller should not 746 * delete this object after making this call. 747 * @param style style of decimal format 748 * @param status Output param set to success/failure code. If the 749 * pattern is invalid this will be set to a failure code. 750 * @internal 751 */ 752 DecimalFormat(const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt, 753 UNumberFormatStyle style, UErrorCode& status); 754 755 #if UCONFIG_HAVE_PARSEALLINPUT 756 757 /** 758 * @internal 759 */ 760 void setParseAllInput(UNumberFormatAttributeValue value); 761 762 #endif 763 764 #endif /* U_HIDE_INTERNAL_API */ 765 766 private: 767 768 /** 769 * Internal constructor for DecimalFormat; sets up internal fields. All public constructors should 770 * call this constructor. 771 */ 772 DecimalFormat(const DecimalFormatSymbols* symbolsToAdopt, UErrorCode& status); 773 774 public: 775 776 /** 777 * Set an integer attribute on this DecimalFormat. 778 * May return U_UNSUPPORTED_ERROR if this instance does not support 779 * the specified attribute. 780 * @param attr the attribute to set 781 * @param newValue new value 782 * @param status the error type 783 * @return *this - for chaining (example: format.setAttribute(...).setAttribute(...) ) 784 * @stable ICU 51 785 */ 786 virtual DecimalFormat& setAttribute(UNumberFormatAttribute attr, int32_t newValue, UErrorCode& status); 787 788 /** 789 * Get an integer 790 * May return U_UNSUPPORTED_ERROR if this instance does not support 791 * the specified attribute. 792 * @param attr the attribute to set 793 * @param status the error type 794 * @return the attribute value. Undefined if there is an error. 795 * @stable ICU 51 796 */ 797 virtual int32_t getAttribute(UNumberFormatAttribute attr, UErrorCode& status) const; 798 799 800 /** 801 * Set whether or not grouping will be used in this format. 802 * @param newValue True, grouping will be used in this format. 803 * @see getGroupingUsed 804 * @stable ICU 53 805 */ 806 void setGroupingUsed(UBool newValue) override; 807 808 /** 809 * Sets whether or not numbers should be parsed as integers only. 810 * @param value set True, this format will parse numbers as integers 811 * only. 812 * @see isParseIntegerOnly 813 * @stable ICU 53 814 */ 815 void setParseIntegerOnly(UBool value) override; 816 817 /** 818 * Sets whether lenient parsing should be enabled (it is off by default). 819 * 820 * @param enable \c true if lenient parsing should be used, 821 * \c false otherwise. 822 * @stable ICU 4.8 823 */ 824 void setLenient(UBool enable) override; 825 826 /** 827 * Create a DecimalFormat from the given pattern and symbols. 828 * Use this constructor when you need to completely customize the 829 * behavior of the format. 830 *
831 * To obtain standard formats for a given 832 * locale, use the factory methods on NumberFormat such as 833 * createInstance or createCurrencyInstance. If you need only minor adjustments 834 * to a standard format, you can modify the format returned by 835 * a NumberFormat factory method. 836 *
837 * NOTE: New users are strongly encouraged to use 838 * #icu::number::NumberFormatter instead of DecimalFormat. 839 * 840 * @param pattern a non-localized pattern string 841 * @param symbolsToAdopt the set of symbols to be used. The caller should not 842 * delete this object after making this call. 843 * @param parseError Output param to receive errors occurred during parsing 844 * @param status Output param set to success/failure code. If the 845 * pattern is invalid this will be set to a failure code. 846 * @stable ICU 2.0 847 */ 848 DecimalFormat(const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt, 849 UParseError& parseError, UErrorCode& status); 850 851 /** 852 * Create a DecimalFormat from the given pattern and symbols. 853 * Use this constructor when you need to completely customize the 854 * behavior of the format. 855 *
856 * To obtain standard formats for a given 857 * locale, use the factory methods on NumberFormat such as 858 * createInstance or createCurrencyInstance. If you need only minor adjustments 859 * to a standard format, you can modify the format returned by 860 * a NumberFormat factory method. 861 *
862 * NOTE: New users are strongly encouraged to use 863 * #icu::number::NumberFormatter instead of DecimalFormat. 864 * 865 * @param pattern a non-localized pattern string 866 * @param symbols the set of symbols to be used 867 * @param status Output param set to success/failure code. If the 868 * pattern is invalid this will be set to a failure code. 869 * @stable ICU 2.0 870 */ 871 DecimalFormat(const UnicodeString& pattern, const DecimalFormatSymbols& symbols, UErrorCode& status); 872 873 /** 874 * Copy constructor. 875 * 876 * @param source the DecimalFormat object to be copied from. 877 * @stable ICU 2.0 878 */ 879 DecimalFormat(const DecimalFormat& source); 880 881 /** 882 * Assignment operator. 883 * 884 * @param rhs the DecimalFormat object to be copied. 885 * @stable ICU 2.0 886 */ 887 DecimalFormat& operator=(const DecimalFormat& rhs); 888 889 /** 890 * Destructor. 891 * @stable ICU 2.0 892 */ 893 ~DecimalFormat() override; 894 895 /** 896 * Clone this Format object polymorphically. The caller owns the 897 * result and should delete it when done. 898 * 899 * @return a polymorphic copy of this DecimalFormat. 900 * @stable ICU 2.0 901 */ 902 DecimalFormat* clone() const override; 903 904 /** 905 * Return true if the given Format objects are semantically equal. 906 * Objects of different subclasses are considered unequal. 907 * 908 * @param other the object to be compared with. 909 * @return true if the given Format objects are semantically equal. 910 * @stable ICU 2.0 911 */ 912 bool operator==(const Format& other) const override; 913 914 915 using NumberFormat::format; 916 917 /** 918 * Format a double or long number using base-10 representation. 919 * 920 * @param number The value to be formatted. 921 * @param appendTo Output parameter to receive result. 922 * Result is appended to existing contents. 923 * @param pos On input: an alignment field, if desired. 924 * On output: the offsets of the alignment field. 925 * @return Reference to 'appendTo' parameter. 926 * @stable ICU 2.0 927 */ 928 UnicodeString& format(double number, UnicodeString& appendTo, FieldPosition& pos) const override; 929 930 #ifndef U_HIDE_INTERNAL_API 931 /** 932 * Format a double or long number using base-10 representation. 933 * 934 * @param number The value to be formatted. 935 * @param appendTo Output parameter to receive result. 936 * Result is appended to existing contents. 937 * @param pos On input: an alignment field, if desired. 938 * On output: the offsets of the alignment field. 939 * @param status 940 * @return Reference to 'appendTo' parameter. 941 * @internal 942 */ 943 UnicodeString& format(double number, UnicodeString& appendTo, FieldPosition& pos, 944 UErrorCode& status) const override; 945 #endif /* U_HIDE_INTERNAL_API */ 946 947 /** 948 * Format a double or long number using base-10 representation. 949 * 950 * @param number The value to be formatted. 951 * @param appendTo Output parameter to receive result. 952 * Result is appended to existing contents. 953 * @param posIter On return, can be used to iterate over positions 954 * of fields generated by this format call. 955 * Can be nullptr. 956 * @param status Output param filled with success/failure status. 957 * @return Reference to 'appendTo' parameter. 958 * @stable ICU 4.4 959 */ 960 UnicodeString& format(double number, UnicodeString& appendTo, FieldPositionIterator* posIter, 961 UErrorCode& status) const override; 962 963 /** 964 * Format a long number using base-10 representation. 965 * 966 * @param number The value to be formatted. 967 * @param appendTo Output parameter to receive result. 968 * Result is appended to existing contents. 969 * @param pos On input: an alignment field, if desired. 970 * On output: the offsets of the alignment field. 971 * @return Reference to 'appendTo' parameter. 972 * @stable ICU 2.0 973 */ 974 UnicodeString& format(int32_t number, UnicodeString& appendTo, FieldPosition& pos) const override; 975 976 #ifndef U_HIDE_INTERNAL_API 977 /** 978 * Format a long number using base-10 representation. 979 * 980 * @param number The value to be formatted. 981 * @param appendTo Output parameter to receive result. 982 * Result is appended to existing contents. 983 * @param pos On input: an alignment field, if desired. 984 * On output: the offsets of the alignment field. 985 * @param status Output param filled with success/failure status. 986 * @return Reference to 'appendTo' parameter. 987 * @internal 988 */ 989 UnicodeString& format(int32_t number, UnicodeString& appendTo, FieldPosition& pos, 990 UErrorCode& status) const override; 991 #endif /* U_HIDE_INTERNAL_API */ 992 993 /** 994 * Format a long number using base-10 representation. 995 * 996 * @param number The value to be formatted. 997 * @param appendTo Output parameter to receive result. 998 * Result is appended to existing contents. 999 * @param posIter On return, can be used to iterate over positions 1000 * of fields generated by this format call. 1001 * Can be nullptr. 1002 * @param status Output param filled with success/failure status. 1003 * @return Reference to 'appendTo' parameter. 1004 * @stable ICU 4.4 1005 */ 1006 UnicodeString& format(int32_t number, UnicodeString& appendTo, FieldPositionIterator* posIter, 1007 UErrorCode& status) const override; 1008 1009 /** 1010 * Format an int64 number using base-10 representation. 1011 * 1012 * @param number The value to be formatted. 1013 * @param appendTo Output parameter to receive result. 1014 * Result is appended to existing contents. 1015 * @param pos On input: an alignment field, if desired. 1016 * On output: the offsets of the alignment field. 1017 * @return Reference to 'appendTo' parameter. 1018 * @stable ICU 2.8 1019 */ 1020 UnicodeString& format(int64_t number, UnicodeString& appendTo, FieldPosition& pos) const override; 1021 1022 #ifndef U_HIDE_INTERNAL_API 1023 /** 1024 * Format an int64 number using base-10 representation. 1025 * 1026 * @param number The value to be formatted. 1027 * @param appendTo Output parameter to receive result. 1028 * Result is appended to existing contents. 1029 * @param pos On input: an alignment field, if desired. 1030 * On output: the offsets of the alignment field. 1031 * @param status Output param filled with success/failure status. 1032 * @return Reference to 'appendTo' parameter. 1033 * @internal 1034 */ 1035 UnicodeString& format(int64_t number, UnicodeString& appendTo, FieldPosition& pos, 1036 UErrorCode& status) const override; 1037 #endif /* U_HIDE_INTERNAL_API */ 1038 1039 /** 1040 * Format an int64 number using base-10 representation. 1041 * 1042 * @param number The value to be formatted. 1043 * @param appendTo Output parameter to receive result. 1044 * Result is appended to existing contents. 1045 * @param posIter On return, can be used to iterate over positions 1046 * of fields generated by this format call. 1047 * Can be nullptr. 1048 * @param status Output param filled with success/failure status. 1049 * @return Reference to 'appendTo' parameter. 1050 * @stable ICU 4.4 1051 */ 1052 UnicodeString& format(int64_t number, UnicodeString& appendTo, FieldPositionIterator* posIter, 1053 UErrorCode& status) const override; 1054 1055 /** 1056 * Format a decimal number. 1057 * The syntax of the unformatted number is a "numeric string" 1058 * as defined in the Decimal Arithmetic Specification, available at 1059 * http://speleotrove.com/decimal 1060 * 1061 * @param number The unformatted number, as a string. 1062 * @param appendTo Output parameter to receive result. 1063 * Result is appended to existing contents. 1064 * @param posIter On return, can be used to iterate over positions 1065 * of fields generated by this format call. 1066 * Can be nullptr. 1067 * @param status Output param filled with success/failure status. 1068 * @return Reference to 'appendTo' parameter. 1069 * @stable ICU 4.4 1070 */ 1071 UnicodeString& format(StringPiece number, UnicodeString& appendTo, FieldPositionIterator* posIter, 1072 UErrorCode& status) const override; 1073 1074 #ifndef U_HIDE_INTERNAL_API 1075 1076 /** 1077 * Format a decimal number. 1078 * The number is a DecimalQuantity wrapper onto a floating point decimal number. 1079 * The default implementation in NumberFormat converts the decimal number 1080 * to a double and formats that. 1081 * 1082 * @param number The number, a DecimalQuantity format Decimal Floating Point. 1083 * @param appendTo Output parameter to receive result. 1084 * Result is appended to existing contents. 1085 * @param posIter On return, can be used to iterate over positions 1086 * of fields generated by this format call. 1087 * @param status Output param filled with success/failure status. 1088 * @return Reference to 'appendTo' parameter. 1089 * @internal 1090 */ 1091 UnicodeString& format(const number::impl::DecimalQuantity& number, UnicodeString& appendTo, 1092 FieldPositionIterator* posIter, UErrorCode& status) const override; 1093 1094 /** 1095 * Format a decimal number. 1096 * The number is a DecimalQuantity wrapper onto a floating point decimal number. 1097 * The default implementation in NumberFormat converts the decimal number 1098 * to a double and formats that. 1099 * 1100 * @param number The number, a DecimalQuantity format Decimal Floating Point. 1101 * @param appendTo Output parameter to receive result. 1102 * Result is appended to existing contents. 1103 * @param pos On input: an alignment field, if desired. 1104 * On output: the offsets of the alignment field. 1105 * @param status Output param filled with success/failure status. 1106 * @return Reference to 'appendTo' parameter. 1107 * @internal 1108 */ 1109 UnicodeString& format(const number::impl::DecimalQuantity& number, UnicodeString& appendTo, 1110 FieldPosition& pos, UErrorCode& status) const override; 1111 1112 #endif // U_HIDE_INTERNAL_API 1113 1114 using NumberFormat::parse; 1115 1116 /** 1117 * Parse the given string using this object's choices. The method 1118 * does string comparisons to try to find an optimal match. 1119 * If no object can be parsed, index is unchanged, and nullptr is 1120 * returned. The result is returned as the most parsimonious 1121 * type of Formattable that will accommodate all of the 1122 * necessary precision. For example, if the result is exactly 12, 1123 * it will be returned as a long. However, if it is 1.5, it will 1124 * be returned as a double. 1125 * 1126 * @param text The text to be parsed. 1127 * @param result Formattable to be set to the parse result. 1128 * If parse fails, return contents are undefined. 1129 * @param parsePosition The position to start parsing at on input. 1130 * On output, moved to after the last successfully 1131 * parse character. On parse failure, does not change. 1132 * @see Formattable 1133 * @stable ICU 2.0 1134 */ 1135 void parse(const UnicodeString& text, Formattable& result, 1136 ParsePosition& parsePosition) const override; 1137 1138 /** 1139 * Parses text from the given string as a currency amount. Unlike 1140 * the parse() method, this method will attempt to parse a generic 1141 * currency name, searching for a match of this object's locale's 1142 * currency display names, or for a 3-letter ISO currency code. 1143 * This method will fail if this format is not a currency format, 1144 * that is, if it does not contain the currency pattern symbol 1145 * (U+00A4) in its prefix or suffix. 1146 * 1147 * @param text the string to parse 1148 * @param pos input-output position; on input, the position within text 1149 * to match; must have 0 <= pos.getIndex() < text.length(); 1150 * on output, the position after the last matched character. 1151 * If the parse fails, the position in unchanged upon output. 1152 * @return if parse succeeds, a pointer to a newly-created CurrencyAmount 1153 * object (owned by the caller) containing information about 1154 * the parsed currency; if parse fails, this is nullptr. 1155 * @stable ICU 49 1156 */ 1157 CurrencyAmount* parseCurrency(const UnicodeString& text, ParsePosition& pos) const override; 1158 1159 /** 1160 * Returns the decimal format symbols, which is generally not changed 1161 * by the programmer or user. 1162 * @return desired DecimalFormatSymbols 1163 * @see DecimalFormatSymbols 1164 * @stable ICU 2.0 1165 */ 1166 virtual const DecimalFormatSymbols* getDecimalFormatSymbols(void) const; 1167 1168 /** 1169 * Sets the decimal format symbols, which is generally not changed 1170 * by the programmer or user. 1171 * @param symbolsToAdopt DecimalFormatSymbols to be adopted. 1172 * @stable ICU 2.0 1173 */ 1174 virtual void adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt); 1175 1176 /** 1177 * Sets the decimal format symbols, which is generally not changed 1178 * by the programmer or user. 1179 * @param symbols DecimalFormatSymbols. 1180 * @stable ICU 2.0 1181 */ 1182 virtual void setDecimalFormatSymbols(const DecimalFormatSymbols& symbols); 1183 1184 1185 /** 1186 * Returns the currency plural format information, 1187 * which is generally not changed by the programmer or user. 1188 * @return desired CurrencyPluralInfo 1189 * @stable ICU 4.2 1190 */ 1191 virtual const CurrencyPluralInfo* getCurrencyPluralInfo(void) const; 1192 1193 /** 1194 * Sets the currency plural format information, 1195 * which is generally not changed by the programmer or user. 1196 * @param toAdopt CurrencyPluralInfo to be adopted. 1197 * @stable ICU 4.2 1198 */ 1199 virtual void adoptCurrencyPluralInfo(CurrencyPluralInfo* toAdopt); 1200 1201 /** 1202 * Sets the currency plural format information, 1203 * which is generally not changed by the programmer or user. 1204 * @param info Currency Plural Info. 1205 * @stable ICU 4.2 1206 */ 1207 virtual void setCurrencyPluralInfo(const CurrencyPluralInfo& info); 1208 1209 1210 /** 1211 * Get the positive prefix. 1212 * 1213 * @param result Output param which will receive the positive prefix. 1214 * @return A reference to 'result'. 1215 * Examples: +123, $123, sFr123 1216 * @stable ICU 2.0 1217 */ 1218 UnicodeString& getPositivePrefix(UnicodeString& result) const; 1219 1220 /** 1221 * Set the positive prefix. 1222 * 1223 * @param newValue the new value of the the positive prefix to be set. 1224 * Examples: +123, $123, sFr123 1225 * @stable ICU 2.0 1226 */ 1227 virtual void setPositivePrefix(const UnicodeString& newValue); 1228 1229 /** 1230 * Get the negative prefix. 1231 * 1232 * @param result Output param which will receive the negative prefix. 1233 * @return A reference to 'result'. 1234 * Examples: -123, ($123) (with negative suffix), sFr-123 1235 * @stable ICU 2.0 1236 */ 1237 UnicodeString& getNegativePrefix(UnicodeString& result) const; 1238 1239 /** 1240 * Set the negative prefix. 1241 * 1242 * @param newValue the new value of the the negative prefix to be set. 1243 * Examples: -123, ($123) (with negative suffix), sFr-123 1244 * @stable ICU 2.0 1245 */ 1246 virtual void setNegativePrefix(const UnicodeString& newValue); 1247 1248 /** 1249 * Get the positive suffix. 1250 * 1251 * @param result Output param which will receive the positive suffix. 1252 * @return A reference to 'result'. 1253 * Example: 123% 1254 * @stable ICU 2.0 1255 */ 1256 UnicodeString& getPositiveSuffix(UnicodeString& result) const; 1257 1258 /** 1259 * Set the positive suffix. 1260 * 1261 * @param newValue the new value of the positive suffix to be set. 1262 * Example: 123% 1263 * @stable ICU 2.0 1264 */ 1265 virtual void setPositiveSuffix(const UnicodeString& newValue); 1266 1267 /** 1268 * Get the negative suffix. 1269 * 1270 * @param result Output param which will receive the negative suffix. 1271 * @return A reference to 'result'. 1272 * Examples: -123%, ($123) (with positive suffixes) 1273 * @stable ICU 2.0 1274 */ 1275 UnicodeString& getNegativeSuffix(UnicodeString& result) const; 1276 1277 /** 1278 * Set the negative suffix. 1279 * 1280 * @param newValue the new value of the negative suffix to be set. 1281 * Examples: 123% 1282 * @stable ICU 2.0 1283 */ 1284 virtual void setNegativeSuffix(const UnicodeString& newValue); 1285 1286 /** 1287 * Whether to show the plus sign on positive (non-negative) numbers; for example, "+12" 1288 * 1289 * For more control over sign display, use NumberFormatter. 1290 * 1291 * @return Whether the sign is shown on positive numbers and zero. 1292 * @stable ICU 64 1293 */ 1294 UBool isSignAlwaysShown() const; 1295 1296 /** 1297 * Set whether to show the plus sign on positive (non-negative) numbers; for example, "+12". 1298 * 1299 * For more control over sign display, use NumberFormatter. 1300 * 1301 * @param value true to always show a sign; false to hide the sign on positive numbers and zero. 1302 * @stable ICU 64 1303 */ 1304 void setSignAlwaysShown(UBool value); 1305 1306 /** 1307 * Get the multiplier for use in percent, permill, etc. 1308 * For a percentage, set the suffixes to have "%" and the multiplier to be 100. 1309 * (For Arabic, use arabic percent symbol). 1310 * For a permill, set the suffixes to have "\\u2031" and the multiplier to be 1000. 1311 * 1312 * The number may also be multiplied by a power of ten; see getMultiplierScale(). 1313 * 1314 * @return the multiplier for use in percent, permill, etc. 1315 * Examples: with 100, 1.23 -> "123", and "123" -> 1.23 1316 * @stable ICU 2.0 1317 */ 1318 int32_t getMultiplier(void) const; 1319 1320 /** 1321 * Set the multiplier for use in percent, permill, etc. 1322 * For a percentage, set the suffixes to have "%" and the multiplier to be 100. 1323 * (For Arabic, use arabic percent symbol). 1324 * For a permill, set the suffixes to have "\\u2031" and the multiplier to be 1000. 1325 * 1326 * This method only supports integer multipliers. To multiply by a non-integer, pair this 1327 * method with setMultiplierScale(). 1328 * 1329 * @param newValue the new value of the multiplier for use in percent, permill, etc. 1330 * Examples: with 100, 1.23 -> "123", and "123" -> 1.23 1331 * @stable ICU 2.0 1332 */ 1333 virtual void setMultiplier(int32_t newValue); 1334 1335 /** 1336 * Gets the power of ten by which number should be multiplied before formatting, which 1337 * can be combined with setMultiplier() to multiply by any arbitrary decimal value. 1338 * 1339 * A multiplier scale of 2 corresponds to multiplication by 100, and a multiplier scale 1340 * of -2 corresponds to multiplication by 0.01. 1341 * 1342 * This method is analogous to UNUM_SCALE in getAttribute. 1343 * 1344 * @return the current value of the power-of-ten multiplier. 1345 * @stable ICU 62 1346 */ 1347 int32_t getMultiplierScale(void) const; 1348 1349 /** 1350 * Sets a power of ten by which number should be multiplied before formatting, which 1351 * can be combined with setMultiplier() to multiply by any arbitrary decimal value. 1352 * 1353 * A multiplier scale of 2 corresponds to multiplication by 100, and a multiplier scale 1354 * of -2 corresponds to multiplication by 0.01. 1355 * 1356 * For example, to multiply numbers by 0.5 before formatting, you can do: 1357 * 1358 *
1359 * df.setMultiplier(5); 1360 * df.setMultiplierScale(-1); 1361 *
1824 * There is no limit to integer digits are set 1825 * by this routine, since that is the typical end-user desire; 1826 * use setMaximumInteger if you want to set a real value. 1827 * For negative numbers, use a second pattern, separated by a semicolon 1828 *
1829 * . Example "#,#00.0#" -> 1,234.56 1830 *
1834 * . Example: "#,#00.0#;(#,#00.0#)" for negatives in parentheses. 1835 *
1866 * There is no limit to integer digits are set 1867 * by this routine, since that is the typical end-user desire; 1868 * use setMaximumInteger if you want to set a real value. 1869 * For negative numbers, use a second pattern, separated by a semicolon 1870 *
1871 * . Example "#,#00.0#" -> 1,234.56 1872 *
min
max
2081 * FormattedNumber result; 2082 * if (auto* ptr = df->toNumberFormatter(status)) { 2083 * result = ptr->formatDouble(123, status); 2084 * } 2085 *
2092 * FormattedNumber result = df 2093 * ->toNumberFormatter(status) 2094 * ->formatDouble(123, status); 2095 *
2103 * LocalizedNumberFormatter lnf; 2104 * if (auto* ptr = df->toNumberFormatter(status)) { 2105 * lnf = *ptr; 2106 * } 2107 *
2120 * . Base* polymorphic_pointer = createPolymorphicObject(); 2121 * . if (polymorphic_pointer->getDynamicClassID() == 2122 * . Derived::getStaticClassID()) ... 2123 *