1 // 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html 3 /* 4 ******************************************************************************** 5 * Copyright (C) 1997-2016, International Business Machines 6 * Corporation and others. All Rights Reserved. 7 ******************************************************************************** 8 * 9 * File DECIMFMT.H 10 * 11 * Modification History: 12 * 13 * Date Name Description 14 * 02/19/97 aliu Converted from java. 15 * 03/20/97 clhuang Updated per C++ implementation. 16 * 04/03/97 aliu Rewrote parsing and formatting completely, and 17 * cleaned up and debugged. Actually works now. 18 * 04/17/97 aliu Changed DigitCount to int per code review. 19 * 07/10/97 helena Made ParsePosition a class and get rid of the function 20 * hiding problems. 21 * 09/09/97 aliu Ported over support for exponential formats. 22 * 07/20/98 stephen Changed documentation 23 * 01/30/13 emmons Added Scaling methods 24 ******************************************************************************** 25 */ 26 27 #ifndef DECIMFMT_H 28 #define DECIMFMT_H 29 30 #include "unicode/utypes.h" 31 /** 32 * \file 33 * \brief C++ API: Formats decimal numbers. 34 */ 35 36 #if !UCONFIG_NO_FORMATTING 37 38 #include "unicode/dcfmtsym.h" 39 #include "unicode/numfmt.h" 40 #include "unicode/locid.h" 41 #include "unicode/fpositer.h" 42 #include "unicode/stringpiece.h" 43 #include "unicode/curramt.h" 44 #include "unicode/enumset.h" 45 46 #ifndef U_HIDE_INTERNAL_API 47 /** 48 * \def UNUM_DECIMALFORMAT_INTERNAL_SIZE 49 * @internal 50 */ 51 #if UCONFIG_FORMAT_FASTPATHS_49 52 #define UNUM_DECIMALFORMAT_INTERNAL_SIZE 16 53 #endif 54 #endif /* U_HIDE_INTERNAL_API */ 55 56 U_NAMESPACE_BEGIN 57 58 class DigitList; 59 class CurrencyPluralInfo; 60 class Hashtable; 61 class UnicodeSet; 62 class FieldPositionHandler; 63 class DecimalFormatStaticSets; 64 class FixedDecimal; 65 class DecimalFormatImpl; 66 class PluralRules; 67 class VisibleDigitsWithExponent; 68 69 // explicit template instantiation. see digitlst.h 70 #if defined (_MSC_VER) 71 template class U_I18N_API EnumSet<UNumberFormatAttribute, 72 UNUM_MAX_NONBOOLEAN_ATTRIBUTE+1, 73 UNUM_LIMIT_BOOLEAN_ATTRIBUTE>; 74 #endif 75 76 /** 77 * DecimalFormat is a concrete subclass of NumberFormat that formats decimal 78 * numbers. It has a variety of features designed to make it possible to parse 79 * and format numbers in any locale, including support for Western, Arabic, or 80 * Indic digits. It also supports different flavors of numbers, including 81 * integers ("123"), fixed-point numbers ("123.4"), scientific notation 82 * ("1.23E4"), percentages ("12%"), and currency amounts ("$123", "USD123", 83 * "123 US dollars"). All of these flavors can be easily localized. 84 * 85 * <p>To obtain a NumberFormat for a specific locale (including the default 86 * locale) call one of NumberFormat's factory methods such as 87 * createInstance(). Do not call the DecimalFormat constructors directly, unless 88 * you know what you are doing, since the NumberFormat factory methods may 89 * return subclasses other than DecimalFormat. 90 * 91 * <p><strong>Example Usage</strong> 92 * 93 * \code 94 * // Normally we would have a GUI with a menu for this 95 * int32_t locCount; 96 * const Locale* locales = NumberFormat::getAvailableLocales(locCount); 97 * 98 * double myNumber = -1234.56; 99 * UErrorCode success = U_ZERO_ERROR; 100 * NumberFormat* form; 101 * 102 * // Print out a number with the localized number, currency and percent 103 * // format for each locale. 104 * UnicodeString countryName; 105 * UnicodeString displayName; 106 * UnicodeString str; 107 * UnicodeString pattern; 108 * Formattable fmtable; 109 * for (int32_t j = 0; j < 3; ++j) { 110 * cout << endl << "FORMAT " << j << endl; 111 * for (int32_t i = 0; i < locCount; ++i) { 112 * if (locales[i].getCountry(countryName).size() == 0) { 113 * // skip language-only 114 * continue; 115 * } 116 * switch (j) { 117 * case 0: 118 * form = NumberFormat::createInstance(locales[i], success ); break; 119 * case 1: 120 * form = NumberFormat::createCurrencyInstance(locales[i], success ); break; 121 * default: 122 * form = NumberFormat::createPercentInstance(locales[i], success ); break; 123 * } 124 * if (form) { 125 * str.remove(); 126 * pattern = ((DecimalFormat*)form)->toPattern(pattern); 127 * cout << locales[i].getDisplayName(displayName) << ": " << pattern; 128 * cout << " -> " << form->format(myNumber,str) << endl; 129 * form->parse(form->format(myNumber,str), fmtable, success); 130 * delete form; 131 * } 132 * } 133 * } 134 * \endcode 135 * <P> 136 * Another example use createInstance(style) 137 * <P> 138 * <pre> 139 * <strong>// Print out a number using the localized number, currency, 140 * // percent, scientific, integer, iso currency, and plural currency 141 * // format for each locale</strong> 142 * Locale* locale = new Locale("en", "US"); 143 * double myNumber = 1234.56; 144 * UErrorCode success = U_ZERO_ERROR; 145 * UnicodeString str; 146 * Formattable fmtable; 147 * for (int j=NumberFormat::kNumberStyle; 148 * j<=NumberFormat::kPluralCurrencyStyle; 149 * ++j) { 150 * NumberFormat* format = NumberFormat::createInstance(locale, j, success); 151 * str.remove(); 152 * cout << "format result " << form->format(myNumber, str) << endl; 153 * format->parse(form->format(myNumber, str), fmtable, success); 154 * }</pre> 155 * 156 * 157 * <p><strong>Patterns</strong> 158 * 159 * <p>A DecimalFormat consists of a <em>pattern</em> and a set of 160 * <em>symbols</em>. The pattern may be set directly using 161 * applyPattern(), or indirectly using other API methods which 162 * manipulate aspects of the pattern, such as the minimum number of integer 163 * digits. The symbols are stored in a DecimalFormatSymbols 164 * object. When using the NumberFormat factory methods, the 165 * pattern and symbols are read from ICU's locale data. 166 * 167 * <p><strong>Special Pattern Characters</strong> 168 * 169 * <p>Many characters in a pattern are taken literally; they are matched during 170 * parsing and output unchanged during formatting. Special characters, on the 171 * other hand, stand for other characters, strings, or classes of characters. 172 * For example, the '#' character is replaced by a localized digit. Often the 173 * replacement character is the same as the pattern character; in the U.S. locale, 174 * the ',' grouping character is replaced by ','. However, the replacement is 175 * still happening, and if the symbols are modified, the grouping character 176 * changes. Some special characters affect the behavior of the formatter by 177 * their presence; for example, if the percent character is seen, then the 178 * value is multiplied by 100 before being displayed. 179 * 180 * <p>To insert a special character in a pattern as a literal, that is, without 181 * any special meaning, the character must be quoted. There are some exceptions to 182 * this which are noted below. 183 * 184 * <p>The characters listed here are used in non-localized patterns. Localized 185 * patterns use the corresponding characters taken from this formatter's 186 * DecimalFormatSymbols object instead, and these characters lose 187 * their special status. Two exceptions are the currency sign and quote, which 188 * are not localized. 189 * 190 * <table border=0 cellspacing=3 cellpadding=0> 191 * <tr bgcolor="#ccccff"> 192 * <td align=left><strong>Symbol</strong> 193 * <td align=left><strong>Location</strong> 194 * <td align=left><strong>Localized?</strong> 195 * <td align=left><strong>Meaning</strong> 196 * <tr valign=top> 197 * <td><code>0</code> 198 * <td>Number 199 * <td>Yes 200 * <td>Digit 201 * <tr valign=top bgcolor="#eeeeff"> 202 * <td><code>1-9</code> 203 * <td>Number 204 * <td>Yes 205 * <td>'1' through '9' indicate rounding. 206 * <tr valign=top> 207 * <td><code>\htmlonly@\endhtmlonly</code> <!--doxygen doesn't like @--> 208 * <td>Number 209 * <td>No 210 * <td>Significant digit 211 * <tr valign=top bgcolor="#eeeeff"> 212 * <td><code>#</code> 213 * <td>Number 214 * <td>Yes 215 * <td>Digit, zero shows as absent 216 * <tr valign=top> 217 * <td><code>.</code> 218 * <td>Number 219 * <td>Yes 220 * <td>Decimal separator or monetary decimal separator 221 * <tr valign=top bgcolor="#eeeeff"> 222 * <td><code>-</code> 223 * <td>Number 224 * <td>Yes 225 * <td>Minus sign 226 * <tr valign=top> 227 * <td><code>,</code> 228 * <td>Number 229 * <td>Yes 230 * <td>Grouping separator 231 * <tr valign=top bgcolor="#eeeeff"> 232 * <td><code>E</code> 233 * <td>Number 234 * <td>Yes 235 * <td>Separates mantissa and exponent in scientific notation. 236 * <em>Need not be quoted in prefix or suffix.</em> 237 * <tr valign=top> 238 * <td><code>+</code> 239 * <td>Exponent 240 * <td>Yes 241 * <td>Prefix positive exponents with localized plus sign. 242 * <em>Need not be quoted in prefix or suffix.</em> 243 * <tr valign=top bgcolor="#eeeeff"> 244 * <td><code>;</code> 245 * <td>Subpattern boundary 246 * <td>Yes 247 * <td>Separates positive and negative subpatterns 248 * <tr valign=top> 249 * <td><code>\%</code> 250 * <td>Prefix or suffix 251 * <td>Yes 252 * <td>Multiply by 100 and show as percentage 253 * <tr valign=top bgcolor="#eeeeff"> 254 * <td><code>\\u2030</code> 255 * <td>Prefix or suffix 256 * <td>Yes 257 * <td>Multiply by 1000 and show as per mille 258 * <tr valign=top> 259 * <td><code>\htmlonly¤\endhtmlonly</code> (<code>\\u00A4</code>) 260 * <td>Prefix or suffix 261 * <td>No 262 * <td>Currency sign, replaced by currency symbol. If 263 * doubled, replaced by international currency symbol. 264 * If tripled, replaced by currency plural names, for example, 265 * "US dollar" or "US dollars" for America. 266 * If present in a pattern, the monetary decimal separator 267 * is used instead of the decimal separator. 268 * <tr valign=top bgcolor="#eeeeff"> 269 * <td><code>'</code> 270 * <td>Prefix or suffix 271 * <td>No 272 * <td>Used to quote special characters in a prefix or suffix, 273 * for example, <code>"'#'#"</code> formats 123 to 274 * <code>"#123"</code>. To create a single quote 275 * itself, use two in a row: <code>"# o''clock"</code>. 276 * <tr valign=top> 277 * <td><code>*</code> 278 * <td>Prefix or suffix boundary 279 * <td>Yes 280 * <td>Pad escape, precedes pad character 281 * </table> 282 * 283 * <p>A DecimalFormat pattern contains a postive and negative 284 * subpattern, for example, "#,##0.00;(#,##0.00)". Each subpattern has a 285 * prefix, a numeric part, and a suffix. If there is no explicit negative 286 * subpattern, the negative subpattern is the localized minus sign prefixed to the 287 * positive subpattern. That is, "0.00" alone is equivalent to "0.00;-0.00". If there 288 * is an explicit negative subpattern, it serves only to specify the negative 289 * prefix and suffix; the number of digits, minimal digits, and other 290 * characteristics are ignored in the negative subpattern. That means that 291 * "#,##0.0#;(#)" has precisely the same result as "#,##0.0#;(#,##0.0#)". 292 * 293 * <p>The prefixes, suffixes, and various symbols used for infinity, digits, 294 * thousands separators, decimal separators, etc. may be set to arbitrary 295 * values, and they will appear properly during formatting. However, care must 296 * be taken that the symbols and strings do not conflict, or parsing will be 297 * unreliable. For example, either the positive and negative prefixes or the 298 * suffixes must be distinct for parse() to be able 299 * to distinguish positive from negative values. Another example is that the 300 * decimal separator and thousands separator should be distinct characters, or 301 * parsing will be impossible. 302 * 303 * <p>The <em>grouping separator</em> is a character that separates clusters of 304 * integer digits to make large numbers more legible. It commonly used for 305 * thousands, but in some locales it separates ten-thousands. The <em>grouping 306 * size</em> is the number of digits between the grouping separators, such as 3 307 * for "100,000,000" or 4 for "1 0000 0000". There are actually two different 308 * grouping sizes: One used for the least significant integer digits, the 309 * <em>primary grouping size</em>, and one used for all others, the 310 * <em>secondary grouping size</em>. In most locales these are the same, but 311 * sometimes they are different. For example, if the primary grouping interval 312 * is 3, and the secondary is 2, then this corresponds to the pattern 313 * "#,##,##0", and the number 123456789 is formatted as "12,34,56,789". If a 314 * pattern contains multiple grouping separators, the interval between the last 315 * one and the end of the integer defines the primary grouping size, and the 316 * interval between the last two defines the secondary grouping size. All others 317 * are ignored, so "#,##,###,####" == "###,###,####" == "##,#,###,####". 318 * 319 * <p>Illegal patterns, such as "#.#.#" or "#.###,###", will cause 320 * DecimalFormat to set a failing UErrorCode. 321 * 322 * <p><strong>Pattern BNF</strong> 323 * 324 * <pre> 325 * pattern := subpattern (';' subpattern)? 326 * subpattern := prefix? number exponent? suffix? 327 * number := (integer ('.' fraction)?) | sigDigits 328 * prefix := '\\u0000'..'\\uFFFD' - specialCharacters 329 * suffix := '\\u0000'..'\\uFFFD' - specialCharacters 330 * integer := '#'* '0'* '0' 331 * fraction := '0'* '#'* 332 * sigDigits := '#'* '@' '@'* '#'* 333 * exponent := 'E' '+'? '0'* '0' 334 * padSpec := '*' padChar 335 * padChar := '\\u0000'..'\\uFFFD' - quote 336 * 337 * Notation: 338 * X* 0 or more instances of X 339 * X? 0 or 1 instances of X 340 * X|Y either X or Y 341 * C..D any character from C up to D, inclusive 342 * S-T characters in S, except those in T 343 * </pre> 344 * The first subpattern is for positive numbers. The second (optional) 345 * subpattern is for negative numbers. 346 * 347 * <p>Not indicated in the BNF syntax above: 348 * 349 * <ul><li>The grouping separator ',' can occur inside the integer and 350 * sigDigits elements, between any two pattern characters of that 351 * element, as long as the integer or sigDigits element is not 352 * followed by the exponent element. 353 * 354 * <li>Two grouping intervals are recognized: That between the 355 * decimal point and the first grouping symbol, and that 356 * between the first and second grouping symbols. These 357 * intervals are identical in most locales, but in some 358 * locales they differ. For example, the pattern 359 * "#,##,###" formats the number 123456789 as 360 * "12,34,56,789".</li> 361 * 362 * <li>The pad specifier <code>padSpec</code> may appear before the prefix, 363 * after the prefix, before the suffix, after the suffix, or not at all. 364 * 365 * <li>In place of '0', the digits '1' through '9' may be used to 366 * indicate a rounding increment. 367 * </ul> 368 * 369 * <p><strong>Parsing</strong> 370 * 371 * <p>DecimalFormat parses all Unicode characters that represent 372 * decimal digits, as defined by u_charDigitValue(). In addition, 373 * DecimalFormat also recognizes as digits the ten consecutive 374 * characters starting with the localized zero digit defined in the 375 * DecimalFormatSymbols object. During formatting, the 376 * DecimalFormatSymbols-based digits are output. 377 * 378 * <p>During parsing, grouping separators are ignored if in lenient mode; 379 * otherwise, if present, they must be in appropriate positions. 380 * 381 * <p>For currency parsing, the formatter is able to parse every currency 382 * style formats no matter which style the formatter is constructed with. 383 * For example, a formatter instance gotten from 384 * NumberFormat.getInstance(ULocale, NumberFormat.CURRENCYSTYLE) can parse 385 * formats such as "USD1.00" and "3.00 US dollars". 386 * 387 * <p>If parse(UnicodeString&,Formattable&,ParsePosition&) 388 * fails to parse a string, it leaves the parse position unchanged. 389 * The convenience method parse(UnicodeString&,Formattable&,UErrorCode&) 390 * indicates parse failure by setting a failing 391 * UErrorCode. 392 * 393 * <p><strong>Formatting</strong> 394 * 395 * <p>Formatting is guided by several parameters, all of which can be 396 * specified either using a pattern or using the API. The following 397 * description applies to formats that do not use <a href="#sci">scientific 398 * notation</a> or <a href="#sigdig">significant digits</a>. 399 * 400 * <ul><li>If the number of actual integer digits exceeds the 401 * <em>maximum integer digits</em>, then only the least significant 402 * digits are shown. For example, 1997 is formatted as "97" if the 403 * maximum integer digits is set to 2. 404 * 405 * <li>If the number of actual integer digits is less than the 406 * <em>minimum integer digits</em>, then leading zeros are added. For 407 * example, 1997 is formatted as "01997" if the minimum integer digits 408 * is set to 5. 409 * 410 * <li>If the number of actual fraction digits exceeds the <em>maximum 411 * fraction digits</em>, then rounding is performed to the 412 * maximum fraction digits. For example, 0.125 is formatted as "0.12" 413 * if the maximum fraction digits is 2. This behavior can be changed 414 * by specifying a rounding increment and/or a rounding mode. 415 * 416 * <li>If the number of actual fraction digits is less than the 417 * <em>minimum fraction digits</em>, then trailing zeros are added. 418 * For example, 0.125 is formatted as "0.1250" if the mimimum fraction 419 * digits is set to 4. 420 * 421 * <li>Trailing fractional zeros are not displayed if they occur 422 * <em>j</em> positions after the decimal, where <em>j</em> is less 423 * than the maximum fraction digits. For example, 0.10004 is 424 * formatted as "0.1" if the maximum fraction digits is four or less. 425 * </ul> 426 * 427 * <p><strong>Special Values</strong> 428 * 429 * <p><code>NaN</code> is represented as a single character, typically 430 * <code>\\uFFFD</code>. This character is determined by the 431 * DecimalFormatSymbols object. This is the only value for which 432 * the prefixes and suffixes are not used. 433 * 434 * <p>Infinity is represented as a single character, typically 435 * <code>\\u221E</code>, with the positive or negative prefixes and suffixes 436 * applied. The infinity character is determined by the 437 * DecimalFormatSymbols object. 438 * 439 * <a name="sci"><strong>Scientific Notation</strong></a> 440 * 441 * <p>Numbers in scientific notation are expressed as the product of a mantissa 442 * and a power of ten, for example, 1234 can be expressed as 1.234 x 10<sup>3</sup>. The 443 * mantissa is typically in the half-open interval [1.0, 10.0) or sometimes [0.0, 1.0), 444 * but it need not be. DecimalFormat supports arbitrary mantissas. 445 * DecimalFormat can be instructed to use scientific 446 * notation through the API or through the pattern. In a pattern, the exponent 447 * character immediately followed by one or more digit characters indicates 448 * scientific notation. Example: "0.###E0" formats the number 1234 as 449 * "1.234E3". 450 * 451 * <ul> 452 * <li>The number of digit characters after the exponent character gives the 453 * minimum exponent digit count. There is no maximum. Negative exponents are 454 * formatted using the localized minus sign, <em>not</em> the prefix and suffix 455 * from the pattern. This allows patterns such as "0.###E0 m/s". To prefix 456 * positive exponents with a localized plus sign, specify '+' between the 457 * exponent and the digits: "0.###E+0" will produce formats "1E+1", "1E+0", 458 * "1E-1", etc. (In localized patterns, use the localized plus sign rather than 459 * '+'.) 460 * 461 * <li>The minimum number of integer digits is achieved by adjusting the 462 * exponent. Example: 0.00123 formatted with "00.###E0" yields "12.3E-4". This 463 * only happens if there is no maximum number of integer digits. If there is a 464 * maximum, then the minimum number of integer digits is fixed at one. 465 * 466 * <li>The maximum number of integer digits, if present, specifies the exponent 467 * grouping. The most common use of this is to generate <em>engineering 468 * notation</em>, in which the exponent is a multiple of three, e.g., 469 * "##0.###E0". The number 12345 is formatted using "##0.####E0" as "12.345E3". 470 * 471 * <li>When using scientific notation, the formatter controls the 472 * digit counts using significant digits logic. The maximum number of 473 * significant digits limits the total number of integer and fraction 474 * digits that will be shown in the mantissa; it does not affect 475 * parsing. For example, 12345 formatted with "##0.##E0" is "12.3E3". 476 * See the section on significant digits for more details. 477 * 478 * <li>The number of significant digits shown is determined as 479 * follows: If areSignificantDigitsUsed() returns false, then the 480 * minimum number of significant digits shown is one, and the maximum 481 * number of significant digits shown is the sum of the <em>minimum 482 * integer</em> and <em>maximum fraction</em> digits, and is 483 * unaffected by the maximum integer digits. If this sum is zero, 484 * then all significant digits are shown. If 485 * areSignificantDigitsUsed() returns true, then the significant digit 486 * counts are specified by getMinimumSignificantDigits() and 487 * getMaximumSignificantDigits(). In this case, the number of 488 * integer digits is fixed at one, and there is no exponent grouping. 489 * 490 * <li>Exponential patterns may not contain grouping separators. 491 * </ul> 492 * 493 * <a name="sigdig"><strong>Significant Digits</strong></a> 494 * 495 * <code>DecimalFormat</code> has two ways of controlling how many 496 * digits are shows: (a) significant digits counts, or (b) integer and 497 * fraction digit counts. Integer and fraction digit counts are 498 * described above. When a formatter is using significant digits 499 * counts, the number of integer and fraction digits is not specified 500 * directly, and the formatter settings for these counts are ignored. 501 * Instead, the formatter uses however many integer and fraction 502 * digits are required to display the specified number of significant 503 * digits. Examples: 504 * 505 * <table border=0 cellspacing=3 cellpadding=0> 506 * <tr bgcolor="#ccccff"> 507 * <td align=left>Pattern 508 * <td align=left>Minimum significant digits 509 * <td align=left>Maximum significant digits 510 * <td align=left>Number 511 * <td align=left>Output of format() 512 * <tr valign=top> 513 * <td><code>\@\@\@</code> 514 * <td>3 515 * <td>3 516 * <td>12345 517 * <td><code>12300</code> 518 * <tr valign=top bgcolor="#eeeeff"> 519 * <td><code>\@\@\@</code> 520 * <td>3 521 * <td>3 522 * <td>0.12345 523 * <td><code>0.123</code> 524 * <tr valign=top> 525 * <td><code>\@\@##</code> 526 * <td>2 527 * <td>4 528 * <td>3.14159 529 * <td><code>3.142</code> 530 * <tr valign=top bgcolor="#eeeeff"> 531 * <td><code>\@\@##</code> 532 * <td>2 533 * <td>4 534 * <td>1.23004 535 * <td><code>1.23</code> 536 * </table> 537 * 538 * <ul> 539 * <li>Significant digit counts may be expressed using patterns that 540 * specify a minimum and maximum number of significant digits. These 541 * are indicated by the <code>'@'</code> and <code>'#'</code> 542 * characters. The minimum number of significant digits is the number 543 * of <code>'@'</code> characters. The maximum number of significant 544 * digits is the number of <code>'@'</code> characters plus the number 545 * of <code>'#'</code> characters following on the right. For 546 * example, the pattern <code>"@@@"</code> indicates exactly 3 547 * significant digits. The pattern <code>"@##"</code> indicates from 548 * 1 to 3 significant digits. Trailing zero digits to the right of 549 * the decimal separator are suppressed after the minimum number of 550 * significant digits have been shown. For example, the pattern 551 * <code>"@##"</code> formats the number 0.1203 as 552 * <code>"0.12"</code>. 553 * 554 * <li>If a pattern uses significant digits, it may not contain a 555 * decimal separator, nor the <code>'0'</code> pattern character. 556 * Patterns such as <code>"@00"</code> or <code>"@.###"</code> are 557 * disallowed. 558 * 559 * <li>Any number of <code>'#'</code> characters may be prepended to 560 * the left of the leftmost <code>'@'</code> character. These have no 561 * effect on the minimum and maximum significant digits counts, but 562 * may be used to position grouping separators. For example, 563 * <code>"#,#@#"</code> indicates a minimum of one significant digits, 564 * a maximum of two significant digits, and a grouping size of three. 565 * 566 * <li>In order to enable significant digits formatting, use a pattern 567 * containing the <code>'@'</code> pattern character. Alternatively, 568 * call setSignificantDigitsUsed(TRUE). 569 * 570 * <li>In order to disable significant digits formatting, use a 571 * pattern that does not contain the <code>'@'</code> pattern 572 * character. Alternatively, call setSignificantDigitsUsed(FALSE). 573 * 574 * <li>The number of significant digits has no effect on parsing. 575 * 576 * <li>Significant digits may be used together with exponential notation. Such 577 * patterns are equivalent to a normal exponential pattern with a minimum and 578 * maximum integer digit count of one, a minimum fraction digit count of 579 * <code>getMinimumSignificantDigits() - 1</code>, and a maximum fraction digit 580 * count of <code>getMaximumSignificantDigits() - 1</code>. For example, the 581 * pattern <code>"@@###E0"</code> is equivalent to <code>"0.0###E0"</code>. 582 * 583 * <li>If signficant digits are in use, then the integer and fraction 584 * digit counts, as set via the API, are ignored. If significant 585 * digits are not in use, then the signficant digit counts, as set via 586 * the API, are ignored. 587 * 588 * </ul> 589 * 590 * <p><strong>Padding</strong> 591 * 592 * <p>DecimalFormat supports padding the result of 593 * format() to a specific width. Padding may be specified either 594 * through the API or through the pattern syntax. In a pattern the pad escape 595 * character, followed by a single pad character, causes padding to be parsed 596 * and formatted. The pad escape character is '*' in unlocalized patterns, and 597 * can be localized using DecimalFormatSymbols::setSymbol() with a 598 * DecimalFormatSymbols::kPadEscapeSymbol 599 * selector. For example, <code>"$*x#,##0.00"</code> formats 123 to 600 * <code>"$xx123.00"</code>, and 1234 to <code>"$1,234.00"</code>. 601 * 602 * <ul> 603 * <li>When padding is in effect, the width of the positive subpattern, 604 * including prefix and suffix, determines the format width. For example, in 605 * the pattern <code>"* #0 o''clock"</code>, the format width is 10. 606 * 607 * <li>The width is counted in 16-bit code units (char16_ts). 608 * 609 * <li>Some parameters which usually do not matter have meaning when padding is 610 * used, because the pattern width is significant with padding. In the pattern 611 * "* ##,##,#,##0.##", the format width is 14. The initial characters "##,##," 612 * do not affect the grouping size or maximum integer digits, but they do affect 613 * the format width. 614 * 615 * <li>Padding may be inserted at one of four locations: before the prefix, 616 * after the prefix, before the suffix, or after the suffix. If padding is 617 * specified in any other location, applyPattern() 618 * sets a failing UErrorCode. If there is no prefix, 619 * before the prefix and after the prefix are equivalent, likewise for the 620 * suffix. 621 * 622 * <li>When specified in a pattern, the 32-bit code point immediately 623 * following the pad escape is the pad character. This may be any character, 624 * including a special pattern character. That is, the pad escape 625 * <em>escapes</em> the following character. If there is no character after 626 * the pad escape, then the pattern is illegal. 627 * 628 * </ul> 629 * 630 * <p><strong>Rounding</strong> 631 * 632 * <p>DecimalFormat supports rounding to a specific increment. For 633 * example, 1230 rounded to the nearest 50 is 1250. 1.234 rounded to the 634 * nearest 0.65 is 1.3. The rounding increment may be specified through the API 635 * or in a pattern. To specify a rounding increment in a pattern, include the 636 * increment in the pattern itself. "#,#50" specifies a rounding increment of 637 * 50. "#,##0.05" specifies a rounding increment of 0.05. 638 * 639 * <p>In the absense of an explicit rounding increment numbers are 640 * rounded to their formatted width. 641 * 642 * <ul> 643 * <li>Rounding only affects the string produced by formatting. It does 644 * not affect parsing or change any numerical values. 645 * 646 * <li>A <em>rounding mode</em> determines how values are rounded; see 647 * DecimalFormat::ERoundingMode. The default rounding mode is 648 * DecimalFormat::kRoundHalfEven. The rounding mode can only be set 649 * through the API; it can not be set with a pattern. 650 * 651 * <li>Some locales use rounding in their currency formats to reflect the 652 * smallest currency denomination. 653 * 654 * <li>In a pattern, digits '1' through '9' specify rounding, but otherwise 655 * behave identically to digit '0'. 656 * </ul> 657 * 658 * <p><strong>Synchronization</strong> 659 * 660 * <p>DecimalFormat objects are not synchronized. Multiple 661 * threads should not access one formatter concurrently. 662 * 663 * <p><strong>Subclassing</strong> 664 * 665 * <p><em>User subclasses are not supported.</em> While clients may write 666 * subclasses, such code will not necessarily work and will not be 667 * guaranteed to work stably from release to release. 668 */ 669 class U_I18N_API DecimalFormat: public NumberFormat { 670 public: 671 /** 672 * Pad position. 673 * @stable ICU 2.4 674 */ 675 enum EPadPosition { 676 kPadBeforePrefix, 677 kPadAfterPrefix, 678 kPadBeforeSuffix, 679 kPadAfterSuffix 680 }; 681 682 /** 683 * Create a DecimalFormat using the default pattern and symbols 684 * for the default locale. This is a convenient way to obtain a 685 * DecimalFormat when internationalization is not the main concern. 686 * <P> 687 * To obtain standard formats for a given locale, use the factory methods 688 * on NumberFormat such as createInstance. These factories will 689 * return the most appropriate sub-class of NumberFormat for a given 690 * locale. 691 * @param status Output param set to success/failure code. If the 692 * pattern is invalid this will be set to a failure code. 693 * @stable ICU 2.0 694 */ 695 DecimalFormat(UErrorCode& status); 696 697 /** 698 * Create a DecimalFormat from the given pattern and the symbols 699 * for the default locale. This is a convenient way to obtain a 700 * DecimalFormat when internationalization is not the main concern. 701 * <P> 702 * To obtain standard formats for a given locale, use the factory methods 703 * on NumberFormat such as createInstance. These factories will 704 * return the most appropriate sub-class of NumberFormat for a given 705 * locale. 706 * @param pattern A non-localized pattern string. 707 * @param status Output param set to success/failure code. If the 708 * pattern is invalid this will be set to a failure code. 709 * @stable ICU 2.0 710 */ 711 DecimalFormat(const UnicodeString& pattern, 712 UErrorCode& status); 713 714 /** 715 * Create a DecimalFormat from the given pattern and symbols. 716 * Use this constructor when you need to completely customize the 717 * behavior of the format. 718 * <P> 719 * To obtain standard formats for a given 720 * locale, use the factory methods on NumberFormat such as 721 * createInstance or createCurrencyInstance. If you need only minor adjustments 722 * to a standard format, you can modify the format returned by 723 * a NumberFormat factory method. 724 * 725 * @param pattern a non-localized pattern string 726 * @param symbolsToAdopt the set of symbols to be used. The caller should not 727 * delete this object after making this call. 728 * @param status Output param set to success/failure code. If the 729 * pattern is invalid this will be set to a failure code. 730 * @stable ICU 2.0 731 */ 732 DecimalFormat( const UnicodeString& pattern, 733 DecimalFormatSymbols* symbolsToAdopt, 734 UErrorCode& status); 735 736 #ifndef U_HIDE_INTERNAL_API 737 /** 738 * This API is for ICU use only. 739 * Create a DecimalFormat from the given pattern, symbols, and style. 740 * 741 * @param pattern a non-localized pattern string 742 * @param symbolsToAdopt the set of symbols to be used. The caller should not 743 * delete this object after making this call. 744 * @param style style of decimal format 745 * @param status Output param set to success/failure code. If the 746 * pattern is invalid this will be set to a failure code. 747 * @internal 748 */ 749 DecimalFormat( const UnicodeString& pattern, 750 DecimalFormatSymbols* symbolsToAdopt, 751 UNumberFormatStyle style, 752 UErrorCode& status); 753 754 #if UCONFIG_HAVE_PARSEALLINPUT 755 /** 756 * @internal 757 */ 758 void setParseAllInput(UNumberFormatAttributeValue value); 759 #endif 760 761 #endif /* U_HIDE_INTERNAL_API */ 762 763 764 /** 765 * Set an integer attribute on this DecimalFormat. 766 * May return U_UNSUPPORTED_ERROR if this instance does not support 767 * the specified attribute. 768 * @param attr the attribute to set 769 * @param newvalue new value 770 * @param status the error type 771 * @return *this - for chaining (example: format.setAttribute(...).setAttribute(...) ) 772 * @stable ICU 51 773 */ 774 virtual DecimalFormat& setAttribute( UNumberFormatAttribute attr, 775 int32_t newvalue, 776 UErrorCode &status); 777 778 /** 779 * Get an integer 780 * May return U_UNSUPPORTED_ERROR if this instance does not support 781 * the specified attribute. 782 * @param attr the attribute to set 783 * @param status the error type 784 * @return the attribute value. Undefined if there is an error. 785 * @stable ICU 51 786 */ 787 virtual int32_t getAttribute( UNumberFormatAttribute attr, 788 UErrorCode &status) const; 789 790 791 /** 792 * Set whether or not grouping will be used in this format. 793 * @param newValue True, grouping will be used in this format. 794 * @see getGroupingUsed 795 * @stable ICU 53 796 */ 797 virtual void setGroupingUsed(UBool newValue); 798 799 /** 800 * Sets whether or not numbers should be parsed as integers only. 801 * @param value set True, this format will parse numbers as integers 802 * only. 803 * @see isParseIntegerOnly 804 * @stable ICU 53 805 */ 806 virtual void setParseIntegerOnly(UBool value); 807 808 /** 809 * Set a particular UDisplayContext value in the formatter, such as 810 * UDISPCTX_CAPITALIZATION_FOR_STANDALONE. 811 * @param value The UDisplayContext value to set. 812 * @param status Input/output status. If at entry this indicates a failure 813 * status, the function will do nothing; otherwise this will be 814 * updated with any new status from the function. 815 * @stable ICU 53 816 */ 817 virtual void setContext(UDisplayContext value, UErrorCode& status); 818 819 /** 820 * Create a DecimalFormat from the given pattern and symbols. 821 * Use this constructor when you need to completely customize the 822 * behavior of the format. 823 * <P> 824 * To obtain standard formats for a given 825 * locale, use the factory methods on NumberFormat such as 826 * createInstance or createCurrencyInstance. If you need only minor adjustments 827 * to a standard format, you can modify the format returned by 828 * a NumberFormat factory method. 829 * 830 * @param pattern a non-localized pattern string 831 * @param symbolsToAdopt the set of symbols to be used. The caller should not 832 * delete this object after making this call. 833 * @param parseError Output param to receive errors occured during parsing 834 * @param status Output param set to success/failure code. If the 835 * pattern is invalid this will be set to a failure code. 836 * @stable ICU 2.0 837 */ 838 DecimalFormat( const UnicodeString& pattern, 839 DecimalFormatSymbols* symbolsToAdopt, 840 UParseError& parseError, 841 UErrorCode& status); 842 /** 843 * Create a DecimalFormat from the given pattern and symbols. 844 * Use this constructor when you need to completely customize the 845 * behavior of the format. 846 * <P> 847 * To obtain standard formats for a given 848 * locale, use the factory methods on NumberFormat such as 849 * createInstance or createCurrencyInstance. If you need only minor adjustments 850 * to a standard format, you can modify the format returned by 851 * a NumberFormat factory method. 852 * 853 * @param pattern a non-localized pattern string 854 * @param symbols the set of symbols to be used 855 * @param status Output param set to success/failure code. If the 856 * pattern is invalid this will be set to a failure code. 857 * @stable ICU 2.0 858 */ 859 DecimalFormat( const UnicodeString& pattern, 860 const DecimalFormatSymbols& symbols, 861 UErrorCode& status); 862 863 /** 864 * Copy constructor. 865 * 866 * @param source the DecimalFormat object to be copied from. 867 * @stable ICU 2.0 868 */ 869 DecimalFormat(const DecimalFormat& source); 870 871 /** 872 * Assignment operator. 873 * 874 * @param rhs the DecimalFormat object to be copied. 875 * @stable ICU 2.0 876 */ 877 DecimalFormat& operator=(const DecimalFormat& rhs); 878 879 /** 880 * Destructor. 881 * @stable ICU 2.0 882 */ 883 virtual ~DecimalFormat(); 884 885 /** 886 * Clone this Format object polymorphically. The caller owns the 887 * result and should delete it when done. 888 * 889 * @return a polymorphic copy of this DecimalFormat. 890 * @stable ICU 2.0 891 */ 892 virtual Format* clone(void) const; 893 894 /** 895 * Return true if the given Format objects are semantically equal. 896 * Objects of different subclasses are considered unequal. 897 * 898 * @param other the object to be compared with. 899 * @return true if the given Format objects are semantically equal. 900 * @stable ICU 2.0 901 */ 902 virtual UBool operator==(const Format& other) const; 903 904 905 using NumberFormat::format; 906 907 /** 908 * Format a double or long number using base-10 representation. 909 * 910 * @param number The value to be formatted. 911 * @param appendTo Output parameter to receive result. 912 * Result is appended to existing contents. 913 * @param pos On input: an alignment field, if desired. 914 * On output: the offsets of the alignment field. 915 * @return Reference to 'appendTo' parameter. 916 * @stable ICU 2.0 917 */ 918 virtual UnicodeString& format(double number, 919 UnicodeString& appendTo, 920 FieldPosition& pos) const; 921 922 923 /** 924 * Format a double or long number using base-10 representation. 925 * 926 * @param number The value to be formatted. 927 * @param appendTo Output parameter to receive result. 928 * Result is appended to existing contents. 929 * @param pos On input: an alignment field, if desired. 930 * On output: the offsets of the alignment field. 931 * @param status 932 * @return Reference to 'appendTo' parameter. 933 * @internal 934 */ 935 virtual UnicodeString& format(double number, 936 UnicodeString& appendTo, 937 FieldPosition& pos, 938 UErrorCode &status) const; 939 940 /** 941 * Format a double or long number using base-10 representation. 942 * 943 * @param number The value to be formatted. 944 * @param appendTo Output parameter to receive result. 945 * Result is appended to existing contents. 946 * @param posIter On return, can be used to iterate over positions 947 * of fields generated by this format call. 948 * Can be NULL. 949 * @param status Output param filled with success/failure status. 950 * @return Reference to 'appendTo' parameter. 951 * @stable ICU 4.4 952 */ 953 virtual UnicodeString& format(double number, 954 UnicodeString& appendTo, 955 FieldPositionIterator* posIter, 956 UErrorCode& status) const; 957 958 /** 959 * Format a long number using base-10 representation. 960 * 961 * @param number The value to be formatted. 962 * @param appendTo Output parameter to receive result. 963 * Result is appended to existing contents. 964 * @param pos On input: an alignment field, if desired. 965 * On output: the offsets of the alignment field. 966 * @return Reference to 'appendTo' parameter. 967 * @stable ICU 2.0 968 */ 969 virtual UnicodeString& format(int32_t number, 970 UnicodeString& appendTo, 971 FieldPosition& pos) const; 972 973 /** 974 * Format a long number using base-10 representation. 975 * 976 * @param number The value to be formatted. 977 * @param appendTo Output parameter to receive result. 978 * Result is appended to existing contents. 979 * @param pos On input: an alignment field, if desired. 980 * On output: the offsets of the alignment field. 981 * @return Reference to 'appendTo' parameter. 982 * @internal 983 */ 984 virtual UnicodeString& format(int32_t number, 985 UnicodeString& appendTo, 986 FieldPosition& pos, 987 UErrorCode &status) const; 988 989 /** 990 * Format a long number using base-10 representation. 991 * 992 * @param number The value to be formatted. 993 * @param appendTo Output parameter to receive result. 994 * Result is appended to existing contents. 995 * @param posIter On return, can be used to iterate over positions 996 * of fields generated by this format call. 997 * Can be NULL. 998 * @param status Output param filled with success/failure status. 999 * @return Reference to 'appendTo' parameter. 1000 * @stable ICU 4.4 1001 */ 1002 virtual UnicodeString& format(int32_t number, 1003 UnicodeString& appendTo, 1004 FieldPositionIterator* posIter, 1005 UErrorCode& status) const; 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 virtual UnicodeString& format(int64_t number, 1019 UnicodeString& appendTo, 1020 FieldPosition& pos) const; 1021 1022 /** 1023 * Format an int64 number using base-10 representation. 1024 * 1025 * @param number The value to be formatted. 1026 * @param appendTo Output parameter to receive result. 1027 * Result is appended to existing contents. 1028 * @param pos On input: an alignment field, if desired. 1029 * On output: the offsets of the alignment field. 1030 * @return Reference to 'appendTo' parameter. 1031 * @internal 1032 */ 1033 virtual UnicodeString& format(int64_t number, 1034 UnicodeString& appendTo, 1035 FieldPosition& pos, 1036 UErrorCode &status) const; 1037 1038 /** 1039 * Format an int64 number using base-10 representation. 1040 * 1041 * @param number The value to be formatted. 1042 * @param appendTo Output parameter to receive result. 1043 * Result is appended to existing contents. 1044 * @param posIter On return, can be used to iterate over positions 1045 * of fields generated by this format call. 1046 * Can be NULL. 1047 * @param status Output param filled with success/failure status. 1048 * @return Reference to 'appendTo' parameter. 1049 * @stable ICU 4.4 1050 */ 1051 virtual UnicodeString& format(int64_t number, 1052 UnicodeString& appendTo, 1053 FieldPositionIterator* posIter, 1054 UErrorCode& status) const; 1055 1056 /** 1057 * Format a decimal number. 1058 * The syntax of the unformatted number is a "numeric string" 1059 * as defined in the Decimal Arithmetic Specification, available at 1060 * http://speleotrove.com/decimal 1061 * 1062 * @param number The unformatted number, as a string. 1063 * @param appendTo Output parameter to receive result. 1064 * Result is appended to existing contents. 1065 * @param posIter On return, can be used to iterate over positions 1066 * of fields generated by this format call. 1067 * Can be NULL. 1068 * @param status Output param filled with success/failure status. 1069 * @return Reference to 'appendTo' parameter. 1070 * @stable ICU 4.4 1071 */ 1072 virtual UnicodeString& format(StringPiece number, 1073 UnicodeString& appendTo, 1074 FieldPositionIterator* posIter, 1075 UErrorCode& status) const; 1076 1077 1078 /** 1079 * Format a decimal number. 1080 * The number is a DigitList wrapper onto a floating point decimal number. 1081 * The default implementation in NumberFormat converts the decimal number 1082 * to a double and formats that. 1083 * 1084 * @param number The number, a DigitList format Decimal Floating Point. 1085 * @param appendTo Output parameter to receive result. 1086 * Result is appended to existing contents. 1087 * @param posIter On return, can be used to iterate over positions 1088 * of fields generated by this format call. 1089 * @param status Output param filled with success/failure status. 1090 * @return Reference to 'appendTo' parameter. 1091 * @internal 1092 */ 1093 virtual UnicodeString& format(const DigitList &number, 1094 UnicodeString& appendTo, 1095 FieldPositionIterator* posIter, 1096 UErrorCode& status) const; 1097 1098 /** 1099 * Format a decimal number. 1100 * @param number The number 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 virtual UnicodeString& format( 1110 const VisibleDigitsWithExponent &number, 1111 UnicodeString& appendTo, 1112 FieldPosition& pos, 1113 UErrorCode& status) const; 1114 1115 /** 1116 * Format a decimal number. 1117 * @param number The number 1118 * @param appendTo Output parameter to receive result. 1119 * Result is appended to existing contents. 1120 * @param posIter On return, can be used to iterate over positions 1121 * of fields generated by this format call. 1122 * @param status Output param filled with success/failure status. 1123 * @return Reference to 'appendTo' parameter. 1124 * @internal 1125 */ 1126 virtual UnicodeString& format( 1127 const VisibleDigitsWithExponent &number, 1128 UnicodeString& appendTo, 1129 FieldPositionIterator* posIter, 1130 UErrorCode& status) const; 1131 1132 /** 1133 * Format a decimal number. 1134 * The number is a DigitList wrapper onto a floating point decimal number. 1135 * The default implementation in NumberFormat converts the decimal number 1136 * to a double and formats that. 1137 * 1138 * @param number The number, a DigitList format Decimal Floating Point. 1139 * @param appendTo Output parameter to receive result. 1140 * Result is appended to existing contents. 1141 * @param pos On input: an alignment field, if desired. 1142 * On output: the offsets of the alignment field. 1143 * @param status Output param filled with success/failure status. 1144 * @return Reference to 'appendTo' parameter. 1145 * @internal 1146 */ 1147 virtual UnicodeString& format(const DigitList &number, 1148 UnicodeString& appendTo, 1149 FieldPosition& pos, 1150 UErrorCode& status) const; 1151 1152 using NumberFormat::parse; 1153 1154 /** 1155 * Parse the given string using this object's choices. The method 1156 * does string comparisons to try to find an optimal match. 1157 * If no object can be parsed, index is unchanged, and NULL is 1158 * returned. The result is returned as the most parsimonious 1159 * type of Formattable that will accomodate all of the 1160 * necessary precision. For example, if the result is exactly 12, 1161 * it will be returned as a long. However, if it is 1.5, it will 1162 * be returned as a double. 1163 * 1164 * @param text The text to be parsed. 1165 * @param result Formattable to be set to the parse result. 1166 * If parse fails, return contents are undefined. 1167 * @param parsePosition The position to start parsing at on input. 1168 * On output, moved to after the last successfully 1169 * parse character. On parse failure, does not change. 1170 * @see Formattable 1171 * @stable ICU 2.0 1172 */ 1173 virtual void parse(const UnicodeString& text, 1174 Formattable& result, 1175 ParsePosition& parsePosition) const; 1176 1177 /** 1178 * Parses text from the given string as a currency amount. Unlike 1179 * the parse() method, this method will attempt to parse a generic 1180 * currency name, searching for a match of this object's locale's 1181 * currency display names, or for a 3-letter ISO currency code. 1182 * This method will fail if this format is not a currency format, 1183 * that is, if it does not contain the currency pattern symbol 1184 * (U+00A4) in its prefix or suffix. 1185 * 1186 * @param text the string to parse 1187 * @param pos input-output position; on input, the position within text 1188 * to match; must have 0 <= pos.getIndex() < text.length(); 1189 * on output, the position after the last matched character. 1190 * If the parse fails, the position in unchanged upon output. 1191 * @return if parse succeeds, a pointer to a newly-created CurrencyAmount 1192 * object (owned by the caller) containing information about 1193 * the parsed currency; if parse fails, this is NULL. 1194 * @stable ICU 49 1195 */ 1196 virtual CurrencyAmount* parseCurrency(const UnicodeString& text, 1197 ParsePosition& pos) const; 1198 1199 /** 1200 * Returns the decimal format symbols, which is generally not changed 1201 * by the programmer or user. 1202 * @return desired DecimalFormatSymbols 1203 * @see DecimalFormatSymbols 1204 * @stable ICU 2.0 1205 */ 1206 virtual const DecimalFormatSymbols* getDecimalFormatSymbols(void) const; 1207 1208 /** 1209 * Sets the decimal format symbols, which is generally not changed 1210 * by the programmer or user. 1211 * @param symbolsToAdopt DecimalFormatSymbols to be adopted. 1212 * @stable ICU 2.0 1213 */ 1214 virtual void adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt); 1215 1216 /** 1217 * Sets the decimal format symbols, which is generally not changed 1218 * by the programmer or user. 1219 * @param symbols DecimalFormatSymbols. 1220 * @stable ICU 2.0 1221 */ 1222 virtual void setDecimalFormatSymbols(const DecimalFormatSymbols& symbols); 1223 1224 1225 /** 1226 * Returns the currency plural format information, 1227 * which is generally not changed by the programmer or user. 1228 * @return desired CurrencyPluralInfo 1229 * @stable ICU 4.2 1230 */ 1231 virtual const CurrencyPluralInfo* getCurrencyPluralInfo(void) const; 1232 1233 /** 1234 * Sets the currency plural format information, 1235 * which is generally not changed by the programmer or user. 1236 * @param toAdopt CurrencyPluralInfo to be adopted. 1237 * @stable ICU 4.2 1238 */ 1239 virtual void adoptCurrencyPluralInfo(CurrencyPluralInfo* toAdopt); 1240 1241 /** 1242 * Sets the currency plural format information, 1243 * which is generally not changed by the programmer or user. 1244 * @param info Currency Plural Info. 1245 * @stable ICU 4.2 1246 */ 1247 virtual void setCurrencyPluralInfo(const CurrencyPluralInfo& info); 1248 1249 1250 /** 1251 * Get the positive prefix. 1252 * 1253 * @param result Output param which will receive the positive prefix. 1254 * @return A reference to 'result'. 1255 * Examples: +123, $123, sFr123 1256 * @stable ICU 2.0 1257 */ 1258 UnicodeString& getPositivePrefix(UnicodeString& result) const; 1259 1260 /** 1261 * Set the positive prefix. 1262 * 1263 * @param newValue the new value of the the positive prefix to be set. 1264 * Examples: +123, $123, sFr123 1265 * @stable ICU 2.0 1266 */ 1267 virtual void setPositivePrefix(const UnicodeString& newValue); 1268 1269 /** 1270 * Get the negative prefix. 1271 * 1272 * @param result Output param which will receive the negative prefix. 1273 * @return A reference to 'result'. 1274 * Examples: -123, ($123) (with negative suffix), sFr-123 1275 * @stable ICU 2.0 1276 */ 1277 UnicodeString& getNegativePrefix(UnicodeString& result) const; 1278 1279 /** 1280 * Set the negative prefix. 1281 * 1282 * @param newValue the new value of the the negative prefix to be set. 1283 * Examples: -123, ($123) (with negative suffix), sFr-123 1284 * @stable ICU 2.0 1285 */ 1286 virtual void setNegativePrefix(const UnicodeString& newValue); 1287 1288 /** 1289 * Get the positive suffix. 1290 * 1291 * @param result Output param which will receive the positive suffix. 1292 * @return A reference to 'result'. 1293 * Example: 123% 1294 * @stable ICU 2.0 1295 */ 1296 UnicodeString& getPositiveSuffix(UnicodeString& result) const; 1297 1298 /** 1299 * Set the positive suffix. 1300 * 1301 * @param newValue the new value of the positive suffix to be set. 1302 * Example: 123% 1303 * @stable ICU 2.0 1304 */ 1305 virtual void setPositiveSuffix(const UnicodeString& newValue); 1306 1307 /** 1308 * Get the negative suffix. 1309 * 1310 * @param result Output param which will receive the negative suffix. 1311 * @return A reference to 'result'. 1312 * Examples: -123%, ($123) (with positive suffixes) 1313 * @stable ICU 2.0 1314 */ 1315 UnicodeString& getNegativeSuffix(UnicodeString& result) const; 1316 1317 /** 1318 * Set the negative suffix. 1319 * 1320 * @param newValue the new value of the negative suffix to be set. 1321 * Examples: 123% 1322 * @stable ICU 2.0 1323 */ 1324 virtual void setNegativeSuffix(const UnicodeString& newValue); 1325 1326 /** 1327 * Get the multiplier for use in percent, permill, etc. 1328 * For a percentage, set the suffixes to have "%" and the multiplier to be 100. 1329 * (For Arabic, use arabic percent symbol). 1330 * For a permill, set the suffixes to have "\\u2031" and the multiplier to be 1000. 1331 * 1332 * @return the multiplier for use in percent, permill, etc. 1333 * Examples: with 100, 1.23 -> "123", and "123" -> 1.23 1334 * @stable ICU 2.0 1335 */ 1336 int32_t getMultiplier(void) const; 1337 1338 /** 1339 * Set the multiplier for use in percent, permill, etc. 1340 * For a percentage, set the suffixes to have "%" and the multiplier to be 100. 1341 * (For Arabic, use arabic percent symbol). 1342 * For a permill, set the suffixes to have "\\u2031" and the multiplier to be 1000. 1343 * 1344 * @param newValue the new value of the multiplier for use in percent, permill, etc. 1345 * Examples: with 100, 1.23 -> "123", and "123" -> 1.23 1346 * @stable ICU 2.0 1347 */ 1348 virtual void setMultiplier(int32_t newValue); 1349 1350 /** 1351 * Get the rounding increment. 1352 * @return A positive rounding increment, or 0.0 if a custom rounding 1353 * increment is not in effect. 1354 * @see #setRoundingIncrement 1355 * @see #getRoundingMode 1356 * @see #setRoundingMode 1357 * @stable ICU 2.0 1358 */ 1359 virtual double getRoundingIncrement(void) const; 1360 1361 /** 1362 * Set the rounding increment. In the absence of a rounding increment, 1363 * numbers will be rounded to the number of digits displayed. 1364 * @param newValue A positive rounding increment, or 0.0 to 1365 * use the default rounding increment. 1366 * Negative increments are equivalent to 0.0. 1367 * @see #getRoundingIncrement 1368 * @see #getRoundingMode 1369 * @see #setRoundingMode 1370 * @stable ICU 2.0 1371 */ 1372 virtual void setRoundingIncrement(double newValue); 1373 1374 /** 1375 * Get the rounding mode. 1376 * @return A rounding mode 1377 * @see #setRoundingIncrement 1378 * @see #getRoundingIncrement 1379 * @see #setRoundingMode 1380 * @stable ICU 2.0 1381 */ 1382 virtual ERoundingMode getRoundingMode(void) const; 1383 1384 /** 1385 * Set the rounding mode. 1386 * @param roundingMode A rounding mode 1387 * @see #setRoundingIncrement 1388 * @see #getRoundingIncrement 1389 * @see #getRoundingMode 1390 * @stable ICU 2.0 1391 */ 1392 virtual void setRoundingMode(ERoundingMode roundingMode); 1393 1394 /** 1395 * Get the width to which the output of format() is padded. 1396 * The width is counted in 16-bit code units. 1397 * @return the format width, or zero if no padding is in effect 1398 * @see #setFormatWidth 1399 * @see #getPadCharacterString 1400 * @see #setPadCharacter 1401 * @see #getPadPosition 1402 * @see #setPadPosition 1403 * @stable ICU 2.0 1404 */ 1405 virtual int32_t getFormatWidth(void) const; 1406 1407 /** 1408 * Set the width to which the output of format() is padded. 1409 * The width is counted in 16-bit code units. 1410 * This method also controls whether padding is enabled. 1411 * @param width the width to which to pad the result of 1412 * format(), or zero to disable padding. A negative 1413 * width is equivalent to 0. 1414 * @see #getFormatWidth 1415 * @see #getPadCharacterString 1416 * @see #setPadCharacter 1417 * @see #getPadPosition 1418 * @see #setPadPosition 1419 * @stable ICU 2.0 1420 */ 1421 virtual void setFormatWidth(int32_t width); 1422 1423 /** 1424 * Get the pad character used to pad to the format width. The 1425 * default is ' '. 1426 * @return a string containing the pad character. This will always 1427 * have a length of one 32-bit code point. 1428 * @see #setFormatWidth 1429 * @see #getFormatWidth 1430 * @see #setPadCharacter 1431 * @see #getPadPosition 1432 * @see #setPadPosition 1433 * @stable ICU 2.0 1434 */ 1435 virtual UnicodeString getPadCharacterString() const; 1436 1437 /** 1438 * Set the character used to pad to the format width. If padding 1439 * is not enabled, then this will take effect if padding is later 1440 * enabled. 1441 * @param padChar a string containing the pad charcter. If the string 1442 * has length 0, then the pad characer is set to ' '. Otherwise 1443 * padChar.char32At(0) will be used as the pad character. 1444 * @see #setFormatWidth 1445 * @see #getFormatWidth 1446 * @see #getPadCharacterString 1447 * @see #getPadPosition 1448 * @see #setPadPosition 1449 * @stable ICU 2.0 1450 */ 1451 virtual void setPadCharacter(const UnicodeString &padChar); 1452 1453 /** 1454 * Get the position at which padding will take place. This is the location 1455 * at which padding will be inserted if the result of format() 1456 * is shorter than the format width. 1457 * @return the pad position, one of kPadBeforePrefix, 1458 * kPadAfterPrefix, kPadBeforeSuffix, or 1459 * kPadAfterSuffix. 1460 * @see #setFormatWidth 1461 * @see #getFormatWidth 1462 * @see #setPadCharacter 1463 * @see #getPadCharacterString 1464 * @see #setPadPosition 1465 * @see #EPadPosition 1466 * @stable ICU 2.0 1467 */ 1468 virtual EPadPosition getPadPosition(void) const; 1469 1470 /** 1471 * Set the position at which padding will take place. This is the location 1472 * at which padding will be inserted if the result of format() 1473 * is shorter than the format width. This has no effect unless padding is 1474 * enabled. 1475 * @param padPos 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 #getPadPosition 1483 * @see #EPadPosition 1484 * @stable ICU 2.0 1485 */ 1486 virtual void setPadPosition(EPadPosition padPos); 1487 1488 /** 1489 * Return whether or not scientific notation is used. 1490 * @return TRUE if this object formats and parses scientific notation 1491 * @see #setScientificNotation 1492 * @see #getMinimumExponentDigits 1493 * @see #setMinimumExponentDigits 1494 * @see #isExponentSignAlwaysShown 1495 * @see #setExponentSignAlwaysShown 1496 * @stable ICU 2.0 1497 */ 1498 virtual UBool isScientificNotation(void) const; 1499 1500 /** 1501 * Set whether or not scientific notation is used. When scientific notation 1502 * is used, the effective maximum number of integer digits is <= 8. If the 1503 * maximum number of integer digits is set to more than 8, the effective 1504 * maximum will be 1. This allows this call to generate a 'default' scientific 1505 * number format without additional changes. 1506 * @param useScientific TRUE if this object formats and parses scientific 1507 * notation 1508 * @see #isScientificNotation 1509 * @see #getMinimumExponentDigits 1510 * @see #setMinimumExponentDigits 1511 * @see #isExponentSignAlwaysShown 1512 * @see #setExponentSignAlwaysShown 1513 * @stable ICU 2.0 1514 */ 1515 virtual void setScientificNotation(UBool useScientific); 1516 1517 /** 1518 * Return the minimum exponent digits that will be shown. 1519 * @return the minimum exponent digits that will be shown 1520 * @see #setScientificNotation 1521 * @see #isScientificNotation 1522 * @see #setMinimumExponentDigits 1523 * @see #isExponentSignAlwaysShown 1524 * @see #setExponentSignAlwaysShown 1525 * @stable ICU 2.0 1526 */ 1527 virtual int8_t getMinimumExponentDigits(void) const; 1528 1529 /** 1530 * Set the minimum exponent digits that will be shown. This has no 1531 * effect unless scientific notation is in use. 1532 * @param minExpDig a value >= 1 indicating the fewest exponent digits 1533 * that will be shown. Values less than 1 will be treated as 1. 1534 * @see #setScientificNotation 1535 * @see #isScientificNotation 1536 * @see #getMinimumExponentDigits 1537 * @see #isExponentSignAlwaysShown 1538 * @see #setExponentSignAlwaysShown 1539 * @stable ICU 2.0 1540 */ 1541 virtual void setMinimumExponentDigits(int8_t minExpDig); 1542 1543 /** 1544 * Return whether the exponent sign is always shown. 1545 * @return TRUE if the exponent is always prefixed with either the 1546 * localized minus sign or the localized plus sign, false if only negative 1547 * exponents are prefixed with the localized minus sign. 1548 * @see #setScientificNotation 1549 * @see #isScientificNotation 1550 * @see #setMinimumExponentDigits 1551 * @see #getMinimumExponentDigits 1552 * @see #setExponentSignAlwaysShown 1553 * @stable ICU 2.0 1554 */ 1555 virtual UBool isExponentSignAlwaysShown(void) const; 1556 1557 /** 1558 * Set whether the exponent sign is always shown. This has no effect 1559 * unless scientific notation is in use. 1560 * @param expSignAlways TRUE if the exponent is always prefixed with either 1561 * the localized minus sign or the localized plus sign, false if only 1562 * negative exponents are prefixed with the localized minus sign. 1563 * @see #setScientificNotation 1564 * @see #isScientificNotation 1565 * @see #setMinimumExponentDigits 1566 * @see #getMinimumExponentDigits 1567 * @see #isExponentSignAlwaysShown 1568 * @stable ICU 2.0 1569 */ 1570 virtual void setExponentSignAlwaysShown(UBool expSignAlways); 1571 1572 /** 1573 * Return the grouping size. Grouping size is the number of digits between 1574 * grouping separators in the integer portion of a number. For example, 1575 * in the number "123,456.78", the grouping size is 3. 1576 * 1577 * @return the grouping size. 1578 * @see setGroupingSize 1579 * @see NumberFormat::isGroupingUsed 1580 * @see DecimalFormatSymbols::getGroupingSeparator 1581 * @stable ICU 2.0 1582 */ 1583 int32_t getGroupingSize(void) const; 1584 1585 /** 1586 * Set the grouping size. Grouping size is the number of digits between 1587 * grouping separators in the integer portion of a number. For example, 1588 * in the number "123,456.78", the grouping size is 3. 1589 * 1590 * @param newValue the new value of the grouping size. 1591 * @see getGroupingSize 1592 * @see NumberFormat::setGroupingUsed 1593 * @see DecimalFormatSymbols::setGroupingSeparator 1594 * @stable ICU 2.0 1595 */ 1596 virtual void setGroupingSize(int32_t newValue); 1597 1598 /** 1599 * Return the secondary grouping size. In some locales one 1600 * grouping interval is used for the least significant integer 1601 * digits (the primary grouping size), and another is used for all 1602 * others (the secondary grouping size). A formatter supporting a 1603 * secondary grouping size will return a positive integer unequal 1604 * to the primary grouping size returned by 1605 * getGroupingSize(). For example, if the primary 1606 * grouping size is 4, and the secondary grouping size is 2, then 1607 * the number 123456789 formats as "1,23,45,6789", and the pattern 1608 * appears as "#,##,###0". 1609 * @return the secondary grouping size, or a value less than 1610 * one if there is none 1611 * @see setSecondaryGroupingSize 1612 * @see NumberFormat::isGroupingUsed 1613 * @see DecimalFormatSymbols::getGroupingSeparator 1614 * @stable ICU 2.4 1615 */ 1616 int32_t getSecondaryGroupingSize(void) const; 1617 1618 /** 1619 * Set the secondary grouping size. If set to a value less than 1, 1620 * then secondary grouping is turned off, and the primary grouping 1621 * size is used for all intervals, not just the least significant. 1622 * 1623 * @param newValue the new value of the secondary grouping size. 1624 * @see getSecondaryGroupingSize 1625 * @see NumberFormat#setGroupingUsed 1626 * @see DecimalFormatSymbols::setGroupingSeparator 1627 * @stable ICU 2.4 1628 */ 1629 virtual void setSecondaryGroupingSize(int32_t newValue); 1630 1631 #ifndef U_HIDE_INTERNAL_API 1632 1633 /** 1634 * Returns the minimum number of grouping digits. 1635 * Grouping separators are output if there are at least this many 1636 * digits to the left of the first (rightmost) grouping separator, 1637 * that is, there are at least (minimum grouping + grouping size) integer digits. 1638 * (Subject to isGroupingUsed().) 1639 * 1640 * For example, if this value is 2, and the grouping size is 3, then 1641 * 9999 -> "9999" and 10000 -> "10,000" 1642 * 1643 * This is a technology preview. This API may change behavior or may be removed. 1644 * 1645 * The default value for this attribute is 0. 1646 * A value of 1, 0, or lower, means that the use of grouping separators 1647 * only depends on the grouping size (and on isGroupingUsed()). 1648 * Currently, the corresponding CLDR data is not used; this is likely to change. 1649 * 1650 * @see setMinimumGroupingDigits 1651 * @see getGroupingSize 1652 * @internal technology preview 1653 */ 1654 int32_t getMinimumGroupingDigits() const; 1655 1656 #endif /* U_HIDE_INTERNAL_API */ 1657 1658 /* Cannot use #ifndef U_HIDE_INTERNAL_API for the following draft method since it is virtual. */ 1659 /** 1660 * Sets the minimum grouping digits. Setting to a value less than or 1661 * equal to 1 turns off minimum grouping digits. 1662 * 1663 * @param newValue the new value of minimum grouping digits. 1664 * @see getMinimumGroupingDigits 1665 * @internal technology preview 1666 */ 1667 virtual void setMinimumGroupingDigits(int32_t newValue); 1668 1669 1670 /** 1671 * Allows you to get the behavior of the decimal separator with integers. 1672 * (The decimal separator will always appear with decimals.) 1673 * 1674 * @return TRUE if the decimal separator always appear with decimals. 1675 * Example: Decimal ON: 12345 -> 12345.; OFF: 12345 -> 12345 1676 * @stable ICU 2.0 1677 */ 1678 UBool isDecimalSeparatorAlwaysShown(void) const; 1679 1680 /** 1681 * Allows you to set the behavior of the decimal separator with integers. 1682 * (The decimal separator will always appear with decimals.) 1683 * 1684 * @param newValue set TRUE if the decimal separator will always appear with decimals. 1685 * Example: Decimal ON: 12345 -> 12345.; OFF: 12345 -> 12345 1686 * @stable ICU 2.0 1687 */ 1688 virtual void setDecimalSeparatorAlwaysShown(UBool newValue); 1689 1690 /** 1691 * Allows you to get the parse behavior of the pattern decimal mark. 1692 * 1693 * @return TRUE if input must contain a match to decimal mark in pattern 1694 * @stable ICU 54 1695 */ 1696 UBool isDecimalPatternMatchRequired(void) const; 1697 1698 /** 1699 * Allows you to set the behavior of the pattern decimal mark. 1700 * 1701 * if TRUE, the input must have a decimal mark if one was specified in the pattern. When 1702 * FALSE the decimal mark may be omitted from the input. 1703 * 1704 * @param newValue set TRUE if input must contain a match to decimal mark in pattern 1705 * @stable ICU 54 1706 */ 1707 virtual void setDecimalPatternMatchRequired(UBool newValue); 1708 1709 1710 /** 1711 * Synthesizes a pattern string that represents the current state 1712 * of this Format object. 1713 * 1714 * @param result Output param which will receive the pattern. 1715 * Previous contents are deleted. 1716 * @return A reference to 'result'. 1717 * @see applyPattern 1718 * @stable ICU 2.0 1719 */ 1720 virtual UnicodeString& toPattern(UnicodeString& result) const; 1721 1722 /** 1723 * Synthesizes a localized pattern string that represents the current 1724 * state of this Format object. 1725 * 1726 * @param result Output param which will receive the localized pattern. 1727 * Previous contents are deleted. 1728 * @return A reference to 'result'. 1729 * @see applyPattern 1730 * @stable ICU 2.0 1731 */ 1732 virtual UnicodeString& toLocalizedPattern(UnicodeString& result) const; 1733 1734 /** 1735 * Apply the given pattern to this Format object. A pattern is a 1736 * short-hand specification for the various formatting properties. 1737 * These properties can also be changed individually through the 1738 * various setter methods. 1739 * <P> 1740 * There is no limit to integer digits are set 1741 * by this routine, since that is the typical end-user desire; 1742 * use setMaximumInteger if you want to set a real value. 1743 * For negative numbers, use a second pattern, separated by a semicolon 1744 * <pre> 1745 * . Example "#,#00.0#" -> 1,234.56 1746 * </pre> 1747 * This means a minimum of 2 integer digits, 1 fraction digit, and 1748 * a maximum of 2 fraction digits. 1749 * <pre> 1750 * . Example: "#,#00.0#;(#,#00.0#)" for negatives in parantheses. 1751 * </pre> 1752 * In negative patterns, the minimum and maximum counts are ignored; 1753 * these are presumed to be set in the positive pattern. 1754 * 1755 * @param pattern The pattern to be applied. 1756 * @param parseError Struct to recieve information on position 1757 * of error if an error is encountered 1758 * @param status Output param set to success/failure code on 1759 * exit. If the pattern is invalid, this will be 1760 * set to a failure result. 1761 * @stable ICU 2.0 1762 */ 1763 virtual void applyPattern(const UnicodeString& pattern, 1764 UParseError& parseError, 1765 UErrorCode& status); 1766 /** 1767 * Sets the pattern. 1768 * @param pattern The pattern to be applied. 1769 * @param status Output param set to success/failure code on 1770 * exit. If the pattern is invalid, this will be 1771 * set to a failure result. 1772 * @stable ICU 2.0 1773 */ 1774 virtual void applyPattern(const UnicodeString& pattern, 1775 UErrorCode& status); 1776 1777 /** 1778 * Apply the given pattern to this Format object. The pattern 1779 * is assumed to be in a localized notation. A pattern is a 1780 * short-hand specification for the various formatting properties. 1781 * These properties can also be changed individually through the 1782 * various setter methods. 1783 * <P> 1784 * There is no limit to integer digits are set 1785 * by this routine, since that is the typical end-user desire; 1786 * use setMaximumInteger if you want to set a real value. 1787 * For negative numbers, use a second pattern, separated by a semicolon 1788 * <pre> 1789 * . Example "#,#00.0#" -> 1,234.56 1790 * </pre> 1791 * This means a minimum of 2 integer digits, 1 fraction digit, and 1792 * a maximum of 2 fraction digits. 1793 * 1794 * Example: "#,#00.0#;(#,#00.0#)" for negatives in parantheses. 1795 * 1796 * In negative patterns, the minimum and maximum counts are ignored; 1797 * these are presumed to be set in the positive pattern. 1798 * 1799 * @param pattern The localized pattern to be applied. 1800 * @param parseError Struct to recieve information on position 1801 * of error if an error is encountered 1802 * @param status Output param set to success/failure code on 1803 * exit. If the pattern is invalid, this will be 1804 * set to a failure result. 1805 * @stable ICU 2.0 1806 */ 1807 virtual void applyLocalizedPattern(const UnicodeString& pattern, 1808 UParseError& parseError, 1809 UErrorCode& status); 1810 1811 /** 1812 * Apply the given pattern to this Format object. 1813 * 1814 * @param pattern The localized pattern to be applied. 1815 * @param status Output param set to success/failure code on 1816 * exit. If the pattern is invalid, this will be 1817 * set to a failure result. 1818 * @stable ICU 2.0 1819 */ 1820 virtual void applyLocalizedPattern(const UnicodeString& pattern, 1821 UErrorCode& status); 1822 1823 1824 /** 1825 * Sets the maximum number of digits allowed in the integer portion of a 1826 * number. This override limits the integer digit count to 309. 1827 * 1828 * @param newValue the new value of the maximum number of digits 1829 * allowed in the integer portion of a number. 1830 * @see NumberFormat#setMaximumIntegerDigits 1831 * @stable ICU 2.0 1832 */ 1833 virtual void setMaximumIntegerDigits(int32_t newValue); 1834 1835 /** 1836 * Sets the minimum number of digits allowed in the integer portion of a 1837 * number. This override limits the integer digit count to 309. 1838 * 1839 * @param newValue the new value of the minimum number of digits 1840 * allowed in the integer portion of a number. 1841 * @see NumberFormat#setMinimumIntegerDigits 1842 * @stable ICU 2.0 1843 */ 1844 virtual void setMinimumIntegerDigits(int32_t newValue); 1845 1846 /** 1847 * Sets the maximum number of digits allowed in the fraction portion of a 1848 * number. This override limits the fraction digit count to 340. 1849 * 1850 * @param newValue the new value of the maximum number of digits 1851 * allowed in the fraction portion of a number. 1852 * @see NumberFormat#setMaximumFractionDigits 1853 * @stable ICU 2.0 1854 */ 1855 virtual void setMaximumFractionDigits(int32_t newValue); 1856 1857 /** 1858 * Sets the minimum number of digits allowed in the fraction portion of a 1859 * number. This override limits the fraction digit count to 340. 1860 * 1861 * @param newValue the new value of the minimum number of digits 1862 * allowed in the fraction portion of a number. 1863 * @see NumberFormat#setMinimumFractionDigits 1864 * @stable ICU 2.0 1865 */ 1866 virtual void setMinimumFractionDigits(int32_t newValue); 1867 1868 /** 1869 * Returns the minimum number of significant digits that will be 1870 * displayed. This value has no effect unless areSignificantDigitsUsed() 1871 * returns true. 1872 * @return the fewest significant digits that will be shown 1873 * @stable ICU 3.0 1874 */ 1875 int32_t getMinimumSignificantDigits() const; 1876 1877 /** 1878 * Returns the maximum number of significant digits that will be 1879 * displayed. This value has no effect unless areSignificantDigitsUsed() 1880 * returns true. 1881 * @return the most significant digits that will be shown 1882 * @stable ICU 3.0 1883 */ 1884 int32_t getMaximumSignificantDigits() const; 1885 1886 /** 1887 * Sets the minimum number of significant digits that will be 1888 * displayed. If <code>min</code> is less than one then it is set 1889 * to one. If the maximum significant digits count is less than 1890 * <code>min</code>, then it is set to <code>min</code>. 1891 * This function also enables the use of significant digits 1892 * by this formatter - areSignificantDigitsUsed() will return TRUE. 1893 * @see #areSignificantDigitsUsed 1894 * @param min the fewest significant digits to be shown 1895 * @stable ICU 3.0 1896 */ 1897 void setMinimumSignificantDigits(int32_t min); 1898 1899 /** 1900 * Sets the maximum number of significant digits that will be 1901 * displayed. If <code>max</code> is less than one then it is set 1902 * to one. If the minimum significant digits count is greater 1903 * than <code>max</code>, then it is set to <code>max</code>. 1904 * This function also enables the use of significant digits 1905 * by this formatter - areSignificantDigitsUsed() will return TRUE. 1906 * @see #areSignificantDigitsUsed 1907 * @param max the most significant digits to be shown 1908 * @stable ICU 3.0 1909 */ 1910 void setMaximumSignificantDigits(int32_t max); 1911 1912 /** 1913 * Returns true if significant digits are in use, or false if 1914 * integer and fraction digit counts are in use. 1915 * @return true if significant digits are in use 1916 * @stable ICU 3.0 1917 */ 1918 UBool areSignificantDigitsUsed() const; 1919 1920 /** 1921 * Sets whether significant digits are in use, or integer and 1922 * fraction digit counts are in use. 1923 * @param useSignificantDigits true to use significant digits, or 1924 * false to use integer and fraction digit counts 1925 * @stable ICU 3.0 1926 */ 1927 void setSignificantDigitsUsed(UBool useSignificantDigits); 1928 1929 public: 1930 /** 1931 * Sets the currency used to display currency 1932 * amounts. This takes effect immediately, if this format is a 1933 * currency format. If this format is not a currency format, then 1934 * the currency is used if and when this object becomes a 1935 * currency format through the application of a new pattern. 1936 * @param theCurrency a 3-letter ISO code indicating new currency 1937 * to use. It need not be null-terminated. May be the empty 1938 * string or NULL to indicate no currency. 1939 * @param ec input-output error code 1940 * @stable ICU 3.0 1941 */ 1942 virtual void setCurrency(const char16_t* theCurrency, UErrorCode& ec); 1943 1944 /** 1945 * Sets the currency used to display currency amounts. See 1946 * setCurrency(const char16_t*, UErrorCode&). 1947 * @deprecated ICU 3.0. Use setCurrency(const char16_t*, UErrorCode&). 1948 */ 1949 virtual void setCurrency(const char16_t* theCurrency); 1950 1951 /** 1952 * Sets the <tt>Currency Context</tt> object used to display currency. 1953 * This takes effect immediately, if this format is a 1954 * currency format. 1955 * @param currencyContext new currency context object to use. 1956 * @stable ICU 54 1957 */ 1958 void setCurrencyUsage(UCurrencyUsage newUsage, UErrorCode* ec); 1959 1960 /** 1961 * Returns the <tt>Currency Context</tt> object used to display currency 1962 * @stable ICU 54 1963 */ 1964 UCurrencyUsage getCurrencyUsage() const; 1965 1966 1967 /** 1968 * The resource tags we use to retrieve decimal format data from 1969 * locale resource bundles. 1970 * @deprecated ICU 3.4. This string has no public purpose. Please don't use it. 1971 */ 1972 static const char fgNumberPatterns[]; 1973 1974 #ifndef U_HIDE_INTERNAL_API 1975 /** 1976 * Get a FixedDecimal corresponding to a double as it would be 1977 * formatted by this DecimalFormat. 1978 * Internal, not intended for public use. 1979 * @internal 1980 */ 1981 FixedDecimal getFixedDecimal(double number, UErrorCode &status) const; 1982 1983 /** 1984 * Get a FixedDecimal corresponding to a formattable as it would be 1985 * formatted by this DecimalFormat. 1986 * Internal, not intended for public use. 1987 * @internal 1988 */ 1989 FixedDecimal getFixedDecimal(const Formattable &number, UErrorCode &status) const; 1990 1991 /** 1992 * Get a FixedDecimal corresponding to a DigitList as it would be 1993 * formatted by this DecimalFormat. Note: the DigitList may be modified. 1994 * Internal, not intended for public use. 1995 * @internal 1996 */ 1997 FixedDecimal getFixedDecimal(DigitList &number, UErrorCode &status) const; 1998 1999 /** 2000 * Get a VisibleDigitsWithExponent corresponding to a double 2001 * as it would be formatted by this DecimalFormat. 2002 * Internal, not intended for public use. 2003 * @internal 2004 */ 2005 VisibleDigitsWithExponent &initVisibleDigitsWithExponent( 2006 double number, 2007 VisibleDigitsWithExponent &digits, 2008 UErrorCode &status) const; 2009 2010 /** 2011 * Get a VisibleDigitsWithExponent corresponding to a formattable 2012 * as it would be formatted by this DecimalFormat. 2013 * Internal, not intended for public use. 2014 * @internal 2015 */ 2016 VisibleDigitsWithExponent &initVisibleDigitsWithExponent( 2017 const Formattable &number, 2018 VisibleDigitsWithExponent &digits, 2019 UErrorCode &status) const; 2020 2021 /** 2022 * Get a VisibleDigitsWithExponent corresponding to a DigitList 2023 * as it would be formatted by this DecimalFormat. 2024 * Note: the DigitList may be modified. 2025 * Internal, not intended for public use. 2026 * @internal 2027 */ 2028 VisibleDigitsWithExponent &initVisibleDigitsWithExponent( 2029 DigitList &number, 2030 VisibleDigitsWithExponent &digits, 2031 UErrorCode &status) const; 2032 2033 #endif /* U_HIDE_INTERNAL_API */ 2034 2035 public: 2036 2037 /** 2038 * Return the class ID for this class. This is useful only for 2039 * comparing to a return value from getDynamicClassID(). For example: 2040 * <pre> 2041 * . Base* polymorphic_pointer = createPolymorphicObject(); 2042 * . if (polymorphic_pointer->getDynamicClassID() == 2043 * . Derived::getStaticClassID()) ... 2044 * </pre> 2045 * @return The class ID for all objects of this class. 2046 * @stable ICU 2.0 2047 */ 2048 static UClassID U_EXPORT2 getStaticClassID(void); 2049 2050 /** 2051 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. 2052 * This method is to implement a simple version of RTTI, since not all 2053 * C++ compilers support genuine RTTI. Polymorphic operator==() and 2054 * clone() methods call this method. 2055 * 2056 * @return The class ID for this object. All objects of a 2057 * given class have the same class ID. Objects of 2058 * other classes have different class IDs. 2059 * @stable ICU 2.0 2060 */ 2061 virtual UClassID getDynamicClassID(void) const; 2062 2063 private: 2064 2065 DecimalFormat(); // default constructor not implemented 2066 2067 /** 2068 * Initialize all fields of a new DecimalFormatter to a safe default value. 2069 * Common code for use by constructors. 2070 */ 2071 void init(); 2072 2073 /** 2074 * Do real work of constructing a new DecimalFormat. 2075 */ 2076 void construct(UErrorCode& status, 2077 UParseError& parseErr, 2078 const UnicodeString* pattern = 0, 2079 DecimalFormatSymbols* symbolsToAdopt = 0 2080 ); 2081 2082 void handleCurrencySignInPattern(UErrorCode& status); 2083 2084 void parse(const UnicodeString& text, 2085 Formattable& result, 2086 ParsePosition& pos, 2087 char16_t* currency) const; 2088 2089 enum { 2090 fgStatusInfinite, 2091 fgStatusLength // Leave last in list. 2092 } StatusFlags; 2093 2094 UBool subparse(const UnicodeString& text, 2095 const UnicodeString* negPrefix, 2096 const UnicodeString* negSuffix, 2097 const UnicodeString* posPrefix, 2098 const UnicodeString* posSuffix, 2099 UBool complexCurrencyParsing, 2100 int8_t type, 2101 ParsePosition& parsePosition, 2102 DigitList& digits, UBool* status, 2103 char16_t* currency) const; 2104 2105 // Mixed style parsing for currency. 2106 // It parses against the current currency pattern 2107 // using complex affix comparison 2108 // parses against the currency plural patterns using complex affix comparison, 2109 // and parses against the current pattern using simple affix comparison. 2110 UBool parseForCurrency(const UnicodeString& text, 2111 ParsePosition& parsePosition, 2112 DigitList& digits, 2113 UBool* status, 2114 char16_t* currency) const; 2115 2116 int32_t skipPadding(const UnicodeString& text, int32_t position) const; 2117 2118 int32_t compareAffix(const UnicodeString& input, 2119 int32_t pos, 2120 UBool isNegative, 2121 UBool isPrefix, 2122 const UnicodeString* affixPat, 2123 UBool complexCurrencyParsing, 2124 int8_t type, 2125 char16_t* currency) const; 2126 2127 static UnicodeString& trimMarksFromAffix(const UnicodeString& affix, UnicodeString& trimmedAffix); 2128 2129 UBool equalWithSignCompatibility(UChar32 lhs, UChar32 rhs) const; 2130 2131 int32_t compareSimpleAffix(const UnicodeString& affix, 2132 const UnicodeString& input, 2133 int32_t pos, 2134 UBool lenient) const; 2135 2136 static int32_t skipPatternWhiteSpace(const UnicodeString& text, int32_t pos); 2137 2138 static int32_t skipUWhiteSpace(const UnicodeString& text, int32_t pos); 2139 2140 static int32_t skipUWhiteSpaceAndMarks(const UnicodeString& text, int32_t pos); 2141 2142 static int32_t skipBidiMarks(const UnicodeString& text, int32_t pos); 2143 2144 int32_t compareComplexAffix(const UnicodeString& affixPat, 2145 const UnicodeString& input, 2146 int32_t pos, 2147 int8_t type, 2148 char16_t* currency) const; 2149 2150 static int32_t match(const UnicodeString& text, int32_t pos, UChar32 ch); 2151 2152 static int32_t match(const UnicodeString& text, int32_t pos, const UnicodeString& str); 2153 2154 static UBool matchSymbol(const UnicodeString &text, int32_t position, int32_t length, const UnicodeString &symbol, 2155 UnicodeSet *sset, UChar32 schar); 2156 2157 static UBool matchDecimal(UChar32 symbolChar, 2158 UBool sawDecimal, UChar32 sawDecimalChar, 2159 const UnicodeSet *sset, UChar32 schar); 2160 2161 static UBool matchGrouping(UChar32 groupingChar, 2162 UBool sawGrouping, UChar32 sawGroupingChar, 2163 const UnicodeSet *sset, 2164 UChar32 decimalChar, const UnicodeSet *decimalSet, 2165 UChar32 schar); 2166 2167 // set up currency affix patterns for mix parsing. 2168 // The patterns saved here are the affix patterns of default currency 2169 // pattern and the unique affix patterns of the plural currency patterns. 2170 // Those patterns are used by parseForCurrency(). 2171 void setupCurrencyAffixPatterns(UErrorCode& status); 2172 2173 // get the currency rounding with respect to currency usage 2174 double getCurrencyRounding(const char16_t* currency, 2175 UErrorCode* ec) const; 2176 2177 // get the currency fraction with respect to currency usage 2178 int getCurrencyFractionDigits(const char16_t* currency, 2179 UErrorCode* ec) const; 2180 2181 // hashtable operations 2182 Hashtable* initHashForAffixPattern(UErrorCode& status); 2183 2184 void deleteHashForAffixPattern(); 2185 2186 void copyHashForAffixPattern(const Hashtable* source, 2187 Hashtable* target, UErrorCode& status); 2188 2189 DecimalFormatImpl *fImpl; 2190 2191 /** 2192 * Constants. 2193 */ 2194 2195 2196 EnumSet<UNumberFormatAttribute, 2197 UNUM_MAX_NONBOOLEAN_ATTRIBUTE+1, 2198 UNUM_LIMIT_BOOLEAN_ATTRIBUTE> 2199 fBoolFlags; 2200 2201 2202 // style is only valid when decimal formatter is constructed by 2203 // DecimalFormat(pattern, decimalFormatSymbol, style) 2204 int fStyle; 2205 2206 2207 // Affix pattern set for currency. 2208 // It is a set of AffixPatternsForCurrency, 2209 // each element of the set saves the negative prefix pattern, 2210 // negative suffix pattern, positive prefix pattern, 2211 // and positive suffix pattern of a pattern. 2212 // It is used for currency mixed style parsing. 2213 // It is actually is a set. 2214 // The set contains the default currency pattern from the locale, 2215 // and the currency plural patterns. 2216 // Since it is a set, it does not contain duplicated items. 2217 // For example, if 2 currency plural patterns are the same, only one pattern 2218 // is included in the set. When parsing, we do not check whether the plural 2219 // count match or not. 2220 Hashtable* fAffixPatternsForCurrency; 2221 2222 // Information needed for DecimalFormat to format/parse currency plural. 2223 CurrencyPluralInfo* fCurrencyPluralInfo; 2224 2225 #if UCONFIG_HAVE_PARSEALLINPUT 2226 UNumberFormatAttributeValue fParseAllInput; 2227 #endif 2228 2229 // Decimal Format Static Sets singleton. 2230 const DecimalFormatStaticSets *fStaticSets; 2231 2232 protected: 2233 2234 #ifndef U_HIDE_INTERNAL_API 2235 /** 2236 * Rounds a value according to the rules of this object. 2237 * @internal 2238 */ 2239 DigitList& _round(const DigitList& number, DigitList& adjustedNum, UBool& isNegative, UErrorCode& status) const; 2240 #endif /* U_HIDE_INTERNAL_API */ 2241 2242 /** 2243 * Returns the currency in effect for this formatter. Subclasses 2244 * should override this method as needed. Unlike getCurrency(), 2245 * this method should never return "". 2246 * @result output parameter for null-terminated result, which must 2247 * have a capacity of at least 4 2248 * @internal 2249 */ 2250 virtual void getEffectiveCurrency(char16_t* result, UErrorCode& ec) const; 2251 2252 /** number of integer digits 2253 * @stable ICU 2.4 2254 */ 2255 static const int32_t kDoubleIntegerDigits; 2256 /** number of fraction digits 2257 * @stable ICU 2.4 2258 */ 2259 static const int32_t kDoubleFractionDigits; 2260 2261 /** 2262 * When someone turns on scientific mode, we assume that more than this 2263 * number of digits is due to flipping from some other mode that didn't 2264 * restrict the maximum, and so we force 1 integer digit. We don't bother 2265 * to track and see if someone is using exponential notation with more than 2266 * this number, it wouldn't make sense anyway, and this is just to make sure 2267 * that someone turning on scientific mode with default settings doesn't 2268 * end up with lots of zeroes. 2269 * @stable ICU 2.8 2270 */ 2271 static const int32_t kMaxScientificIntegerDigits; 2272 2273 }; 2274 2275 U_NAMESPACE_END 2276 2277 #endif /* #if !UCONFIG_NO_FORMATTING */ 2278 2279 #endif // _DECIMFMT 2280 //eof 2281