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