|
|
|||
File indexing completed on 2026-09-12 09:28:36
0001 // © 2016 and later: Unicode, Inc. and others. 0002 // License & terms of use: http://www.unicode.org/copyright.html 0003 /* 0004 ******************************************************************************** 0005 * Copyright (C) 1997-2016, International Business Machines 0006 * Corporation and others. All Rights Reserved. 0007 ******************************************************************************** 0008 * 0009 * File DECIMFMT.H 0010 * 0011 * Modification History: 0012 * 0013 * Date Name Description 0014 * 02/19/97 aliu Converted from java. 0015 * 03/20/97 clhuang Updated per C++ implementation. 0016 * 04/03/97 aliu Rewrote parsing and formatting completely, and 0017 * cleaned up and debugged. Actually works now. 0018 * 04/17/97 aliu Changed DigitCount to int per code review. 0019 * 07/10/97 helena Made ParsePosition a class and get rid of the function 0020 * hiding problems. 0021 * 09/09/97 aliu Ported over support for exponential formats. 0022 * 07/20/98 stephen Changed documentation 0023 * 01/30/13 emmons Added Scaling methods 0024 ******************************************************************************** 0025 */ 0026 0027 #ifndef DECIMFMT_H 0028 #define DECIMFMT_H 0029 0030 #include "unicode/utypes.h" 0031 0032 #if U_SHOW_CPLUSPLUS_API 0033 0034 /** 0035 * \file 0036 * \brief C++ API: Compatibility APIs for decimal formatting. 0037 */ 0038 0039 #if !UCONFIG_NO_FORMATTING 0040 0041 #include "unicode/dcfmtsym.h" 0042 #include "unicode/numfmt.h" 0043 #include "unicode/locid.h" 0044 #include "unicode/fpositer.h" 0045 #include "unicode/stringpiece.h" 0046 #include "unicode/curramt.h" 0047 #include "unicode/enumset.h" 0048 0049 U_NAMESPACE_BEGIN 0050 0051 class CurrencyPluralInfo; 0052 class CompactDecimalFormat; 0053 0054 namespace number { 0055 class LocalizedNumberFormatter; 0056 namespace impl { 0057 class DecimalQuantity; 0058 struct DecimalFormatFields; 0059 class UFormattedNumberData; 0060 } 0061 } 0062 0063 namespace numparse::impl { 0064 class NumberParserImpl; 0065 } 0066 0067 /** 0068 * **IMPORTANT:** New users are strongly encouraged to see if 0069 * numberformatter.h fits their use case. Although not deprecated, this header 0070 * is provided for backwards compatibility only. 0071 * 0072 * DecimalFormat is a concrete subclass of NumberFormat that formats decimal 0073 * numbers. It has a variety of features designed to make it possible to parse 0074 * and format numbers in any locale, including support for Western, Arabic, or 0075 * Indic digits. It also supports different flavors of numbers, including 0076 * integers ("123"), fixed-point numbers ("123.4"), scientific notation 0077 * ("1.23E4"), percentages ("12%"), and currency amounts ("$123", "USD123", 0078 * "123 US dollars"). All of these flavors can be easily localized. 0079 * 0080 * To obtain a NumberFormat for a specific locale (including the default 0081 * locale) call one of NumberFormat's factory methods such as 0082 * createInstance(). Do not call the DecimalFormat constructors directly, unless 0083 * you know what you are doing, since the NumberFormat factory methods may 0084 * return subclasses other than DecimalFormat. 0085 * 0086 * **Example Usage** 0087 * 0088 * \code 0089 * // Normally we would have a GUI with a menu for this 0090 * int32_t locCount; 0091 * const Locale* locales = NumberFormat::getAvailableLocales(locCount); 0092 * 0093 * double myNumber = -1234.56; 0094 * UErrorCode success = U_ZERO_ERROR; 0095 * NumberFormat* form; 0096 * 0097 * // Print out a number with the localized number, currency and percent 0098 * // format for each locale. 0099 * UnicodeString countryName; 0100 * UnicodeString displayName; 0101 * UnicodeString str; 0102 * UnicodeString pattern; 0103 * Formattable fmtable; 0104 * for (int32_t j = 0; j < 3; ++j) { 0105 * cout << endl << "FORMAT " << j << endl; 0106 * for (int32_t i = 0; i < locCount; ++i) { 0107 * if (locales[i].getCountry(countryName).size() == 0) { 0108 * // skip language-only 0109 * continue; 0110 * } 0111 * switch (j) { 0112 * case 0: 0113 * form = NumberFormat::createInstance(locales[i], success ); break; 0114 * case 1: 0115 * form = NumberFormat::createCurrencyInstance(locales[i], success ); break; 0116 * default: 0117 * form = NumberFormat::createPercentInstance(locales[i], success ); break; 0118 * } 0119 * if (form) { 0120 * str.remove(); 0121 * pattern = ((DecimalFormat*)form)->toPattern(pattern); 0122 * cout << locales[i].getDisplayName(displayName) << ": " << pattern; 0123 * cout << " -> " << form->format(myNumber,str) << endl; 0124 * form->parse(form->format(myNumber,str), fmtable, success); 0125 * delete form; 0126 * } 0127 * } 0128 * } 0129 * \endcode 0130 * 0131 * **Another example use createInstance(style)** 0132 * 0133 * \code 0134 * // Print out a number using the localized number, currency, 0135 * // percent, scientific, integer, iso currency, and plural currency 0136 * // format for each locale</strong> 0137 * Locale* locale = new Locale("en", "US"); 0138 * double myNumber = 1234.56; 0139 * UErrorCode success = U_ZERO_ERROR; 0140 * UnicodeString str; 0141 * Formattable fmtable; 0142 * for (int j=NumberFormat::kNumberStyle; 0143 * j<=NumberFormat::kPluralCurrencyStyle; 0144 * ++j) { 0145 * NumberFormat* form = NumberFormat::createInstance(locale, j, success); 0146 * str.remove(); 0147 * cout << "format result " << form->format(myNumber, str) << endl; 0148 * format->parse(form->format(myNumber, str), fmtable, success); 0149 * delete form; 0150 * } 0151 * \endcode 0152 * 0153 * 0154 * <p><strong>Patterns</strong> 0155 * 0156 * <p>A DecimalFormat consists of a <em>pattern</em> and a set of 0157 * <em>symbols</em>. The pattern may be set directly using 0158 * applyPattern(), or indirectly using other API methods which 0159 * manipulate aspects of the pattern, such as the minimum number of integer 0160 * digits. The symbols are stored in a DecimalFormatSymbols 0161 * object. When using the NumberFormat factory methods, the 0162 * pattern and symbols are read from ICU's locale data. 0163 * 0164 * <p><strong>Special Pattern Characters</strong> 0165 * 0166 * <p>Many characters in a pattern are taken literally; they are matched during 0167 * parsing and output unchanged during formatting. Special characters, on the 0168 * other hand, stand for other characters, strings, or classes of characters. 0169 * For example, the '#' character is replaced by a localized digit. Often the 0170 * replacement character is the same as the pattern character; in the U.S. locale, 0171 * the ',' grouping character is replaced by ','. However, the replacement is 0172 * still happening, and if the symbols are modified, the grouping character 0173 * changes. Some special characters affect the behavior of the formatter by 0174 * their presence; for example, if the percent character is seen, then the 0175 * value is multiplied by 100 before being displayed. 0176 * 0177 * <p>To insert a special character in a pattern as a literal, that is, without 0178 * any special meaning, the character must be quoted. There are some exceptions to 0179 * this which are noted below. 0180 * 0181 * <p>The characters listed here are used in non-localized patterns. Localized 0182 * patterns use the corresponding characters taken from this formatter's 0183 * DecimalFormatSymbols object instead, and these characters lose 0184 * their special status. Two exceptions are the currency sign and quote, which 0185 * are not localized. 0186 * 0187 * <table border=0 cellspacing=3 cellpadding=0> 0188 * <tr bgcolor="#ccccff"> 0189 * <td align=left><strong>Symbol</strong> 0190 * <td align=left><strong>Location</strong> 0191 * <td align=left><strong>Localized?</strong> 0192 * <td align=left><strong>Meaning</strong> 0193 * <tr valign=top> 0194 * <td><code>0</code> 0195 * <td>Number 0196 * <td>Yes 0197 * <td>Digit 0198 * <tr valign=top bgcolor="#eeeeff"> 0199 * <td><code>1-9</code> 0200 * <td>Number 0201 * <td>Yes 0202 * <td>'1' through '9' indicate rounding. 0203 * <tr valign=top> 0204 * <td><code>\htmlonly@\endhtmlonly</code> <!--doxygen doesn't like @--> 0205 * <td>Number 0206 * <td>No 0207 * <td>Significant digit 0208 * <tr valign=top bgcolor="#eeeeff"> 0209 * <td><code>#</code> 0210 * <td>Number 0211 * <td>Yes 0212 * <td>Digit, zero shows as absent 0213 * <tr valign=top> 0214 * <td><code>.</code> 0215 * <td>Number 0216 * <td>Yes 0217 * <td>Decimal separator or monetary decimal separator 0218 * <tr valign=top bgcolor="#eeeeff"> 0219 * <td><code>-</code> 0220 * <td>Number 0221 * <td>Yes 0222 * <td>Minus sign 0223 * <tr valign=top> 0224 * <td><code>,</code> 0225 * <td>Number 0226 * <td>Yes 0227 * <td>Grouping separator 0228 * <tr valign=top bgcolor="#eeeeff"> 0229 * <td><code>E</code> 0230 * <td>Number 0231 * <td>Yes 0232 * <td>Separates mantissa and exponent in scientific notation. 0233 * <em>Need not be quoted in prefix or suffix.</em> 0234 * <tr valign=top> 0235 * <td><code>+</code> 0236 * <td>Exponent 0237 * <td>Yes 0238 * <td>Prefix positive exponents with localized plus sign. 0239 * <em>Need not be quoted in prefix or suffix.</em> 0240 * <tr valign=top bgcolor="#eeeeff"> 0241 * <td><code>;</code> 0242 * <td>Subpattern boundary 0243 * <td>Yes 0244 * <td>Separates positive and negative subpatterns 0245 * <tr valign=top> 0246 * <td><code>\%</code> 0247 * <td>Prefix or suffix 0248 * <td>Yes 0249 * <td>Multiply by 100 and show as percentage 0250 * <tr valign=top bgcolor="#eeeeff"> 0251 * <td><code>\\u2030</code> 0252 * <td>Prefix or suffix 0253 * <td>Yes 0254 * <td>Multiply by 1000 and show as per mille 0255 * <tr valign=top> 0256 * <td><code>\htmlonly¤\endhtmlonly</code> (<code>\\u00A4</code>) 0257 * <td>Prefix or suffix 0258 * <td>No 0259 * <td>Currency sign, replaced by currency symbol. If 0260 * doubled, replaced by international currency symbol. 0261 * If tripled, replaced by currency plural names, for example, 0262 * "US dollar" or "US dollars" for America. 0263 * If present in a pattern, the monetary decimal separator 0264 * is used instead of the decimal separator. 0265 * <tr valign=top bgcolor="#eeeeff"> 0266 * <td><code>'</code> 0267 * <td>Prefix or suffix 0268 * <td>No 0269 * <td>Used to quote special characters in a prefix or suffix, 0270 * for example, <code>"'#'#"</code> formats 123 to 0271 * <code>"#123"</code>. To create a single quote 0272 * itself, use two in a row: <code>"# o''clock"</code>. 0273 * <tr valign=top> 0274 * <td><code>*</code> 0275 * <td>Prefix or suffix boundary 0276 * <td>Yes 0277 * <td>Pad escape, precedes pad character 0278 * </table> 0279 * 0280 * <p>A DecimalFormat pattern contains a positive and negative 0281 * subpattern, for example, "#,##0.00;(#,##0.00)". Each subpattern has a 0282 * prefix, a numeric part, and a suffix. If there is no explicit negative 0283 * subpattern, the negative subpattern is the localized minus sign prefixed to the 0284 * positive subpattern. That is, "0.00" alone is equivalent to "0.00;-0.00". If there 0285 * is an explicit negative subpattern, it serves only to specify the negative 0286 * prefix and suffix; the number of digits, minimal digits, and other 0287 * characteristics are ignored in the negative subpattern. That means that 0288 * "#,##0.0#;(#)" has precisely the same result as "#,##0.0#;(#,##0.0#)". 0289 * 0290 * <p>The prefixes, suffixes, and various symbols used for infinity, digits, 0291 * thousands separators, decimal separators, etc. may be set to arbitrary 0292 * values, and they will appear properly during formatting. However, care must 0293 * be taken that the symbols and strings do not conflict, or parsing will be 0294 * unreliable. For example, either the positive and negative prefixes or the 0295 * suffixes must be distinct for parse() to be able 0296 * to distinguish positive from negative values. Another example is that the 0297 * decimal separator and thousands separator should be distinct characters, or 0298 * parsing will be impossible. 0299 * 0300 * <p>The <em>grouping separator</em> is a character that separates clusters of 0301 * integer digits to make large numbers more legible. It commonly used for 0302 * thousands, but in some locales it separates ten-thousands. The <em>grouping 0303 * size</em> is the number of digits between the grouping separators, such as 3 0304 * for "100,000,000" or 4 for "1 0000 0000". There are actually two different 0305 * grouping sizes: One used for the least significant integer digits, the 0306 * <em>primary grouping size</em>, and one used for all others, the 0307 * <em>secondary grouping size</em>. In most locales these are the same, but 0308 * sometimes they are different. For example, if the primary grouping interval 0309 * is 3, and the secondary is 2, then this corresponds to the pattern 0310 * "#,##,##0", and the number 123456789 is formatted as "12,34,56,789". If a 0311 * pattern contains multiple grouping separators, the interval between the last 0312 * one and the end of the integer defines the primary grouping size, and the 0313 * interval between the last two defines the secondary grouping size. All others 0314 * are ignored, so "#,##,###,####" == "###,###,####" == "##,#,###,####". 0315 * 0316 * <p>Illegal patterns, such as "#.#.#" or "#.###,###", will cause 0317 * DecimalFormat to set a failing UErrorCode. 0318 * 0319 * <p><strong>Pattern BNF</strong> 0320 * 0321 * <pre> 0322 * pattern := subpattern (';' subpattern)? 0323 * subpattern := prefix? number exponent? suffix? 0324 * number := (integer ('.' fraction)?) | sigDigits 0325 * prefix := '\\u0000'..'\\uFFFD' - specialCharacters 0326 * suffix := '\\u0000'..'\\uFFFD' - specialCharacters 0327 * integer := '#'* '0'* '0' 0328 * fraction := '0'* '#'* 0329 * sigDigits := '#'* '@' '@'* '#'* 0330 * exponent := 'E' '+'? '0'* '0' 0331 * padSpec := '*' padChar 0332 * padChar := '\\u0000'..'\\uFFFD' - quote 0333 * 0334 * Notation: 0335 * X* 0 or more instances of X 0336 * X? 0 or 1 instances of X 0337 * X|Y either X or Y 0338 * C..D any character from C up to D, inclusive 0339 * S-T characters in S, except those in T 0340 * </pre> 0341 * The first subpattern is for positive numbers. The second (optional) 0342 * subpattern is for negative numbers. 0343 * 0344 * <p>Not indicated in the BNF syntax above: 0345 * 0346 * <ul><li>The grouping separator ',' can occur inside the integer and 0347 * sigDigits elements, between any two pattern characters of that 0348 * element, as long as the integer or sigDigits element is not 0349 * followed by the exponent element. 0350 * 0351 * <li>Two grouping intervals are recognized: That between the 0352 * decimal point and the first grouping symbol, and that 0353 * between the first and second grouping symbols. These 0354 * intervals are identical in most locales, but in some 0355 * locales they differ. For example, the pattern 0356 * "#,##,###" formats the number 123456789 as 0357 * "12,34,56,789".</li> 0358 * 0359 * <li>The pad specifier <code>padSpec</code> may appear before the prefix, 0360 * after the prefix, before the suffix, after the suffix, or not at all. 0361 * 0362 * <li>In place of '0', the digits '1' through '9' may be used to 0363 * indicate a rounding increment. 0364 * </ul> 0365 * 0366 * <p><strong>Parsing</strong> 0367 * 0368 * <p>DecimalFormat parses all Unicode characters that represent 0369 * decimal digits, as defined by u_charDigitValue(). In addition, 0370 * DecimalFormat also recognizes as digits the ten consecutive 0371 * characters starting with the localized zero digit defined in the 0372 * DecimalFormatSymbols object. During formatting, the 0373 * DecimalFormatSymbols-based digits are output. 0374 * 0375 * <p>During parsing, grouping separators are ignored if in lenient mode; 0376 * otherwise, if present, they must be in appropriate positions. 0377 * 0378 * <p>For currency parsing, the formatter is able to parse every currency 0379 * style formats no matter which style the formatter is constructed with. 0380 * For example, a formatter instance gotten from 0381 * NumberFormat.getInstance(ULocale, NumberFormat.CURRENCYSTYLE) can parse 0382 * formats such as "USD1.00" and "3.00 US dollars". 0383 * 0384 * <p>If parse(UnicodeString&,Formattable&,ParsePosition&) 0385 * fails to parse a string, it leaves the parse position unchanged. 0386 * The convenience method parse(UnicodeString&,Formattable&,UErrorCode&) 0387 * indicates parse failure by setting a failing 0388 * UErrorCode. 0389 * 0390 * <p><strong>Formatting</strong> 0391 * 0392 * <p>Formatting is guided by several parameters, all of which can be 0393 * specified either using a pattern or using the API. The following 0394 * description applies to formats that do not use <a href="#sci">scientific 0395 * notation</a> or <a href="#sigdig">significant digits</a>. 0396 * 0397 * <ul><li>If the number of actual integer digits exceeds the 0398 * <em>maximum integer digits</em>, then only the least significant 0399 * digits are shown. For example, 1997 is formatted as "97" if the 0400 * maximum integer digits is set to 2. 0401 * 0402 * <li>If the number of actual integer digits is less than the 0403 * <em>minimum integer digits</em>, then leading zeros are added. For 0404 * example, 1997 is formatted as "01997" if the minimum integer digits 0405 * is set to 5. 0406 * 0407 * <li>If the number of actual fraction digits exceeds the <em>maximum 0408 * fraction digits</em>, then rounding is performed to the 0409 * maximum fraction digits. For example, 0.125 is formatted as "0.12" 0410 * if the maximum fraction digits is 2. This behavior can be changed 0411 * by specifying a rounding increment and/or a rounding mode. 0412 * 0413 * <li>If the number of actual fraction digits is less than the 0414 * <em>minimum fraction digits</em>, then trailing zeros are added. 0415 * For example, 0.125 is formatted as "0.1250" if the minimum fraction 0416 * digits is set to 4. 0417 * 0418 * <li>Trailing fractional zeros are not displayed if they occur 0419 * <em>j</em> positions after the decimal, where <em>j</em> is less 0420 * than the maximum fraction digits. For example, 0.10004 is 0421 * formatted as "0.1" if the maximum fraction digits is four or less. 0422 * </ul> 0423 * 0424 * <p><strong>Special Values</strong> 0425 * 0426 * <p><code>NaN</code> is represented as a single character, typically 0427 * <code>\\uFFFD</code>. This character is determined by the 0428 * DecimalFormatSymbols object. This is the only value for which 0429 * the prefixes and suffixes are not used. 0430 * 0431 * <p>Infinity is represented as a single character, typically 0432 * <code>\\u221E</code>, with the positive or negative prefixes and suffixes 0433 * applied. The infinity character is determined by the 0434 * DecimalFormatSymbols object. 0435 * 0436 * <a name="sci"><strong>Scientific Notation</strong></a> 0437 * 0438 * <p>Numbers in scientific notation are expressed as the product of a mantissa 0439 * and a power of ten, for example, 1234 can be expressed as 1.234 x 10<sup>3</sup>. The 0440 * mantissa is typically in the half-open interval [1.0, 10.0) or sometimes [0.0, 1.0), 0441 * but it need not be. DecimalFormat supports arbitrary mantissas. 0442 * DecimalFormat can be instructed to use scientific 0443 * notation through the API or through the pattern. In a pattern, the exponent 0444 * character immediately followed by one or more digit characters indicates 0445 * scientific notation. Example: "0.###E0" formats the number 1234 as 0446 * "1.234E3". 0447 * 0448 * <ul> 0449 * <li>The number of digit characters after the exponent character gives the 0450 * minimum exponent digit count. There is no maximum. Negative exponents are 0451 * formatted using the localized minus sign, <em>not</em> the prefix and suffix 0452 * from the pattern. This allows patterns such as "0.###E0 m/s". To prefix 0453 * positive exponents with a localized plus sign, specify '+' between the 0454 * exponent and the digits: "0.###E+0" will produce formats "1E+1", "1E+0", 0455 * "1E-1", etc. (In localized patterns, use the localized plus sign rather than 0456 * '+'.) 0457 * 0458 * <li>The minimum number of integer digits is achieved by adjusting the 0459 * exponent. Example: 0.00123 formatted with "00.###E0" yields "12.3E-4". This 0460 * only happens if there is no maximum number of integer digits. If there is a 0461 * maximum, then the minimum number of integer digits is fixed at one. 0462 * 0463 * <li>The maximum number of integer digits, if present, specifies the exponent 0464 * grouping. The most common use of this is to generate <em>engineering 0465 * notation</em>, in which the exponent is a multiple of three, e.g., 0466 * "##0.###E0". The number 12345 is formatted using "##0.####E0" as "12.345E3". 0467 * 0468 * <li>When using scientific notation, the formatter controls the 0469 * digit counts using significant digits logic. The maximum number of 0470 * significant digits limits the total number of integer and fraction 0471 * digits that will be shown in the mantissa; it does not affect 0472 * parsing. For example, 12345 formatted with "##0.##E0" is "12.3E3". 0473 * See the section on significant digits for more details. 0474 * 0475 * <li>The number of significant digits shown is determined as 0476 * follows: If areSignificantDigitsUsed() returns false, then the 0477 * minimum number of significant digits shown is one, and the maximum 0478 * number of significant digits shown is the sum of the <em>minimum 0479 * integer</em> and <em>maximum fraction</em> digits, and is 0480 * unaffected by the maximum integer digits. If this sum is zero, 0481 * then all significant digits are shown. If 0482 * areSignificantDigitsUsed() returns true, then the significant digit 0483 * counts are specified by getMinimumSignificantDigits() and 0484 * getMaximumSignificantDigits(). In this case, the number of 0485 * integer digits is fixed at one, and there is no exponent grouping. 0486 * 0487 * <li>Exponential patterns may not contain grouping separators. 0488 * </ul> 0489 * 0490 * <a name="sigdig"><strong>Significant Digits</strong></a> 0491 * 0492 * <code>DecimalFormat</code> has two ways of controlling how many 0493 * digits are shows: (a) significant digits counts, or (b) integer and 0494 * fraction digit counts. Integer and fraction digit counts are 0495 * described above. When a formatter is using significant digits 0496 * counts, the number of integer and fraction digits is not specified 0497 * directly, and the formatter settings for these counts are ignored. 0498 * Instead, the formatter uses however many integer and fraction 0499 * digits are required to display the specified number of significant 0500 * digits. Examples: 0501 * 0502 * <table border=0 cellspacing=3 cellpadding=0> 0503 * <tr bgcolor="#ccccff"> 0504 * <td align=left>Pattern 0505 * <td align=left>Minimum significant digits 0506 * <td align=left>Maximum significant digits 0507 * <td align=left>Number 0508 * <td align=left>Output of format() 0509 * <tr valign=top> 0510 * <td><code>\@\@\@</code> 0511 * <td>3 0512 * <td>3 0513 * <td>12345 0514 * <td><code>12300</code> 0515 * <tr valign=top bgcolor="#eeeeff"> 0516 * <td><code>\@\@\@</code> 0517 * <td>3 0518 * <td>3 0519 * <td>0.12345 0520 * <td><code>0.123</code> 0521 * <tr valign=top> 0522 * <td><code>\@\@##</code> 0523 * <td>2 0524 * <td>4 0525 * <td>3.14159 0526 * <td><code>3.142</code> 0527 * <tr valign=top bgcolor="#eeeeff"> 0528 * <td><code>\@\@##</code> 0529 * <td>2 0530 * <td>4 0531 * <td>1.23004 0532 * <td><code>1.23</code> 0533 * </table> 0534 * 0535 * <ul> 0536 * <li>Significant digit counts may be expressed using patterns that 0537 * specify a minimum and maximum number of significant digits. These 0538 * are indicated by the <code>'@'</code> and <code>'#'</code> 0539 * characters. The minimum number of significant digits is the number 0540 * of <code>'@'</code> characters. The maximum number of significant 0541 * digits is the number of <code>'@'</code> characters plus the number 0542 * of <code>'#'</code> characters following on the right. For 0543 * example, the pattern <code>"@@@"</code> indicates exactly 3 0544 * significant digits. The pattern <code>"@##"</code> indicates from 0545 * 1 to 3 significant digits. Trailing zero digits to the right of 0546 * the decimal separator are suppressed after the minimum number of 0547 * significant digits have been shown. For example, the pattern 0548 * <code>"@##"</code> formats the number 0.1203 as 0549 * <code>"0.12"</code>. 0550 * 0551 * <li>If a pattern uses significant digits, it may not contain a 0552 * decimal separator, nor the <code>'0'</code> pattern character. 0553 * Patterns such as <code>"@00"</code> or <code>"@.###"</code> are 0554 * disallowed. 0555 * 0556 * <li>Any number of <code>'#'</code> characters may be prepended to 0557 * the left of the leftmost <code>'@'</code> character. These have no 0558 * effect on the minimum and maximum significant digits counts, but 0559 * may be used to position grouping separators. For example, 0560 * <code>"#,#@#"</code> indicates a minimum of one significant digits, 0561 * a maximum of two significant digits, and a grouping size of three. 0562 * 0563 * <li>In order to enable significant digits formatting, use a pattern 0564 * containing the <code>'@'</code> pattern character. Alternatively, 0565 * call setSignificantDigitsUsed(true). 0566 * 0567 * <li>In order to disable significant digits formatting, use a 0568 * pattern that does not contain the <code>'@'</code> pattern 0569 * character. Alternatively, call setSignificantDigitsUsed(false). 0570 * 0571 * <li>The number of significant digits has no effect on parsing. 0572 * 0573 * <li>Significant digits may be used together with exponential notation. Such 0574 * patterns are equivalent to a normal exponential pattern with a minimum and 0575 * maximum integer digit count of one, a minimum fraction digit count of 0576 * <code>getMinimumSignificantDigits() - 1</code>, and a maximum fraction digit 0577 * count of <code>getMaximumSignificantDigits() - 1</code>. For example, the 0578 * pattern <code>"@@###E0"</code> is equivalent to <code>"0.0###E0"</code>. 0579 * 0580 * <li>If significant digits are in use, then the integer and fraction 0581 * digit counts, as set via the API, are ignored. If significant 0582 * digits are not in use, then the significant digit counts, as set via 0583 * the API, are ignored. 0584 * 0585 * </ul> 0586 * 0587 * <p><strong>Padding</strong> 0588 * 0589 * <p>DecimalFormat supports padding the result of 0590 * format() to a specific width. Padding may be specified either 0591 * through the API or through the pattern syntax. In a pattern the pad escape 0592 * character, followed by a single pad character, causes padding to be parsed 0593 * and formatted. The pad escape character is '*' in unlocalized patterns, and 0594 * can be localized using DecimalFormatSymbols::setSymbol() with a 0595 * DecimalFormatSymbols::kPadEscapeSymbol 0596 * selector. For example, <code>"$*x#,##0.00"</code> formats 123 to 0597 * <code>"$xx123.00"</code>, and 1234 to <code>"$1,234.00"</code>. 0598 * 0599 * <ul> 0600 * <li>When padding is in effect, the width of the positive subpattern, 0601 * including prefix and suffix, determines the format width. For example, in 0602 * the pattern <code>"* #0 o''clock"</code>, the format width is 10. 0603 * 0604 * <li>The width is counted in 16-bit code units (char16_ts). 0605 * 0606 * <li>Some parameters which usually do not matter have meaning when padding is 0607 * used, because the pattern width is significant with padding. In the pattern 0608 * "* ##,##,#,##0.##", the format width is 14. The initial characters "##,##," 0609 * do not affect the grouping size or maximum integer digits, but they do affect 0610 * the format width. 0611 * 0612 * <li>Padding may be inserted at one of four locations: before the prefix, 0613 * after the prefix, before the suffix, or after the suffix. If padding is 0614 * specified in any other location, applyPattern() 0615 * sets a failing UErrorCode. If there is no prefix, 0616 * before the prefix and after the prefix are equivalent, likewise for the 0617 * suffix. 0618 * 0619 * <li>When specified in a pattern, the 32-bit code point immediately 0620 * following the pad escape is the pad character. This may be any character, 0621 * including a special pattern character. That is, the pad escape 0622 * <em>escapes</em> the following character. If there is no character after 0623 * the pad escape, then the pattern is illegal. 0624 * 0625 * </ul> 0626 * 0627 * <p><strong>Rounding</strong> 0628 * 0629 * <p>DecimalFormat supports rounding to a specific increment. For 0630 * example, 1230 rounded to the nearest 50 is 1250. 1.234 rounded to the 0631 * nearest 0.65 is 1.3. The rounding increment may be specified through the API 0632 * or in a pattern. To specify a rounding increment in a pattern, include the 0633 * increment in the pattern itself. "#,#50" specifies a rounding increment of 0634 * 50. "#,##0.05" specifies a rounding increment of 0.05. 0635 * 0636 * <p>In the absence of an explicit rounding increment numbers are 0637 * rounded to their formatted width. 0638 * 0639 * <ul> 0640 * <li>Rounding only affects the string produced by formatting. It does 0641 * not affect parsing or change any numerical values. 0642 * 0643 * <li>A <em>rounding mode</em> determines how values are rounded; see 0644 * DecimalFormat::ERoundingMode. The default rounding mode is 0645 * DecimalFormat::kRoundHalfEven. The rounding mode can only be set 0646 * through the API; it can not be set with a pattern. 0647 * 0648 * <li>Some locales use rounding in their currency formats to reflect the 0649 * smallest currency denomination. 0650 * 0651 * <li>In a pattern, digits '1' through '9' specify rounding, but otherwise 0652 * behave identically to digit '0'. 0653 * </ul> 0654 * 0655 * <p><strong>Synchronization</strong> 0656 * 0657 * <p>DecimalFormat objects are not synchronized. Multiple 0658 * threads should not access one formatter concurrently. 0659 * 0660 * <p><strong>Subclassing</strong> 0661 * 0662 * <p><em>User subclasses are not supported.</em> While clients may write 0663 * subclasses, such code will not necessarily work and will not be 0664 * guaranteed to work stably from release to release. 0665 */ 0666 class U_I18N_API DecimalFormat : public NumberFormat { 0667 public: 0668 /** 0669 * Pad position. 0670 * @stable ICU 2.4 0671 */ 0672 enum EPadPosition { 0673 kPadBeforePrefix, kPadAfterPrefix, kPadBeforeSuffix, kPadAfterSuffix 0674 }; 0675 0676 /** 0677 * Create a DecimalFormat using the default pattern and symbols 0678 * for the default locale. This is a convenient way to obtain a 0679 * DecimalFormat when internationalization is not the main concern. 0680 * <P> 0681 * To obtain standard formats for a given locale, use the factory methods 0682 * on NumberFormat such as createInstance. These factories will 0683 * return the most appropriate sub-class of NumberFormat for a given 0684 * locale. 0685 * <p> 0686 * <strong>NOTE:</strong> New users are strongly encouraged to use 0687 * #icu::number::NumberFormatter instead of DecimalFormat. 0688 * @param status Output param set to success/failure code. If the 0689 * pattern is invalid this will be set to a failure code. 0690 * @stable ICU 2.0 0691 */ 0692 DecimalFormat(UErrorCode& status); 0693 0694 /** 0695 * Create a DecimalFormat from the given pattern and the symbols 0696 * for the default locale. This is a convenient way to obtain a 0697 * DecimalFormat when internationalization is not the main concern. 0698 * <P> 0699 * To obtain standard formats for a given locale, use the factory methods 0700 * on NumberFormat such as createInstance. These factories will 0701 * return the most appropriate sub-class of NumberFormat for a given 0702 * locale. 0703 * <p> 0704 * <strong>NOTE:</strong> New users are strongly encouraged to use 0705 * #icu::number::NumberFormatter instead of DecimalFormat. 0706 * @param pattern A non-localized pattern string. 0707 * @param status Output param set to success/failure code. If the 0708 * pattern is invalid this will be set to a failure code. 0709 * @stable ICU 2.0 0710 */ 0711 DecimalFormat(const UnicodeString& pattern, UErrorCode& status); 0712 0713 /** 0714 * Create a DecimalFormat from the given pattern and symbols. 0715 * Use this constructor when you need to completely customize the 0716 * behavior of the format. 0717 * <P> 0718 * To obtain standard formats for a given 0719 * locale, use the factory methods on NumberFormat such as 0720 * createInstance or createCurrencyInstance. If you need only minor adjustments 0721 * to a standard format, you can modify the format returned by 0722 * a NumberFormat factory method. 0723 * <p> 0724 * <strong>NOTE:</strong> New users are strongly encouraged to use 0725 * #icu::number::NumberFormatter instead of DecimalFormat. 0726 * 0727 * @param pattern a non-localized pattern string 0728 * @param symbolsToAdopt the set of symbols to be used. The caller should not 0729 * delete this object after making this call. 0730 * @param status Output param set to success/failure code. If the 0731 * pattern is invalid this will be set to a failure code. 0732 * @stable ICU 2.0 0733 */ 0734 DecimalFormat(const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt, UErrorCode& status); 0735 0736 #ifndef U_HIDE_INTERNAL_API 0737 0738 /** 0739 * This API is for ICU use only. 0740 * Create a DecimalFormat from the given pattern, symbols, and style. 0741 * 0742 * @param pattern a non-localized pattern string 0743 * @param symbolsToAdopt the set of symbols to be used. The caller should not 0744 * delete this object after making this call. 0745 * @param style style of decimal format 0746 * @param status Output param set to success/failure code. If the 0747 * pattern is invalid this will be set to a failure code. 0748 * @internal 0749 */ 0750 DecimalFormat(const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt, 0751 UNumberFormatStyle style, UErrorCode& status); 0752 0753 #if UCONFIG_HAVE_PARSEALLINPUT 0754 0755 /** 0756 * @internal 0757 */ 0758 void setParseAllInput(UNumberFormatAttributeValue value); 0759 0760 #endif 0761 0762 #endif /* U_HIDE_INTERNAL_API */ 0763 0764 private: 0765 0766 /** 0767 * Internal constructor for DecimalFormat; sets up internal fields. All public constructors should 0768 * call this constructor. 0769 */ 0770 DecimalFormat(const DecimalFormatSymbols* symbolsToAdopt, UErrorCode& status); 0771 0772 public: 0773 0774 /** 0775 * Set an integer attribute on this DecimalFormat. 0776 * May return U_UNSUPPORTED_ERROR if this instance does not support 0777 * the specified attribute. 0778 * @param attr the attribute to set 0779 * @param newValue new value 0780 * @param status the error type 0781 * @return *this - for chaining (example: format.setAttribute(...).setAttribute(...) ) 0782 * @stable ICU 51 0783 */ 0784 virtual DecimalFormat& setAttribute(UNumberFormatAttribute attr, int32_t newValue, UErrorCode& status); 0785 0786 /** 0787 * Get an integer 0788 * May return U_UNSUPPORTED_ERROR if this instance does not support 0789 * the specified attribute. 0790 * @param attr the attribute to set 0791 * @param status the error type 0792 * @return the attribute value. Undefined if there is an error. 0793 * @stable ICU 51 0794 */ 0795 virtual int32_t getAttribute(UNumberFormatAttribute attr, UErrorCode& status) const; 0796 0797 0798 /** 0799 * Set whether or not grouping will be used in this format. 0800 * @param newValue True, grouping will be used in this format. 0801 * @see getGroupingUsed 0802 * @stable ICU 53 0803 */ 0804 void setGroupingUsed(UBool newValue) override; 0805 0806 /** 0807 * Sets whether or not numbers should be parsed as integers only. 0808 * @param value set True, this format will parse numbers as integers 0809 * only. 0810 * @see isParseIntegerOnly 0811 * @stable ICU 53 0812 */ 0813 void setParseIntegerOnly(UBool value) override; 0814 0815 /** 0816 * Sets whether lenient parsing should be enabled (it is off by default). 0817 * 0818 * @param enable \c true if lenient parsing should be used, 0819 * \c false otherwise. 0820 * @stable ICU 4.8 0821 */ 0822 void setLenient(UBool enable) override; 0823 0824 /** 0825 * Create a DecimalFormat from the given pattern and symbols. 0826 * Use this constructor when you need to completely customize the 0827 * behavior of the format. 0828 * <P> 0829 * To obtain standard formats for a given 0830 * locale, use the factory methods on NumberFormat such as 0831 * createInstance or createCurrencyInstance. If you need only minor adjustments 0832 * to a standard format, you can modify the format returned by 0833 * a NumberFormat factory method. 0834 * <p> 0835 * <strong>NOTE:</strong> New users are strongly encouraged to use 0836 * #icu::number::NumberFormatter instead of DecimalFormat. 0837 * 0838 * @param pattern a non-localized pattern string 0839 * @param symbolsToAdopt the set of symbols to be used. The caller should not 0840 * delete this object after making this call. 0841 * @param parseError Output param to receive errors occurred during parsing 0842 * @param status Output param set to success/failure code. If the 0843 * pattern is invalid this will be set to a failure code. 0844 * @stable ICU 2.0 0845 */ 0846 DecimalFormat(const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt, 0847 UParseError& parseError, UErrorCode& status); 0848 0849 /** 0850 * Create a DecimalFormat from the given pattern and symbols. 0851 * Use this constructor when you need to completely customize the 0852 * behavior of the format. 0853 * <P> 0854 * To obtain standard formats for a given 0855 * locale, use the factory methods on NumberFormat such as 0856 * createInstance or createCurrencyInstance. If you need only minor adjustments 0857 * to a standard format, you can modify the format returned by 0858 * a NumberFormat factory method. 0859 * <p> 0860 * <strong>NOTE:</strong> New users are strongly encouraged to use 0861 * #icu::number::NumberFormatter instead of DecimalFormat. 0862 * 0863 * @param pattern a non-localized pattern string 0864 * @param symbols the set of symbols to be used 0865 * @param status Output param set to success/failure code. If the 0866 * pattern is invalid this will be set to a failure code. 0867 * @stable ICU 2.0 0868 */ 0869 DecimalFormat(const UnicodeString& pattern, const DecimalFormatSymbols& symbols, UErrorCode& status); 0870 0871 /** 0872 * Copy constructor. 0873 * 0874 * @param source the DecimalFormat object to be copied from. 0875 * @stable ICU 2.0 0876 */ 0877 DecimalFormat(const DecimalFormat& source); 0878 0879 /** 0880 * Assignment operator. 0881 * 0882 * @param rhs the DecimalFormat object to be copied. 0883 * @stable ICU 2.0 0884 */ 0885 DecimalFormat& operator=(const DecimalFormat& rhs); 0886 0887 /** 0888 * Destructor. 0889 * @stable ICU 2.0 0890 */ 0891 ~DecimalFormat() override; 0892 0893 /** 0894 * Clone this Format object polymorphically. The caller owns the 0895 * result and should delete it when done. 0896 * 0897 * @return a polymorphic copy of this DecimalFormat. 0898 * @stable ICU 2.0 0899 */ 0900 DecimalFormat* clone() const override; 0901 0902 /** 0903 * Return true if the given Format objects are semantically equal. 0904 * Objects of different subclasses are considered unequal. 0905 * 0906 * @param other the object to be compared with. 0907 * @return true if the given Format objects are semantically equal. 0908 * @stable ICU 2.0 0909 */ 0910 bool operator==(const Format& other) const override; 0911 0912 0913 using NumberFormat::format; 0914 0915 /** 0916 * Format a double or long number using base-10 representation. 0917 * 0918 * @param number The value to be formatted. 0919 * @param appendTo Output parameter to receive result. 0920 * Result is appended to existing contents. 0921 * @param pos On input: an alignment field, if desired. 0922 * On output: the offsets of the alignment field. 0923 * @return Reference to 'appendTo' parameter. 0924 * @stable ICU 2.0 0925 */ 0926 UnicodeString& format(double number, UnicodeString& appendTo, FieldPosition& pos) const override; 0927 0928 #ifndef U_HIDE_INTERNAL_API 0929 /** 0930 * Format a double or long number using base-10 representation. 0931 * 0932 * @param number The value to be formatted. 0933 * @param appendTo Output parameter to receive result. 0934 * Result is appended to existing contents. 0935 * @param pos On input: an alignment field, if desired. 0936 * On output: the offsets of the alignment field. 0937 * @param status 0938 * @return Reference to 'appendTo' parameter. 0939 * @internal 0940 */ 0941 UnicodeString& format(double number, UnicodeString& appendTo, FieldPosition& pos, 0942 UErrorCode& status) const override; 0943 #endif /* U_HIDE_INTERNAL_API */ 0944 0945 /** 0946 * Format a double or long number using base-10 representation. 0947 * 0948 * @param number The value to be formatted. 0949 * @param appendTo Output parameter to receive result. 0950 * Result is appended to existing contents. 0951 * @param posIter On return, can be used to iterate over positions 0952 * of fields generated by this format call. 0953 * Can be nullptr. 0954 * @param status Output param filled with success/failure status. 0955 * @return Reference to 'appendTo' parameter. 0956 * @stable ICU 4.4 0957 */ 0958 UnicodeString& format(double number, UnicodeString& appendTo, FieldPositionIterator* posIter, 0959 UErrorCode& status) const override; 0960 0961 /** 0962 * Format a long number using base-10 representation. 0963 * 0964 * @param number The value to be formatted. 0965 * @param appendTo Output parameter to receive result. 0966 * Result is appended to existing contents. 0967 * @param pos On input: an alignment field, if desired. 0968 * On output: the offsets of the alignment field. 0969 * @return Reference to 'appendTo' parameter. 0970 * @stable ICU 2.0 0971 */ 0972 UnicodeString& format(int32_t number, UnicodeString& appendTo, FieldPosition& pos) const override; 0973 0974 #ifndef U_HIDE_INTERNAL_API 0975 /** 0976 * Format a long number using base-10 representation. 0977 * 0978 * @param number The value to be formatted. 0979 * @param appendTo Output parameter to receive result. 0980 * Result is appended to existing contents. 0981 * @param pos On input: an alignment field, if desired. 0982 * On output: the offsets of the alignment field. 0983 * @param status Output param filled with success/failure status. 0984 * @return Reference to 'appendTo' parameter. 0985 * @internal 0986 */ 0987 UnicodeString& format(int32_t number, UnicodeString& appendTo, FieldPosition& pos, 0988 UErrorCode& status) const override; 0989 #endif /* U_HIDE_INTERNAL_API */ 0990 0991 /** 0992 * Format a long number using base-10 representation. 0993 * 0994 * @param number The value to be formatted. 0995 * @param appendTo Output parameter to receive result. 0996 * Result is appended to existing contents. 0997 * @param posIter On return, can be used to iterate over positions 0998 * of fields generated by this format call. 0999 * Can be nullptr. 1000 * @param status Output param filled with success/failure status. 1001 * @return Reference to 'appendTo' parameter. 1002 * @stable ICU 4.4 1003 */ 1004 UnicodeString& format(int32_t number, UnicodeString& appendTo, FieldPositionIterator* posIter, 1005 UErrorCode& status) const override; 1006 1007 /** 1008 * Format an int64 number using base-10 representation. 1009 * 1010 * @param number The value to be formatted. 1011 * @param appendTo Output parameter to receive result. 1012 * Result is appended to existing contents. 1013 * @param pos On input: an alignment field, if desired. 1014 * On output: the offsets of the alignment field. 1015 * @return Reference to 'appendTo' parameter. 1016 * @stable ICU 2.8 1017 */ 1018 UnicodeString& format(int64_t number, UnicodeString& appendTo, FieldPosition& pos) const override; 1019 1020 #ifndef U_HIDE_INTERNAL_API 1021 /** 1022 * Format an int64 number using base-10 representation. 1023 * 1024 * @param number The value to be formatted. 1025 * @param appendTo Output parameter to receive result. 1026 * Result is appended to existing contents. 1027 * @param pos On input: an alignment field, if desired. 1028 * On output: the offsets of the alignment field. 1029 * @param status Output param filled with success/failure status. 1030 * @return Reference to 'appendTo' parameter. 1031 * @internal 1032 */ 1033 UnicodeString& format(int64_t number, UnicodeString& appendTo, FieldPosition& pos, 1034 UErrorCode& status) const override; 1035 #endif /* U_HIDE_INTERNAL_API */ 1036 1037 /** 1038 * Format an int64 number using base-10 representation. 1039 * 1040 * @param number The value to be formatted. 1041 * @param appendTo Output parameter to receive result. 1042 * Result is appended to existing contents. 1043 * @param posIter On return, can be used to iterate over positions 1044 * of fields generated by this format call. 1045 * Can be nullptr. 1046 * @param status Output param filled with success/failure status. 1047 * @return Reference to 'appendTo' parameter. 1048 * @stable ICU 4.4 1049 */ 1050 UnicodeString& format(int64_t number, UnicodeString& appendTo, FieldPositionIterator* posIter, 1051 UErrorCode& status) const override; 1052 1053 /** 1054 * Format a decimal number. 1055 * The syntax of the unformatted number is a "numeric string" 1056 * as defined in the Decimal Arithmetic Specification, available at 1057 * http://speleotrove.com/decimal 1058 * 1059 * @param number The unformatted number, as a string. 1060 * @param appendTo Output parameter to receive result. 1061 * Result is appended to existing contents. 1062 * @param posIter On return, can be used to iterate over positions 1063 * of fields generated by this format call. 1064 * Can be nullptr. 1065 * @param status Output param filled with success/failure status. 1066 * @return Reference to 'appendTo' parameter. 1067 * @stable ICU 4.4 1068 */ 1069 UnicodeString& format(StringPiece number, UnicodeString& appendTo, FieldPositionIterator* posIter, 1070 UErrorCode& status) const override; 1071 1072 #ifndef U_HIDE_INTERNAL_API 1073 1074 /** 1075 * Format a decimal number. 1076 * The number is a DecimalQuantity wrapper onto a floating point decimal number. 1077 * The default implementation in NumberFormat converts the decimal number 1078 * to a double and formats that. 1079 * 1080 * @param number The number, a DecimalQuantity format Decimal Floating Point. 1081 * @param appendTo Output parameter to receive result. 1082 * Result is appended to existing contents. 1083 * @param posIter On return, can be used to iterate over positions 1084 * of fields generated by this format call. 1085 * @param status Output param filled with success/failure status. 1086 * @return Reference to 'appendTo' parameter. 1087 * @internal 1088 */ 1089 UnicodeString& format(const number::impl::DecimalQuantity& number, UnicodeString& appendTo, 1090 FieldPositionIterator* posIter, UErrorCode& status) const override; 1091 1092 /** 1093 * Format a decimal number. 1094 * The number is a DecimalQuantity wrapper onto a floating point decimal number. 1095 * The default implementation in NumberFormat converts the decimal number 1096 * to a double and formats that. 1097 * 1098 * @param number The number, a DecimalQuantity format Decimal Floating Point. 1099 * @param appendTo Output parameter to receive result. 1100 * Result is appended to existing contents. 1101 * @param pos On input: an alignment field, if desired. 1102 * On output: the offsets of the alignment field. 1103 * @param status Output param filled with success/failure status. 1104 * @return Reference to 'appendTo' parameter. 1105 * @internal 1106 */ 1107 UnicodeString& format(const number::impl::DecimalQuantity& number, UnicodeString& appendTo, 1108 FieldPosition& pos, UErrorCode& status) const override; 1109 1110 #endif // U_HIDE_INTERNAL_API 1111 1112 using NumberFormat::parse; 1113 1114 /** 1115 * Parse the given string using this object's choices. The method 1116 * does string comparisons to try to find an optimal match. 1117 * If no object can be parsed, index is unchanged, and nullptr is 1118 * returned. The result is returned as the most parsimonious 1119 * type of Formattable that will accommodate all of the 1120 * necessary precision. For example, if the result is exactly 12, 1121 * it will be returned as a long. However, if it is 1.5, it will 1122 * be returned as a double. 1123 * 1124 * @param text The text to be parsed. 1125 * @param result Formattable to be set to the parse result. 1126 * If parse fails, return contents are undefined. 1127 * @param parsePosition The position to start parsing at on input. 1128 * On output, moved to after the last successfully 1129 * parse character. On parse failure, does not change. 1130 * @see Formattable 1131 * @stable ICU 2.0 1132 */ 1133 void parse(const UnicodeString& text, Formattable& result, 1134 ParsePosition& parsePosition) const override; 1135 1136 /** 1137 * Parses text from the given string as a currency amount. Unlike 1138 * the parse() method, this method will attempt to parse a generic 1139 * currency name, searching for a match of this object's locale's 1140 * currency display names, or for a 3-letter ISO currency code. 1141 * This method will fail if this format is not a currency format, 1142 * that is, if it does not contain the currency pattern symbol 1143 * (U+00A4) in its prefix or suffix. 1144 * 1145 * @param text the string to parse 1146 * @param pos input-output position; on input, the position within text 1147 * to match; must have 0 <= pos.getIndex() < text.length(); 1148 * on output, the position after the last matched character. 1149 * If the parse fails, the position in unchanged upon output. 1150 * @return if parse succeeds, a pointer to a newly-created CurrencyAmount 1151 * object (owned by the caller) containing information about 1152 * the parsed currency; if parse fails, this is nullptr. 1153 * @stable ICU 49 1154 */ 1155 CurrencyAmount* parseCurrency(const UnicodeString& text, ParsePosition& pos) const override; 1156 1157 /** 1158 * Returns the decimal format symbols, which is generally not changed 1159 * by the programmer or user. 1160 * @return desired DecimalFormatSymbols 1161 * @see DecimalFormatSymbols 1162 * @stable ICU 2.0 1163 */ 1164 virtual const DecimalFormatSymbols* getDecimalFormatSymbols() const; 1165 1166 /** 1167 * Sets the decimal format symbols, which is generally not changed 1168 * by the programmer or user. 1169 * @param symbolsToAdopt DecimalFormatSymbols to be adopted. 1170 * @stable ICU 2.0 1171 */ 1172 virtual void adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt); 1173 1174 /** 1175 * Sets the decimal format symbols, which is generally not changed 1176 * by the programmer or user. 1177 * @param symbols DecimalFormatSymbols. 1178 * @stable ICU 2.0 1179 */ 1180 virtual void setDecimalFormatSymbols(const DecimalFormatSymbols& symbols); 1181 1182 1183 /** 1184 * Returns the currency plural format information, 1185 * which is generally not changed by the programmer or user. 1186 * @return desired CurrencyPluralInfo 1187 * @stable ICU 4.2 1188 */ 1189 virtual const CurrencyPluralInfo* getCurrencyPluralInfo() const; 1190 1191 /** 1192 * Sets the currency plural format information, 1193 * which is generally not changed by the programmer or user. 1194 * @param toAdopt CurrencyPluralInfo to be adopted. 1195 * @stable ICU 4.2 1196 */ 1197 virtual void adoptCurrencyPluralInfo(CurrencyPluralInfo* toAdopt); 1198 1199 /** 1200 * Sets the currency plural format information, 1201 * which is generally not changed by the programmer or user. 1202 * @param info Currency Plural Info. 1203 * @stable ICU 4.2 1204 */ 1205 virtual void setCurrencyPluralInfo(const CurrencyPluralInfo& info); 1206 1207 1208 /** 1209 * Get the positive prefix. 1210 * 1211 * @param result Output param which will receive the positive prefix. 1212 * @return A reference to 'result'. 1213 * Examples: +123, $123, sFr123 1214 * @stable ICU 2.0 1215 */ 1216 UnicodeString& getPositivePrefix(UnicodeString& result) const; 1217 1218 /** 1219 * Set the positive prefix. 1220 * 1221 * @param newValue the new value of the the positive prefix to be set. 1222 * Examples: +123, $123, sFr123 1223 * @stable ICU 2.0 1224 */ 1225 virtual void setPositivePrefix(const UnicodeString& newValue); 1226 1227 /** 1228 * Get the negative prefix. 1229 * 1230 * @param result Output param which will receive the negative prefix. 1231 * @return A reference to 'result'. 1232 * Examples: -123, ($123) (with negative suffix), sFr-123 1233 * @stable ICU 2.0 1234 */ 1235 UnicodeString& getNegativePrefix(UnicodeString& result) const; 1236 1237 /** 1238 * Set the negative prefix. 1239 * 1240 * @param newValue the new value of the the negative prefix to be set. 1241 * Examples: -123, ($123) (with negative suffix), sFr-123 1242 * @stable ICU 2.0 1243 */ 1244 virtual void setNegativePrefix(const UnicodeString& newValue); 1245 1246 /** 1247 * Get the positive suffix. 1248 * 1249 * @param result Output param which will receive the positive suffix. 1250 * @return A reference to 'result'. 1251 * Example: 123% 1252 * @stable ICU 2.0 1253 */ 1254 UnicodeString& getPositiveSuffix(UnicodeString& result) const; 1255 1256 /** 1257 * Set the positive suffix. 1258 * 1259 * @param newValue the new value of the positive suffix to be set. 1260 * Example: 123% 1261 * @stable ICU 2.0 1262 */ 1263 virtual void setPositiveSuffix(const UnicodeString& newValue); 1264 1265 /** 1266 * Get the negative suffix. 1267 * 1268 * @param result Output param which will receive the negative suffix. 1269 * @return A reference to 'result'. 1270 * Examples: -123%, ($123) (with positive suffixes) 1271 * @stable ICU 2.0 1272 */ 1273 UnicodeString& getNegativeSuffix(UnicodeString& result) const; 1274 1275 /** 1276 * Set the negative suffix. 1277 * 1278 * @param newValue the new value of the negative suffix to be set. 1279 * Examples: 123% 1280 * @stable ICU 2.0 1281 */ 1282 virtual void setNegativeSuffix(const UnicodeString& newValue); 1283 1284 /** 1285 * Whether to show the plus sign on positive (non-negative) numbers; for example, "+12" 1286 * 1287 * For more control over sign display, use NumberFormatter. 1288 * 1289 * @return Whether the sign is shown on positive numbers and zero. 1290 * @stable ICU 64 1291 */ 1292 UBool isSignAlwaysShown() const; 1293 1294 /** 1295 * Set whether to show the plus sign on positive (non-negative) numbers; for example, "+12". 1296 * 1297 * For more control over sign display, use NumberFormatter. 1298 * 1299 * @param value true to always show a sign; false to hide the sign on positive numbers and zero. 1300 * @stable ICU 64 1301 */ 1302 void setSignAlwaysShown(UBool value); 1303 1304 /** 1305 * Get the multiplier for use in percent, permill, etc. 1306 * For a percentage, set the suffixes to have "%" and the multiplier to be 100. 1307 * (For Arabic, use arabic percent symbol). 1308 * For a permill, set the suffixes to have "\\u2031" and the multiplier to be 1000. 1309 * 1310 * The number may also be multiplied by a power of ten; see getMultiplierScale(). 1311 * 1312 * @return the multiplier for use in percent, permill, etc. 1313 * Examples: with 100, 1.23 -> "123", and "123" -> 1.23 1314 * @stable ICU 2.0 1315 */ 1316 int32_t getMultiplier() const; 1317 1318 /** 1319 * Set the multiplier for use in percent, permill, etc. 1320 * For a percentage, set the suffixes to have "%" and the multiplier to be 100. 1321 * (For Arabic, use arabic percent symbol). 1322 * For a permill, set the suffixes to have "\\u2031" and the multiplier to be 1000. 1323 * 1324 * This method only supports integer multipliers. To multiply by a non-integer, pair this 1325 * method with setMultiplierScale(). 1326 * 1327 * @param newValue the new value of the multiplier for use in percent, permill, etc. 1328 * Examples: with 100, 1.23 -> "123", and "123" -> 1.23 1329 * @stable ICU 2.0 1330 */ 1331 virtual void setMultiplier(int32_t newValue); 1332 1333 /** 1334 * Gets the power of ten by which number should be multiplied before formatting, which 1335 * can be combined with setMultiplier() to multiply by any arbitrary decimal value. 1336 * 1337 * A multiplier scale of 2 corresponds to multiplication by 100, and a multiplier scale 1338 * of -2 corresponds to multiplication by 0.01. 1339 * 1340 * This method is analogous to UNUM_SCALE in getAttribute. 1341 * 1342 * @return the current value of the power-of-ten multiplier. 1343 * @stable ICU 62 1344 */ 1345 int32_t getMultiplierScale() const; 1346 1347 /** 1348 * Sets a power of ten by which number should be multiplied before formatting, which 1349 * can be combined with setMultiplier() to multiply by any arbitrary decimal value. 1350 * 1351 * A multiplier scale of 2 corresponds to multiplication by 100, and a multiplier scale 1352 * of -2 corresponds to multiplication by 0.01. 1353 * 1354 * For example, to multiply numbers by 0.5 before formatting, you can do: 1355 * 1356 * <pre> 1357 * df.setMultiplier(5); 1358 * df.setMultiplierScale(-1); 1359 * </pre> 1360 * 1361 * This method is analogous to UNUM_SCALE in setAttribute. 1362 * 1363 * @param newValue the new value of the power-of-ten multiplier. 1364 * @stable ICU 62 1365 */ 1366 void setMultiplierScale(int32_t newValue); 1367 1368 /** 1369 * Get the rounding increment. 1370 * @return A positive rounding increment, or 0.0 if a custom rounding 1371 * increment is not in effect. 1372 * @see #setRoundingIncrement 1373 * @see #getRoundingMode 1374 * @see #setRoundingMode 1375 * @stable ICU 2.0 1376 */ 1377 virtual double getRoundingIncrement() const; 1378 1379 /** 1380 * Set the rounding increment. In the absence of a rounding increment, 1381 * numbers will be rounded to the number of digits displayed. 1382 * @param newValue A positive rounding increment, or 0.0 to 1383 * use the default rounding increment. 1384 * Negative increments are equivalent to 0.0. 1385 * @see #getRoundingIncrement 1386 * @see #getRoundingMode 1387 * @see #setRoundingMode 1388 * @stable ICU 2.0 1389 */ 1390 virtual void setRoundingIncrement(double newValue); 1391 1392 /** 1393 * Get the rounding mode. 1394 * @return A rounding mode 1395 * @see #setRoundingIncrement 1396 * @see #getRoundingIncrement 1397 * @see #setRoundingMode 1398 * @stable ICU 2.0 1399 */ 1400 virtual ERoundingMode getRoundingMode() const override; 1401 1402 /** 1403 * Set the rounding mode. 1404 * @param roundingMode A rounding mode 1405 * @see #setRoundingIncrement 1406 * @see #getRoundingIncrement 1407 * @see #getRoundingMode 1408 * @stable ICU 2.0 1409 */ 1410 virtual void setRoundingMode(ERoundingMode roundingMode) override; 1411 1412 /** 1413 * Get the width to which the output of format() is padded. 1414 * The width is counted in 16-bit code units. 1415 * @return the format width, or zero if no padding is in effect 1416 * @see #setFormatWidth 1417 * @see #getPadCharacterString 1418 * @see #setPadCharacter 1419 * @see #getPadPosition 1420 * @see #setPadPosition 1421 * @stable ICU 2.0 1422 */ 1423 virtual int32_t getFormatWidth() const; 1424 1425 /** 1426 * Set the width to which the output of format() is padded. 1427 * The width is counted in 16-bit code units. 1428 * This method also controls whether padding is enabled. 1429 * @param width the width to which to pad the result of 1430 * format(), or zero to disable padding. A negative 1431 * width is equivalent to 0. 1432 * @see #getFormatWidth 1433 * @see #getPadCharacterString 1434 * @see #setPadCharacter 1435 * @see #getPadPosition 1436 * @see #setPadPosition 1437 * @stable ICU 2.0 1438 */ 1439 virtual void setFormatWidth(int32_t width); 1440 1441 /** 1442 * Get the pad character used to pad to the format width. The 1443 * default is ' '. 1444 * @return a string containing the pad character. This will always 1445 * have a length of one 32-bit code point. 1446 * @see #setFormatWidth 1447 * @see #getFormatWidth 1448 * @see #setPadCharacter 1449 * @see #getPadPosition 1450 * @see #setPadPosition 1451 * @stable ICU 2.0 1452 */ 1453 virtual UnicodeString getPadCharacterString() const; 1454 1455 /** 1456 * Set the character used to pad to the format width. If padding 1457 * is not enabled, then this will take effect if padding is later 1458 * enabled. 1459 * @param padChar a string containing the pad character. If the string 1460 * has length 0, then the pad character is set to ' '. Otherwise 1461 * padChar.char32At(0) will be used as the pad character. 1462 * @see #setFormatWidth 1463 * @see #getFormatWidth 1464 * @see #getPadCharacterString 1465 * @see #getPadPosition 1466 * @see #setPadPosition 1467 * @stable ICU 2.0 1468 */ 1469 virtual void setPadCharacter(const UnicodeString& padChar); 1470 1471 /** 1472 * Get the position at which padding will take place. This is the location 1473 * at which padding will be inserted if the result of format() 1474 * is shorter than the format width. 1475 * @return the pad position, one of kPadBeforePrefix, 1476 * kPadAfterPrefix, kPadBeforeSuffix, or 1477 * kPadAfterSuffix. 1478 * @see #setFormatWidth 1479 * @see #getFormatWidth 1480 * @see #setPadCharacter 1481 * @see #getPadCharacterString 1482 * @see #setPadPosition 1483 * @see #EPadPosition 1484 * @stable ICU 2.0 1485 */ 1486 virtual EPadPosition getPadPosition() const; 1487 1488 /** 1489 * Set the position at which padding will take place. This is the location 1490 * at which padding will be inserted if the result of format() 1491 * is shorter than the format width. This has no effect unless padding is 1492 * enabled. 1493 * @param padPos the pad position, one of kPadBeforePrefix, 1494 * kPadAfterPrefix, kPadBeforeSuffix, or 1495 * kPadAfterSuffix. 1496 * @see #setFormatWidth 1497 * @see #getFormatWidth 1498 * @see #setPadCharacter 1499 * @see #getPadCharacterString 1500 * @see #getPadPosition 1501 * @see #EPadPosition 1502 * @stable ICU 2.0 1503 */ 1504 virtual void setPadPosition(EPadPosition padPos); 1505 1506 /** 1507 * Return whether or not scientific notation is used. 1508 * @return true if this object formats and parses scientific notation 1509 * @see #setScientificNotation 1510 * @see #getMinimumExponentDigits 1511 * @see #setMinimumExponentDigits 1512 * @see #isExponentSignAlwaysShown 1513 * @see #setExponentSignAlwaysShown 1514 * @stable ICU 2.0 1515 */ 1516 virtual UBool isScientificNotation() const; 1517 1518 /** 1519 * Set whether or not scientific notation is used. When scientific notation 1520 * is used, the effective maximum number of integer digits is <= 8. If the 1521 * maximum number of integer digits is set to more than 8, the effective 1522 * maximum will be 1. This allows this call to generate a 'default' scientific 1523 * number format without additional changes. 1524 * @param useScientific true if this object formats and parses scientific 1525 * notation 1526 * @see #isScientificNotation 1527 * @see #getMinimumExponentDigits 1528 * @see #setMinimumExponentDigits 1529 * @see #isExponentSignAlwaysShown 1530 * @see #setExponentSignAlwaysShown 1531 * @stable ICU 2.0 1532 */ 1533 virtual void setScientificNotation(UBool useScientific); 1534 1535 /** 1536 * Return the minimum exponent digits that will be shown. 1537 * @return the minimum exponent digits that will be shown 1538 * @see #setScientificNotation 1539 * @see #isScientificNotation 1540 * @see #setMinimumExponentDigits 1541 * @see #isExponentSignAlwaysShown 1542 * @see #setExponentSignAlwaysShown 1543 * @stable ICU 2.0 1544 */ 1545 virtual int8_t getMinimumExponentDigits() const; 1546 1547 /** 1548 * Set the minimum exponent digits that will be shown. This has no 1549 * effect unless scientific notation is in use. 1550 * @param minExpDig a value >= 1 indicating the fewest exponent digits 1551 * that will be shown. Values less than 1 will be treated as 1. 1552 * @see #setScientificNotation 1553 * @see #isScientificNotation 1554 * @see #getMinimumExponentDigits 1555 * @see #isExponentSignAlwaysShown 1556 * @see #setExponentSignAlwaysShown 1557 * @stable ICU 2.0 1558 */ 1559 virtual void setMinimumExponentDigits(int8_t minExpDig); 1560 1561 /** 1562 * Return whether the exponent sign is always shown. 1563 * @return true if the exponent is always prefixed with either the 1564 * localized minus sign or the localized plus sign, false if only negative 1565 * exponents are prefixed with the localized minus sign. 1566 * @see #setScientificNotation 1567 * @see #isScientificNotation 1568 * @see #setMinimumExponentDigits 1569 * @see #getMinimumExponentDigits 1570 * @see #setExponentSignAlwaysShown 1571 * @stable ICU 2.0 1572 */ 1573 virtual UBool isExponentSignAlwaysShown() const; 1574 1575 /** 1576 * Set whether the exponent sign is always shown. This has no effect 1577 * unless scientific notation is in use. 1578 * @param expSignAlways true if the exponent is always prefixed with either 1579 * the localized minus sign or the localized plus sign, false if only 1580 * negative exponents are prefixed with the localized minus sign. 1581 * @see #setScientificNotation 1582 * @see #isScientificNotation 1583 * @see #setMinimumExponentDigits 1584 * @see #getMinimumExponentDigits 1585 * @see #isExponentSignAlwaysShown 1586 * @stable ICU 2.0 1587 */ 1588 virtual void setExponentSignAlwaysShown(UBool expSignAlways); 1589 1590 /** 1591 * Return the grouping size. Grouping size is the number of digits between 1592 * grouping separators in the integer portion of a number. For example, 1593 * in the number "123,456.78", the grouping size is 3. 1594 * 1595 * @return the grouping size. 1596 * @see setGroupingSize 1597 * @see NumberFormat::isGroupingUsed 1598 * @see DecimalFormatSymbols::getGroupingSeparator 1599 * @stable ICU 2.0 1600 */ 1601 int32_t getGroupingSize() const; 1602 1603 /** 1604 * Set the grouping size. Grouping size is the number of digits between 1605 * grouping separators in the integer portion of a number. For example, 1606 * in the number "123,456.78", the grouping size is 3. 1607 * 1608 * @param newValue the new value of the grouping size. 1609 * @see getGroupingSize 1610 * @see NumberFormat::setGroupingUsed 1611 * @see DecimalFormatSymbols::setGroupingSeparator 1612 * @stable ICU 2.0 1613 */ 1614 virtual void setGroupingSize(int32_t newValue); 1615 1616 /** 1617 * Return the secondary grouping size. In some locales one 1618 * grouping interval is used for the least significant integer 1619 * digits (the primary grouping size), and another is used for all 1620 * others (the secondary grouping size). A formatter supporting a 1621 * secondary grouping size will return a positive integer unequal 1622 * to the primary grouping size returned by 1623 * getGroupingSize(). For example, if the primary 1624 * grouping size is 4, and the secondary grouping size is 2, then 1625 * the number 123456789 formats as "1,23,45,6789", and the pattern 1626 * appears as "#,##,###0". 1627 * @return the secondary grouping size, or a value less than 1628 * one if there is none 1629 * @see setSecondaryGroupingSize 1630 * @see NumberFormat::isGroupingUsed 1631 * @see DecimalFormatSymbols::getGroupingSeparator 1632 * @stable ICU 2.4 1633 */ 1634 int32_t getSecondaryGroupingSize() const; 1635 1636 /** 1637 * Set the secondary grouping size. If set to a value less than 1, 1638 * then secondary grouping is turned off, and the primary grouping 1639 * size is used for all intervals, not just the least significant. 1640 * 1641 * @param newValue the new value of the secondary grouping size. 1642 * @see getSecondaryGroupingSize 1643 * @see NumberFormat#setGroupingUsed 1644 * @see DecimalFormatSymbols::setGroupingSeparator 1645 * @stable ICU 2.4 1646 */ 1647 virtual void setSecondaryGroupingSize(int32_t newValue); 1648 1649 /** 1650 * Returns the minimum number of grouping digits. 1651 * Grouping separators are output if there are at least this many 1652 * digits to the left of the first (rightmost) grouping separator, 1653 * that is, there are at least (minimum grouping + grouping size) integer digits. 1654 * (Subject to isGroupingUsed().) 1655 * 1656 * For example, if this value is 2, and the grouping size is 3, then 1657 * 9999 -> "9999" and 10000 -> "10,000" 1658 * 1659 * The default value for this attribute is 0. 1660 * A value of 1, 0, or lower, means that the use of grouping separators 1661 * only depends on the grouping size (and on isGroupingUsed()). 1662 * 1663 * NOTE: The CLDR data is used in NumberFormatter but not in DecimalFormat. 1664 * This is for backwards compatibility reasons. 1665 * 1666 * For more control over grouping strategies, use NumberFormatter. 1667 * 1668 * @see setMinimumGroupingDigits 1669 * @see getGroupingSize 1670 * @stable ICU 64 1671 */ 1672 int32_t getMinimumGroupingDigits() const; 1673 1674 /** 1675 * Sets the minimum grouping digits. Setting the value to 1676 * - 1: Turns off minimum grouping digits. 1677 * - 0 or -1: The behavior is undefined. 1678 * - UNUM_MINIMUM_GROUPING_DIGITS_AUTO: Display grouping using the default 1679 * strategy for all locales. 1680 * - UNUM_MINIMUM_GROUPING_DIGITS_MIN2: Display grouping using locale 1681 * defaults, except do not show grouping on values smaller than 10000 1682 * (such that there is a minimum of two digits before the first 1683 * separator). 1684 * 1685 * For more control over grouping strategies, use NumberFormatter. 1686 * 1687 * @param newValue the new value of minimum grouping digits. 1688 * @see getMinimumGroupingDigits 1689 * @stable ICU 64 1690 */ 1691 void setMinimumGroupingDigits(int32_t newValue); 1692 1693 /** 1694 * Allows you to get the behavior of the decimal separator with integers. 1695 * (The decimal separator will always appear with decimals.) 1696 * 1697 * @return true if the decimal separator always appear with decimals. 1698 * Example: Decimal ON: 12345 -> 12345.; OFF: 12345 -> 12345 1699 * @stable ICU 2.0 1700 */ 1701 UBool isDecimalSeparatorAlwaysShown() const; 1702 1703 /** 1704 * Allows you to set the behavior of the decimal separator with integers. 1705 * (The decimal separator will always appear with decimals.) 1706 * 1707 * @param newValue set true if the decimal separator will always appear with decimals. 1708 * Example: Decimal ON: 12345 -> 12345.; OFF: 12345 -> 12345 1709 * @stable ICU 2.0 1710 */ 1711 virtual void setDecimalSeparatorAlwaysShown(UBool newValue); 1712 1713 /** 1714 * Allows you to get the parse behavior of the pattern decimal mark. 1715 * 1716 * @return true if input must contain a match to decimal mark in pattern 1717 * @stable ICU 54 1718 */ 1719 UBool isDecimalPatternMatchRequired() const; 1720 1721 /** 1722 * Allows you to set the parse behavior of the pattern decimal mark. 1723 * 1724 * if true, the input must have a decimal mark if one was specified in the pattern. When 1725 * false the decimal mark may be omitted from the input. 1726 * 1727 * @param newValue set true if input must contain a match to decimal mark in pattern 1728 * @stable ICU 54 1729 */ 1730 virtual void setDecimalPatternMatchRequired(UBool newValue); 1731 1732 /** 1733 * Returns whether to ignore exponents when parsing. 1734 * 1735 * @return Whether to ignore exponents when parsing. 1736 * @see #setParseNoExponent 1737 * @stable ICU 64 1738 */ 1739 UBool isParseNoExponent() const; 1740 1741 /** 1742 * Specifies whether to stop parsing when an exponent separator is encountered. For 1743 * example, parses "123E4" to 123 (with parse position 3) instead of 1230000 (with parse position 1744 * 5). 1745 * 1746 * @param value true to prevent exponents from being parsed; false to allow them to be parsed. 1747 * @stable ICU 64 1748 */ 1749 void setParseNoExponent(UBool value); 1750 1751 /** 1752 * Returns whether parsing is sensitive to case (lowercase/uppercase). 1753 * 1754 * @return Whether parsing is case-sensitive. 1755 * @see #setParseCaseSensitive 1756 * @stable ICU 64 1757 */ 1758 UBool isParseCaseSensitive() const; 1759 1760 /** 1761 * Whether to pay attention to case when parsing; default is to ignore case (perform 1762 * case-folding). For example, "A" == "a" in case-insensitive but not case-sensitive mode. 1763 * 1764 * Currency symbols are never case-folded. For example, "us$1.00" will not parse in case-insensitive 1765 * mode, even though "US$1.00" parses. 1766 * 1767 * @param value true to enable case-sensitive parsing (the default); false to force 1768 * case-sensitive parsing behavior. 1769 * @stable ICU 64 1770 */ 1771 void setParseCaseSensitive(UBool value); 1772 1773 /** 1774 * Returns whether truncation of high-order integer digits should result in an error. 1775 * By default, setMaximumIntegerDigits truncates high-order digits silently. 1776 * 1777 * @return Whether an error code is set if high-order digits are truncated. 1778 * @see setFormatFailIfMoreThanMaxDigits 1779 * @stable ICU 64 1780 */ 1781 UBool isFormatFailIfMoreThanMaxDigits() const; 1782 1783 /** 1784 * Sets whether truncation of high-order integer digits should result in an error. 1785 * By default, setMaximumIntegerDigits truncates high-order digits silently. 1786 * 1787 * @param value Whether to set an error code if high-order digits are truncated. 1788 * @stable ICU 64 1789 */ 1790 void setFormatFailIfMoreThanMaxDigits(UBool value); 1791 1792 /** 1793 * Synthesizes a pattern string that represents the current state 1794 * of this Format object. 1795 * 1796 * @param result Output param which will receive the pattern. 1797 * Previous contents are deleted. 1798 * @return A reference to 'result'. 1799 * @see applyPattern 1800 * @stable ICU 2.0 1801 */ 1802 virtual UnicodeString& toPattern(UnicodeString& result) const; 1803 1804 /** 1805 * Synthesizes a localized pattern string that represents the current 1806 * state of this Format object. 1807 * 1808 * @param result Output param which will receive the localized pattern. 1809 * Previous contents are deleted. 1810 * @return A reference to 'result'. 1811 * @see applyPattern 1812 * @stable ICU 2.0 1813 */ 1814 virtual UnicodeString& toLocalizedPattern(UnicodeString& result) const; 1815 1816 /** 1817 * Apply the given pattern to this Format object. A pattern is a 1818 * short-hand specification for the various formatting properties. 1819 * These properties can also be changed individually through the 1820 * various setter methods. 1821 * <P> 1822 * There is no limit to integer digits are set 1823 * by this routine, since that is the typical end-user desire; 1824 * use setMaximumInteger if you want to set a real value. 1825 * For negative numbers, use a second pattern, separated by a semicolon 1826 * <pre> 1827 * . Example "#,#00.0#" -> 1,234.56 1828 * </pre> 1829 * This means a minimum of 2 integer digits, 1 fraction digit, and 1830 * a maximum of 2 fraction digits. 1831 * <pre> 1832 * . Example: "#,#00.0#;(#,#00.0#)" for negatives in parentheses. 1833 * </pre> 1834 * In negative patterns, the minimum and maximum counts are ignored; 1835 * these are presumed to be set in the positive pattern. 1836 * 1837 * @param pattern The pattern to be applied. 1838 * @param parseError Struct to receive information on position 1839 * of error if an error is encountered 1840 * @param status Output param set to success/failure code on 1841 * exit. If the pattern is invalid, this will be 1842 * set to a failure result. 1843 * @stable ICU 2.0 1844 */ 1845 virtual void applyPattern(const UnicodeString& pattern, UParseError& parseError, UErrorCode& status); 1846 1847 /** 1848 * Sets the pattern. 1849 * @param pattern The pattern to be applied. 1850 * @param status Output param set to success/failure code on 1851 * exit. If the pattern is invalid, this will be 1852 * set to a failure result. 1853 * @stable ICU 2.0 1854 */ 1855 virtual void applyPattern(const UnicodeString& pattern, UErrorCode& status); 1856 1857 /** 1858 * Apply the given pattern to this Format object. The pattern 1859 * is assumed to be in a localized notation. A pattern is a 1860 * short-hand specification for the various formatting properties. 1861 * These properties can also be changed individually through the 1862 * various setter methods. 1863 * <P> 1864 * There is no limit to integer digits are set 1865 * by this routine, since that is the typical end-user desire; 1866 * use setMaximumInteger if you want to set a real value. 1867 * For negative numbers, use a second pattern, separated by a semicolon 1868 * <pre> 1869 * . Example "#,#00.0#" -> 1,234.56 1870 * </pre> 1871 * This means a minimum of 2 integer digits, 1 fraction digit, and 1872 * a maximum of 2 fraction digits. 1873 * 1874 * Example: "#,#00.0#;(#,#00.0#)" for negatives in parentheses. 1875 * 1876 * In negative patterns, the minimum and maximum counts are ignored; 1877 * these are presumed to be set in the positive pattern. 1878 * 1879 * @param pattern The localized pattern to be applied. 1880 * @param parseError Struct to receive information on position 1881 * of error if an error is encountered 1882 * @param status Output param set to success/failure code on 1883 * exit. If the pattern is invalid, this will be 1884 * set to a failure result. 1885 * @stable ICU 2.0 1886 */ 1887 virtual void applyLocalizedPattern(const UnicodeString& pattern, UParseError& parseError, 1888 UErrorCode& status); 1889 1890 /** 1891 * Apply the given pattern to this Format object. 1892 * 1893 * @param pattern The localized pattern to be applied. 1894 * @param status Output param set to success/failure code on 1895 * exit. If the pattern is invalid, this will be 1896 * set to a failure result. 1897 * @stable ICU 2.0 1898 */ 1899 virtual void applyLocalizedPattern(const UnicodeString& pattern, UErrorCode& status); 1900 1901 1902 /** 1903 * Sets the maximum number of digits allowed in the integer portion of a 1904 * number. This override limits the integer digit count to 309. 1905 * 1906 * @param newValue the new value of the maximum number of digits 1907 * allowed in the integer portion of a number. 1908 * @see NumberFormat#setMaximumIntegerDigits 1909 * @stable ICU 2.0 1910 */ 1911 void setMaximumIntegerDigits(int32_t newValue) override; 1912 1913 /** 1914 * Sets the minimum number of digits allowed in the integer portion of a 1915 * number. This override limits the integer digit count to 309. 1916 * 1917 * @param newValue the new value of the minimum number of digits 1918 * allowed in the integer portion of a number. 1919 * @see NumberFormat#setMinimumIntegerDigits 1920 * @stable ICU 2.0 1921 */ 1922 void setMinimumIntegerDigits(int32_t newValue) override; 1923 1924 /** 1925 * Sets the maximum number of digits allowed in the fraction portion of a 1926 * number. This override limits the fraction digit count to 340. 1927 * 1928 * @param newValue the new value of the maximum number of digits 1929 * allowed in the fraction portion of a number. 1930 * @see NumberFormat#setMaximumFractionDigits 1931 * @stable ICU 2.0 1932 */ 1933 void setMaximumFractionDigits(int32_t newValue) override; 1934 1935 /** 1936 * Sets the minimum number of digits allowed in the fraction portion of a 1937 * number. This override limits the fraction digit count to 340. 1938 * 1939 * @param newValue the new value of the minimum number of digits 1940 * allowed in the fraction portion of a number. 1941 * @see NumberFormat#setMinimumFractionDigits 1942 * @stable ICU 2.0 1943 */ 1944 void setMinimumFractionDigits(int32_t newValue) override; 1945 1946 /** 1947 * Returns the minimum number of significant digits that will be 1948 * displayed. This value has no effect unless areSignificantDigitsUsed() 1949 * returns true. 1950 * @return the fewest significant digits that will be shown 1951 * @stable ICU 3.0 1952 */ 1953 int32_t getMinimumSignificantDigits() const; 1954 1955 /** 1956 * Returns the maximum number of significant digits that will be 1957 * displayed. This value has no effect unless areSignificantDigitsUsed() 1958 * returns true. 1959 * @return the most significant digits that will be shown 1960 * @stable ICU 3.0 1961 */ 1962 int32_t getMaximumSignificantDigits() const; 1963 1964 /** 1965 * Sets the minimum number of significant digits that will be 1966 * displayed. If <code>min</code> is less than one then it is set 1967 * to one. If the maximum significant digits count is less than 1968 * <code>min</code>, then it is set to <code>min</code>. 1969 * This function also enables the use of significant digits 1970 * by this formatter - areSignificantDigitsUsed() will return true. 1971 * @see #areSignificantDigitsUsed 1972 * @param min the fewest significant digits to be shown 1973 * @stable ICU 3.0 1974 */ 1975 void setMinimumSignificantDigits(int32_t min); 1976 1977 /** 1978 * Sets the maximum number of significant digits that will be 1979 * displayed. If <code>max</code> is less than one then it is set 1980 * to one. If the minimum significant digits count is greater 1981 * than <code>max</code>, then it is set to <code>max</code>. 1982 * This function also enables the use of significant digits 1983 * by this formatter - areSignificantDigitsUsed() will return true. 1984 * @see #areSignificantDigitsUsed 1985 * @param max the most significant digits to be shown 1986 * @stable ICU 3.0 1987 */ 1988 void setMaximumSignificantDigits(int32_t max); 1989 1990 /** 1991 * Returns true if significant digits are in use, or false if 1992 * integer and fraction digit counts are in use. 1993 * @return true if significant digits are in use 1994 * @stable ICU 3.0 1995 */ 1996 UBool areSignificantDigitsUsed() const; 1997 1998 /** 1999 * Sets whether significant digits are in use, or integer and 2000 * fraction digit counts are in use. 2001 * @param useSignificantDigits true to use significant digits, or 2002 * false to use integer and fraction digit counts 2003 * @stable ICU 3.0 2004 */ 2005 void setSignificantDigitsUsed(UBool useSignificantDigits); 2006 2007 /** 2008 * Sets the currency used to display currency 2009 * amounts. This takes effect immediately, if this format is a 2010 * currency format. If this format is not a currency format, then 2011 * the currency is used if and when this object becomes a 2012 * currency format through the application of a new pattern. 2013 * @param theCurrency a 3-letter ISO code indicating new currency 2014 * to use. It need not be null-terminated. May be the empty 2015 * string or nullptr to indicate no currency. 2016 * @param ec input-output error code 2017 * @stable ICU 3.0 2018 */ 2019 void setCurrency(const char16_t* theCurrency, UErrorCode& ec) override; 2020 2021 #ifndef U_FORCE_HIDE_DEPRECATED_API 2022 /** 2023 * Sets the currency used to display currency amounts. See 2024 * setCurrency(const char16_t*, UErrorCode&). 2025 * @deprecated ICU 3.0. Use setCurrency(const char16_t*, UErrorCode&). 2026 */ 2027 virtual void setCurrency(const char16_t* theCurrency); 2028 #endif // U_FORCE_HIDE_DEPRECATED_API 2029 2030 /** 2031 * Sets the `Currency Usage` object used to display currency. 2032 * This takes effect immediately, if this format is a 2033 * currency format. 2034 * @param newUsage new currency usage object to use. 2035 * @param ec input-output error code 2036 * @stable ICU 54 2037 */ 2038 void setCurrencyUsage(UCurrencyUsage newUsage, UErrorCode* ec); 2039 2040 /** 2041 * Returns the `Currency Usage` object used to display currency 2042 * @stable ICU 54 2043 */ 2044 UCurrencyUsage getCurrencyUsage() const; 2045 2046 #ifndef U_HIDE_INTERNAL_API 2047 2048 /** 2049 * Format a number and save it into the given DecimalQuantity. 2050 * Internal, not intended for public use. 2051 * @internal 2052 */ 2053 void formatToDecimalQuantity(double number, number::impl::DecimalQuantity& output, 2054 UErrorCode& status) const; 2055 2056 /** 2057 * Get a DecimalQuantity corresponding to a formattable as it would be 2058 * formatted by this DecimalFormat. 2059 * Internal, not intended for public use. 2060 * @internal 2061 */ 2062 void formatToDecimalQuantity(const Formattable& number, number::impl::DecimalQuantity& output, 2063 UErrorCode& status) const; 2064 2065 #endif /* U_HIDE_INTERNAL_API */ 2066 2067 /** 2068 * Converts this DecimalFormat to a (Localized)NumberFormatter. Starting 2069 * in ICU 60, NumberFormatter is the recommended way to format numbers. 2070 * You can use the returned LocalizedNumberFormatter to format numbers and 2071 * get a FormattedNumber, which contains a string as well as additional 2072 * annotations about the formatted value. 2073 * 2074 * If a memory allocation failure occurs, the return value of this method 2075 * might be null. If you are concerned about correct recovery from 2076 * out-of-memory situations, use this pattern: 2077 * 2078 * <pre> 2079 * FormattedNumber result; 2080 * if (auto* ptr = df->toNumberFormatter(status)) { 2081 * result = ptr->formatDouble(123, status); 2082 * } 2083 * </pre> 2084 * 2085 * If you are not concerned about out-of-memory situations, or if your 2086 * environment throws exceptions when memory allocation failure occurs, 2087 * you can chain the methods, like this: 2088 * 2089 * <pre> 2090 * FormattedNumber result = df 2091 * ->toNumberFormatter(status) 2092 * ->formatDouble(123, status); 2093 * </pre> 2094 * 2095 * NOTE: The returned LocalizedNumberFormatter is owned by this DecimalFormat. 2096 * If a non-const method is called on the DecimalFormat, or if the DecimalFormat 2097 * is deleted, the object becomes invalid. If you plan to keep the return value 2098 * beyond the lifetime of the DecimalFormat, copy it to a local variable: 2099 * 2100 * <pre> 2101 * LocalizedNumberFormatter lnf; 2102 * if (auto* ptr = df->toNumberFormatter(status)) { 2103 * lnf = *ptr; 2104 * } 2105 * </pre> 2106 * 2107 * @param status Set on failure, like U_MEMORY_ALLOCATION_ERROR. 2108 * @return A pointer to an internal object, or nullptr on failure. 2109 * Do not delete the return value! 2110 * @stable ICU 64 2111 */ 2112 const number::LocalizedNumberFormatter* toNumberFormatter(UErrorCode& status) const; 2113 2114 /** 2115 * Return the class ID for this class. This is useful only for 2116 * comparing to a return value from getDynamicClassID(). For example: 2117 * <pre> 2118 * . Base* polymorphic_pointer = createPolymorphicObject(); 2119 * . if (polymorphic_pointer->getDynamicClassID() == 2120 * . Derived::getStaticClassID()) ... 2121 * </pre> 2122 * @return The class ID for all objects of this class. 2123 * @stable ICU 2.0 2124 */ 2125 static UClassID U_EXPORT2 getStaticClassID(); 2126 2127 /** 2128 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. 2129 * This method is to implement a simple version of RTTI, since not all 2130 * C++ compilers support genuine RTTI. Polymorphic operator==() and 2131 * clone() methods call this method. 2132 * 2133 * @return The class ID for this object. All objects of a 2134 * given class have the same class ID. Objects of 2135 * other classes have different class IDs. 2136 * @stable ICU 2.0 2137 */ 2138 UClassID getDynamicClassID() const override; 2139 2140 private: 2141 2142 /** Rebuilds the formatter object from the property bag. */ 2143 void touch(UErrorCode& status); 2144 2145 /** Rebuilds the formatter object, ignoring any error code. */ 2146 void touchNoError(); 2147 2148 /** 2149 * Updates the property bag with settings from the given pattern. 2150 * 2151 * @param pattern The pattern string to parse. 2152 * @param ignoreRounding Whether to leave out rounding information (minFrac, maxFrac, and rounding 2153 * increment) when parsing the pattern. This may be desirable if a custom rounding mode, such 2154 * as CurrencyUsage, is to be used instead. One of {@link 2155 * PatternStringParser#IGNORE_ROUNDING_ALWAYS}, {@link PatternStringParser#IGNORE_ROUNDING_IF_CURRENCY}, 2156 * or {@link PatternStringParser#IGNORE_ROUNDING_NEVER}. 2157 * @see PatternAndPropertyUtils#parseToExistingProperties 2158 */ 2159 void setPropertiesFromPattern(const UnicodeString& pattern, int32_t ignoreRounding, 2160 UErrorCode& status); 2161 2162 const numparse::impl::NumberParserImpl* getParser(UErrorCode& status) const; 2163 2164 const numparse::impl::NumberParserImpl* getCurrencyParser(UErrorCode& status) const; 2165 2166 static void fieldPositionHelper( 2167 const number::impl::UFormattedNumberData& formatted, 2168 FieldPosition& fieldPosition, 2169 int32_t offset, 2170 UErrorCode& status); 2171 2172 static void fieldPositionIteratorHelper( 2173 const number::impl::UFormattedNumberData& formatted, 2174 FieldPositionIterator* fpi, 2175 int32_t offset, 2176 UErrorCode& status); 2177 2178 void setupFastFormat(); 2179 2180 bool fastFormatDouble(double input, UnicodeString& output) const; 2181 2182 bool fastFormatInt64(int64_t input, UnicodeString& output) const; 2183 2184 void doFastFormatInt32(int32_t input, bool isNegative, UnicodeString& output) const; 2185 2186 //=====================================================================================// 2187 // INSTANCE FIELDS // 2188 //=====================================================================================// 2189 2190 2191 // One instance field for the implementation, keep all fields inside of an implementation 2192 // class defined in number_mapper.h 2193 number::impl::DecimalFormatFields* fields = nullptr; 2194 2195 // Allow child class CompactDecimalFormat to access fProperties: 2196 friend class CompactDecimalFormat; 2197 2198 // Allow MeasureFormat to use fieldPositionHelper: 2199 friend class MeasureFormat; 2200 2201 }; 2202 2203 U_NAMESPACE_END 2204 2205 #endif /* #if !UCONFIG_NO_FORMATTING */ 2206 2207 #endif /* U_SHOW_CPLUSPLUS_API */ 2208 2209 #endif // _DECIMFMT 2210 //eof
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|