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 half-even rounding it 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 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 * <ul> 614 * <li>Rounding only affects the string produced by formatting. It does 615 * not affect parsing or change any numerical values. 616 * 617 * <li>A <em>rounding mode</em> determines how values are rounded; see 618 * DecimalFormat::ERoundingMode. Rounding increments specified in 619 * patterns use the default mode, DecimalFormat::kRoundHalfEven. 620 * 621 * <li>Some locales use rounding in their currency formats to reflect the 622 * smallest currency denomination. 623 * 624 * <li>In a pattern, digits '1' through '9' specify rounding, but otherwise 625 * behave identically to digit '0'. 626 * </ul> 627 * 628 * <p><strong>Synchronization</strong> 629 * 630 * <p>DecimalFormat objects are not synchronized. Multiple 631 * threads should not access one formatter concurrently. 632 * 633 * <p><strong>Subclassing</strong> 634 * 635 * <p><em>User subclasses are not supported.</em> While clients may write 636 * subclasses, such code will not necessarily work and will not be 637 * guaranteed to work stably from release to release. 638 */ 639 class U_I18N_API DecimalFormat: public NumberFormat { 640 public: 641 /** 642 * Rounding mode. 643 * @stable ICU 2.4 644 */ 645 enum ERoundingMode { 646 kRoundCeiling, /**< Round towards positive infinity */ 647 kRoundFloor, /**< Round towards negative infinity */ 648 kRoundDown, /**< Round towards zero */ 649 kRoundUp, /**< Round away from zero */ 650 kRoundHalfEven, /**< Round towards the nearest integer, or 651 towards the nearest even integer if equidistant */ 652 kRoundHalfDown, /**< Round towards the nearest integer, or 653 towards zero if equidistant */ 654 kRoundHalfUp /**< Round towards the nearest integer, or 655 away from zero if equidistant */ 656 // We don't support ROUND_UNNECESSARY 657 }; 658 659 /** 660 * Pad position. 661 * @stable ICU 2.4 662 */ 663 enum EPadPosition { 664 kPadBeforePrefix, 665 kPadAfterPrefix, 666 kPadBeforeSuffix, 667 kPadAfterSuffix 668 }; 669 670 /** 671 * Create a DecimalFormat using the default pattern and symbols 672 * for the default locale. This is a convenient way to obtain a 673 * DecimalFormat when internationalization is not the main concern. 674 * <P> 675 * To obtain standard formats for a given locale, use the factory methods 676 * on NumberFormat such as createInstance. These factories will 677 * return the most appropriate sub-class of NumberFormat for a given 678 * locale. 679 * @param status Output param set to success/failure code. If the 680 * pattern is invalid this will be set to a failure code. 681 * @stable ICU 2.0 682 */ 683 DecimalFormat(UErrorCode& status); 684 685 /** 686 * Create a DecimalFormat from the given pattern and the symbols 687 * for the default locale. This is a convenient way to obtain a 688 * DecimalFormat when internationalization is not the main concern. 689 * <P> 690 * To obtain standard formats for a given locale, use the factory methods 691 * on NumberFormat such as createInstance. These factories will 692 * return the most appropriate sub-class of NumberFormat for a given 693 * locale. 694 * @param pattern A non-localized pattern string. 695 * @param status Output param set to success/failure code. If the 696 * pattern is invalid this will be set to a failure code. 697 * @stable ICU 2.0 698 */ 699 DecimalFormat(const UnicodeString& pattern, 700 UErrorCode& status); 701 702 /** 703 * Create a DecimalFormat from the given pattern and symbols. 704 * Use this constructor when you need to completely customize the 705 * behavior of the format. 706 * <P> 707 * To obtain standard formats for a given 708 * locale, use the factory methods on NumberFormat such as 709 * createInstance or createCurrencyInstance. If you need only minor adjustments 710 * to a standard format, you can modify the format returned by 711 * a NumberFormat factory method. 712 * 713 * @param pattern a non-localized pattern string 714 * @param symbolsToAdopt the set of symbols to be used. The caller should not 715 * delete this object after making this call. 716 * @param status Output param set to success/failure code. If the 717 * pattern is invalid this will be set to a failure code. 718 * @stable ICU 2.0 719 */ 720 DecimalFormat( const UnicodeString& pattern, 721 DecimalFormatSymbols* symbolsToAdopt, 722 UErrorCode& status); 723 724 /** 725 * This API is for ICU use only. 726 * Create a DecimalFormat from the given pattern, symbols, and style. 727 * 728 * @param pattern a non-localized pattern string 729 * @param symbolsToAdopt the set of symbols to be used. The caller should not 730 * delete this object after making this call. 731 * @param style style of decimal format, kNumberStyle etc. 732 * @param status Output param set to success/failure code. If the 733 * pattern is invalid this will be set to a failure code. 734 * @internal ICU 4.2 735 */ 736 DecimalFormat( const UnicodeString& pattern, 737 DecimalFormatSymbols* symbolsToAdopt, 738 NumberFormat::EStyles style, 739 UErrorCode& status); 740 741 /** 742 * Create a DecimalFormat from the given pattern and symbols. 743 * Use this constructor when you need to completely customize the 744 * behavior of the format. 745 * <P> 746 * To obtain standard formats for a given 747 * locale, use the factory methods on NumberFormat such as 748 * createInstance or createCurrencyInstance. If you need only minor adjustments 749 * to a standard format, you can modify the format returned by 750 * a NumberFormat factory method. 751 * 752 * @param pattern a non-localized pattern string 753 * @param symbolsToAdopt the set of symbols to be used. The caller should not 754 * delete this object after making this call. 755 * @param parseError Output param to receive errors occured during parsing 756 * @param status Output param set to success/failure code. If the 757 * pattern is invalid this will be set to a failure code. 758 * @stable ICU 2.0 759 */ 760 DecimalFormat( const UnicodeString& pattern, 761 DecimalFormatSymbols* symbolsToAdopt, 762 UParseError& parseError, 763 UErrorCode& status); 764 /** 765 * Create a DecimalFormat from the given pattern and symbols. 766 * Use this constructor when you need to completely customize the 767 * behavior of the format. 768 * <P> 769 * To obtain standard formats for a given 770 * locale, use the factory methods on NumberFormat such as 771 * createInstance or createCurrencyInstance. If you need only minor adjustments 772 * to a standard format, you can modify the format returned by 773 * a NumberFormat factory method. 774 * 775 * @param pattern a non-localized pattern string 776 * @param symbols the set of symbols to be used 777 * @param status Output param set to success/failure code. If the 778 * pattern is invalid this will be set to a failure code. 779 * @stable ICU 2.0 780 */ 781 DecimalFormat( const UnicodeString& pattern, 782 const DecimalFormatSymbols& symbols, 783 UErrorCode& status); 784 785 /** 786 * Copy constructor. 787 * 788 * @param source the DecimalFormat object to be copied from. 789 * @stable ICU 2.0 790 */ 791 DecimalFormat(const DecimalFormat& source); 792 793 /** 794 * Assignment operator. 795 * 796 * @param rhs the DecimalFormat object to be copied. 797 * @stable ICU 2.0 798 */ 799 DecimalFormat& operator=(const DecimalFormat& rhs); 800 801 /** 802 * Destructor. 803 * @stable ICU 2.0 804 */ 805 virtual ~DecimalFormat(); 806 807 /** 808 * Clone this Format object polymorphically. The caller owns the 809 * result and should delete it when done. 810 * 811 * @return a polymorphic copy of this DecimalFormat. 812 * @stable ICU 2.0 813 */ 814 virtual Format* clone(void) const; 815 816 /** 817 * Return true if the given Format objects are semantically equal. 818 * Objects of different subclasses are considered unequal. 819 * 820 * @param other the object to be compared with. 821 * @return true if the given Format objects are semantically equal. 822 * @stable ICU 2.0 823 */ 824 virtual UBool operator==(const Format& other) const; 825 826 827 using NumberFormat::format; 828 829 /** 830 * Format a double or long number using base-10 representation. 831 * 832 * @param number The value to be formatted. 833 * @param appendTo Output parameter to receive result. 834 * Result is appended to existing contents. 835 * @param pos On input: an alignment field, if desired. 836 * On output: the offsets of the alignment field. 837 * @return Reference to 'appendTo' parameter. 838 * @stable ICU 2.0 839 */ 840 virtual UnicodeString& format(double number, 841 UnicodeString& appendTo, 842 FieldPosition& pos) const; 843 844 /** 845 * Format a double or long number using base-10 representation. 846 * 847 * @param number The value to be formatted. 848 * @param appendTo Output parameter to receive result. 849 * Result is appended to existing contents. 850 * @param posIter On return, can be used to iterate over positions 851 * of fields generated by this format call. 852 * Can be NULL. 853 * @param status Output param filled with success/failure status. 854 * @return Reference to 'appendTo' parameter. 855 * @draft 4.4 856 */ 857 virtual UnicodeString& format(double number, 858 UnicodeString& appendTo, 859 FieldPositionIterator* posIter, 860 UErrorCode& status) const; 861 862 /** 863 * Format a long number using base-10 representation. 864 * 865 * @param number The value to be formatted. 866 * @param appendTo Output parameter to receive result. 867 * Result is appended to existing contents. 868 * @param pos On input: an alignment field, if desired. 869 * On output: the offsets of the alignment field. 870 * @return Reference to 'appendTo' parameter. 871 * @stable ICU 2.0 872 */ 873 virtual UnicodeString& format(int32_t number, 874 UnicodeString& appendTo, 875 FieldPosition& pos) const; 876 877 /** 878 * Format a long number using base-10 representation. 879 * 880 * @param number The value to be formatted. 881 * @param appendTo Output parameter to receive result. 882 * Result is appended to existing contents. 883 * @param posIter On return, can be used to iterate over positions 884 * of fields generated by this format call. 885 * Can be NULL. 886 * @param status Output param filled with success/failure status. 887 * @return Reference to 'appendTo' parameter. 888 * @draft 4.4 889 */ 890 virtual UnicodeString& format(int32_t number, 891 UnicodeString& appendTo, 892 FieldPositionIterator* posIter, 893 UErrorCode& status) const; 894 895 /** 896 * Format an int64 number using base-10 representation. 897 * 898 * @param number The value to be formatted. 899 * @param appendTo Output parameter to receive result. 900 * Result is appended to existing contents. 901 * @param pos On input: an alignment field, if desired. 902 * On output: the offsets of the alignment field. 903 * @return Reference to 'appendTo' parameter. 904 * @stable ICU 2.8 905 */ 906 virtual UnicodeString& format(int64_t number, 907 UnicodeString& appendTo, 908 FieldPosition& pos) const; 909 910 /** 911 * Format an int64 number using base-10 representation. 912 * 913 * @param number The value to be formatted. 914 * @param appendTo Output parameter to receive result. 915 * Result is appended to existing contents. 916 * @param posIter On return, can be used to iterate over positions 917 * of fields generated by this format call. 918 * Can be NULL. 919 * @param status Output param filled with success/failure status. 920 * @return Reference to 'appendTo' parameter. 921 * @draft 4.4 922 */ 923 virtual UnicodeString& format(int64_t number, 924 UnicodeString& appendTo, 925 FieldPositionIterator* posIter, 926 UErrorCode& status) const; 927 928 /** 929 * Format a decimal number. 930 * The syntax of the unformatted number is a "numeric string" 931 * as defined in the Decimal Arithmetic Specification, available at 932 * http://speleotrove.com/decimal 933 * 934 * @param number The unformatted number, as a string. 935 * @param appendTo Output parameter to receive result. 936 * Result is appended to existing contents. 937 * @param posIter On return, can be used to iterate over positions 938 * of fields generated by this format call. 939 * Can be NULL. 940 * @param status Output param filled with success/failure status. 941 * @return Reference to 'appendTo' parameter. 942 * @draft 4.4 943 */ 944 virtual UnicodeString& format(const StringPiece &number, 945 UnicodeString& appendTo, 946 FieldPositionIterator* posIter, 947 UErrorCode& status) const; 948 949 950 /** 951 * Format a decimal number. 952 * The number is a DigitList wrapper onto a floating point decimal number. 953 * The default implementation in NumberFormat converts the decimal number 954 * to a double and formats that. 955 * 956 * @param number The number, a DigitList format Decimal Floating Point. 957 * @param appendTo Output parameter to receive result. 958 * Result is appended to existing contents. 959 * @param posIter On return, can be used to iterate over positions 960 * of fields generated by this format call. 961 * @param status Output param filled with success/failure status. 962 * @return Reference to 'appendTo' parameter. 963 * @internal 964 */ 965 virtual UnicodeString& format(const DigitList &number, 966 UnicodeString& appendTo, 967 FieldPositionIterator* posIter, 968 UErrorCode& status) const; 969 970 /** 971 * Format a decimal number. 972 * The number is a DigitList wrapper onto a floating point decimal number. 973 * The default implementation in NumberFormat converts the decimal number 974 * to a double and formats that. 975 * 976 * @param number The number, a DigitList format Decimal Floating Point. 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 * @param status Output param filled with success/failure status. 982 * @return Reference to 'appendTo' parameter. 983 * @internal 984 */ 985 virtual UnicodeString& format(const DigitList &number, 986 UnicodeString& appendTo, 987 FieldPosition& pos, 988 UErrorCode& status) const; 989 990 991 /** 992 * Format a Formattable using base-10 representation. 993 * 994 * @param obj The value to be formatted. 995 * @param appendTo Output parameter to receive result. 996 * Result is appended to existing contents. 997 * @param pos On input: an alignment field, if desired. 998 * On output: the offsets of the alignment field. 999 * @param status Error code indicating success or failure. 1000 * @return Reference to 'appendTo' parameter. 1001 * @stable ICU 2.0 1002 */ 1003 virtual UnicodeString& format(const Formattable& obj, 1004 UnicodeString& appendTo, 1005 FieldPosition& pos, 1006 UErrorCode& status) const; 1007 1008 /** 1009 * Redeclared NumberFormat method. 1010 * Formats an object to produce a string. 1011 * 1012 * @param obj The object to format. 1013 * @param appendTo Output parameter to receive result. 1014 * Result is appended to existing contents. 1015 * @param status Output parameter filled in with success or failure status. 1016 * @return Reference to 'appendTo' parameter. 1017 * @stable ICU 2.0 1018 */ 1019 UnicodeString& format(const Formattable& obj, 1020 UnicodeString& appendTo, 1021 UErrorCode& status) const; 1022 1023 /** 1024 * Redeclared NumberFormat method. 1025 * Format a double number. 1026 * 1027 * @param number The value to be formatted. 1028 * @param appendTo Output parameter to receive result. 1029 * Result is appended to existing contents. 1030 * @return Reference to 'appendTo' parameter. 1031 * @stable ICU 2.0 1032 */ 1033 UnicodeString& format(double number, 1034 UnicodeString& appendTo) const; 1035 1036 /** 1037 * Redeclared NumberFormat method. 1038 * Format a long number. These methods call the NumberFormat 1039 * pure virtual format() methods with the default FieldPosition. 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 * @return Reference to 'appendTo' parameter. 1045 * @stable ICU 2.0 1046 */ 1047 UnicodeString& format(int32_t number, 1048 UnicodeString& appendTo) const; 1049 1050 /** 1051 * Redeclared NumberFormat method. 1052 * Format an int64 number. These methods call the NumberFormat 1053 * pure virtual format() methods with the default FieldPosition. 1054 * 1055 * @param number The value to be formatted. 1056 * @param appendTo Output parameter to receive result. 1057 * Result is appended to existing contents. 1058 * @return Reference to 'appendTo' parameter. 1059 * @stable ICU 2.8 1060 */ 1061 UnicodeString& format(int64_t number, 1062 UnicodeString& appendTo) const; 1063 /** 1064 * Parse the given string using this object's choices. The method 1065 * does string comparisons to try to find an optimal match. 1066 * If no object can be parsed, index is unchanged, and NULL is 1067 * returned. The result is returned as the most parsimonious 1068 * type of Formattable that will accomodate all of the 1069 * necessary precision. For example, if the result is exactly 12, 1070 * it will be returned as a long. However, if it is 1.5, it will 1071 * be returned as a double. 1072 * 1073 * @param text The text to be parsed. 1074 * @param result Formattable to be set to the parse result. 1075 * If parse fails, return contents are undefined. 1076 * @param parsePosition The position to start parsing at on input. 1077 * On output, moved to after the last successfully 1078 * parse character. On parse failure, does not change. 1079 * @see Formattable 1080 * @stable ICU 2.0 1081 */ 1082 virtual void parse(const UnicodeString& text, 1083 Formattable& result, 1084 ParsePosition& parsePosition) const; 1085 1086 // Declare here again to get rid of function hiding problems. 1087 /** 1088 * Parse the given string using this object's choices. 1089 * 1090 * @param text The text to be parsed. 1091 * @param result Formattable to be set to the parse result. 1092 * @param status Output parameter filled in with success or failure status. 1093 * @stable ICU 2.0 1094 */ 1095 virtual void parse(const UnicodeString& text, 1096 Formattable& result, 1097 UErrorCode& status) const; 1098 1099 /** 1100 * Parses text from the given string as a currency amount. Unlike 1101 * the parse() method, this method will attempt to parse a generic 1102 * currency name, searching for a match of this object's locale's 1103 * currency display names, or for a 3-letter ISO currency code. 1104 * This method will fail if this format is not a currency format, 1105 * that is, if it does not contain the currency pattern symbol 1106 * (U+00A4) in its prefix or suffix. 1107 * 1108 * @param text the string to parse 1109 * @param result output parameter to receive result. This will have 1110 * its currency set to the parsed ISO currency code. 1111 * @param pos input-output position; on input, the position within 1112 * text to match; must have 0 <= pos.getIndex() < text.length(); 1113 * on output, the position after the last matched character. If 1114 * the parse fails, the position in unchanged upon output. 1115 * @return a reference to result 1116 * @internal 1117 */ 1118 virtual Formattable& parseCurrency(const UnicodeString& text, 1119 Formattable& result, 1120 ParsePosition& pos) const; 1121 1122 /** 1123 * Returns the decimal format symbols, which is generally not changed 1124 * by the programmer or user. 1125 * @return desired DecimalFormatSymbols 1126 * @see DecimalFormatSymbols 1127 * @stable ICU 2.0 1128 */ 1129 virtual const DecimalFormatSymbols* getDecimalFormatSymbols(void) const; 1130 1131 /** 1132 * Sets the decimal format symbols, which is generally not changed 1133 * by the programmer or user. 1134 * @param symbolsToAdopt DecimalFormatSymbols to be adopted. 1135 * @stable ICU 2.0 1136 */ 1137 virtual void adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt); 1138 1139 /** 1140 * Sets the decimal format symbols, which is generally not changed 1141 * by the programmer or user. 1142 * @param symbols DecimalFormatSymbols. 1143 * @stable ICU 2.0 1144 */ 1145 virtual void setDecimalFormatSymbols(const DecimalFormatSymbols& symbols); 1146 1147 1148 /** 1149 * Returns the currency plural format information, 1150 * which is generally not changed by the programmer or user. 1151 * @return desired CurrencyPluralInfo 1152 * @stable ICU 4.2 1153 */ 1154 virtual const CurrencyPluralInfo* getCurrencyPluralInfo(void) const; 1155 1156 /** 1157 * Sets the currency plural format information, 1158 * which is generally not changed by the programmer or user. 1159 * @param toAdopt CurrencyPluralInfo to be adopted. 1160 * @stable ICU 4.2 1161 */ 1162 virtual void adoptCurrencyPluralInfo(CurrencyPluralInfo* toAdopt); 1163 1164 /** 1165 * Sets the currency plural format information, 1166 * which is generally not changed by the programmer or user. 1167 * @param info Currency Plural Info. 1168 * @stable ICU 4.2 1169 */ 1170 virtual void setCurrencyPluralInfo(const CurrencyPluralInfo& info); 1171 1172 1173 /** 1174 * Get the positive prefix. 1175 * 1176 * @param result Output param which will receive the positive prefix. 1177 * @return A reference to 'result'. 1178 * Examples: +123, $123, sFr123 1179 * @stable ICU 2.0 1180 */ 1181 UnicodeString& getPositivePrefix(UnicodeString& result) const; 1182 1183 /** 1184 * Set the positive prefix. 1185 * 1186 * @param newValue the new value of the the positive prefix to be set. 1187 * Examples: +123, $123, sFr123 1188 * @stable ICU 2.0 1189 */ 1190 virtual void setPositivePrefix(const UnicodeString& newValue); 1191 1192 /** 1193 * Get the negative prefix. 1194 * 1195 * @param result Output param which will receive the negative prefix. 1196 * @return A reference to 'result'. 1197 * Examples: -123, ($123) (with negative suffix), sFr-123 1198 * @stable ICU 2.0 1199 */ 1200 UnicodeString& getNegativePrefix(UnicodeString& result) const; 1201 1202 /** 1203 * Set the negative prefix. 1204 * 1205 * @param newValue the new value of the the negative prefix to be set. 1206 * Examples: -123, ($123) (with negative suffix), sFr-123 1207 * @stable ICU 2.0 1208 */ 1209 virtual void setNegativePrefix(const UnicodeString& newValue); 1210 1211 /** 1212 * Get the positive suffix. 1213 * 1214 * @param result Output param which will receive the positive suffix. 1215 * @return A reference to 'result'. 1216 * Example: 123% 1217 * @stable ICU 2.0 1218 */ 1219 UnicodeString& getPositiveSuffix(UnicodeString& result) const; 1220 1221 /** 1222 * Set the positive suffix. 1223 * 1224 * @param newValue the new value of the positive suffix to be set. 1225 * Example: 123% 1226 * @stable ICU 2.0 1227 */ 1228 virtual void setPositiveSuffix(const UnicodeString& newValue); 1229 1230 /** 1231 * Get the negative suffix. 1232 * 1233 * @param result Output param which will receive the negative suffix. 1234 * @return A reference to 'result'. 1235 * Examples: -123%, ($123) (with positive suffixes) 1236 * @stable ICU 2.0 1237 */ 1238 UnicodeString& getNegativeSuffix(UnicodeString& result) const; 1239 1240 /** 1241 * Set the negative suffix. 1242 * 1243 * @param newValue the new value of the negative suffix to be set. 1244 * Examples: 123% 1245 * @stable ICU 2.0 1246 */ 1247 virtual void setNegativeSuffix(const UnicodeString& newValue); 1248 1249 /** 1250 * Get the multiplier for use in percent, permill, etc. 1251 * For a percentage, set the suffixes to have "%" and the multiplier to be 100. 1252 * (For Arabic, use arabic percent symbol). 1253 * For a permill, set the suffixes to have "\\u2031" and the multiplier to be 1000. 1254 * 1255 * @return the multiplier for use in percent, permill, etc. 1256 * Examples: with 100, 1.23 -> "123", and "123" -> 1.23 1257 * @stable ICU 2.0 1258 */ 1259 int32_t getMultiplier(void) const; 1260 1261 /** 1262 * Set the multiplier for use in percent, permill, etc. 1263 * For a percentage, set the suffixes to have "%" and the multiplier to be 100. 1264 * (For Arabic, use arabic percent symbol). 1265 * For a permill, set the suffixes to have "\\u2031" and the multiplier to be 1000. 1266 * 1267 * @param newValue the new value of the multiplier for use in percent, permill, etc. 1268 * Examples: with 100, 1.23 -> "123", and "123" -> 1.23 1269 * @stable ICU 2.0 1270 */ 1271 virtual void setMultiplier(int32_t newValue); 1272 1273 /** 1274 * Get the rounding increment. 1275 * @return A positive rounding increment, or 0.0 if rounding 1276 * is not in effect. 1277 * @see #setRoundingIncrement 1278 * @see #getRoundingMode 1279 * @see #setRoundingMode 1280 * @stable ICU 2.0 1281 */ 1282 virtual double getRoundingIncrement(void) const; 1283 1284 /** 1285 * Set the rounding increment. This method also controls whether 1286 * rounding is enabled. 1287 * @param newValue A positive rounding increment, or 0.0 to disable rounding. 1288 * Negative increments are equivalent to 0.0. 1289 * @see #getRoundingIncrement 1290 * @see #getRoundingMode 1291 * @see #setRoundingMode 1292 * @stable ICU 2.0 1293 */ 1294 virtual void setRoundingIncrement(double newValue); 1295 1296 /** 1297 * Get the rounding mode. 1298 * @return A rounding mode 1299 * @see #setRoundingIncrement 1300 * @see #getRoundingIncrement 1301 * @see #setRoundingMode 1302 * @stable ICU 2.0 1303 */ 1304 virtual ERoundingMode getRoundingMode(void) const; 1305 1306 /** 1307 * Set the rounding mode. This has no effect unless the rounding 1308 * increment is greater than zero. 1309 * @param roundingMode A rounding mode 1310 * @see #setRoundingIncrement 1311 * @see #getRoundingIncrement 1312 * @see #getRoundingMode 1313 * @stable ICU 2.0 1314 */ 1315 virtual void setRoundingMode(ERoundingMode roundingMode); 1316 1317 /** 1318 * Get the width to which the output of format() is padded. 1319 * The width is counted in 16-bit code units. 1320 * @return the format width, or zero if no padding is in effect 1321 * @see #setFormatWidth 1322 * @see #getPadCharacterString 1323 * @see #setPadCharacter 1324 * @see #getPadPosition 1325 * @see #setPadPosition 1326 * @stable ICU 2.0 1327 */ 1328 virtual int32_t getFormatWidth(void) const; 1329 1330 /** 1331 * Set the width to which the output of format() is padded. 1332 * The width is counted in 16-bit code units. 1333 * This method also controls whether padding is enabled. 1334 * @param width the width to which to pad the result of 1335 * format(), or zero to disable padding. A negative 1336 * width is equivalent to 0. 1337 * @see #getFormatWidth 1338 * @see #getPadCharacterString 1339 * @see #setPadCharacter 1340 * @see #getPadPosition 1341 * @see #setPadPosition 1342 * @stable ICU 2.0 1343 */ 1344 virtual void setFormatWidth(int32_t width); 1345 1346 /** 1347 * Get the pad character used to pad to the format width. The 1348 * default is ' '. 1349 * @return a string containing the pad character. This will always 1350 * have a length of one 32-bit code point. 1351 * @see #setFormatWidth 1352 * @see #getFormatWidth 1353 * @see #setPadCharacter 1354 * @see #getPadPosition 1355 * @see #setPadPosition 1356 * @stable ICU 2.0 1357 */ 1358 virtual UnicodeString getPadCharacterString() const; 1359 1360 /** 1361 * Set the character used to pad to the format width. If padding 1362 * is not enabled, then this will take effect if padding is later 1363 * enabled. 1364 * @param padChar a string containing the pad charcter. If the string 1365 * has length 0, then the pad characer is set to ' '. Otherwise 1366 * padChar.char32At(0) will be used as the pad character. 1367 * @see #setFormatWidth 1368 * @see #getFormatWidth 1369 * @see #getPadCharacterString 1370 * @see #getPadPosition 1371 * @see #setPadPosition 1372 * @stable ICU 2.0 1373 */ 1374 virtual void setPadCharacter(const UnicodeString &padChar); 1375 1376 /** 1377 * Get the position at which padding will take place. This is the location 1378 * at which padding will be inserted if the result of format() 1379 * is shorter than the format width. 1380 * @return the pad position, one of kPadBeforePrefix, 1381 * kPadAfterPrefix, kPadBeforeSuffix, or 1382 * kPadAfterSuffix. 1383 * @see #setFormatWidth 1384 * @see #getFormatWidth 1385 * @see #setPadCharacter 1386 * @see #getPadCharacterString 1387 * @see #setPadPosition 1388 * @see #EPadPosition 1389 * @stable ICU 2.0 1390 */ 1391 virtual EPadPosition getPadPosition(void) const; 1392 1393 /** 1394 * Set the position at which padding will take place. This is the location 1395 * at which padding will be inserted if the result of format() 1396 * is shorter than the format width. This has no effect unless padding is 1397 * enabled. 1398 * @param padPos the pad position, one of kPadBeforePrefix, 1399 * kPadAfterPrefix, kPadBeforeSuffix, or 1400 * kPadAfterSuffix. 1401 * @see #setFormatWidth 1402 * @see #getFormatWidth 1403 * @see #setPadCharacter 1404 * @see #getPadCharacterString 1405 * @see #getPadPosition 1406 * @see #EPadPosition 1407 * @stable ICU 2.0 1408 */ 1409 virtual void setPadPosition(EPadPosition padPos); 1410 1411 /** 1412 * Return whether or not scientific notation is used. 1413 * @return TRUE if this object formats and parses scientific notation 1414 * @see #setScientificNotation 1415 * @see #getMinimumExponentDigits 1416 * @see #setMinimumExponentDigits 1417 * @see #isExponentSignAlwaysShown 1418 * @see #setExponentSignAlwaysShown 1419 * @stable ICU 2.0 1420 */ 1421 virtual UBool isScientificNotation(void); 1422 1423 /** 1424 * Set whether or not scientific notation is used. When scientific notation 1425 * is used, the effective maximum number of integer digits is <= 8. If the 1426 * maximum number of integer digits is set to more than 8, the effective 1427 * maximum will be 1. This allows this call to generate a 'default' scientific 1428 * number format without additional changes. 1429 * @param useScientific TRUE if this object formats and parses scientific 1430 * notation 1431 * @see #isScientificNotation 1432 * @see #getMinimumExponentDigits 1433 * @see #setMinimumExponentDigits 1434 * @see #isExponentSignAlwaysShown 1435 * @see #setExponentSignAlwaysShown 1436 * @stable ICU 2.0 1437 */ 1438 virtual void setScientificNotation(UBool useScientific); 1439 1440 /** 1441 * Return the minimum exponent digits that will be shown. 1442 * @return the minimum exponent digits that will be shown 1443 * @see #setScientificNotation 1444 * @see #isScientificNotation 1445 * @see #setMinimumExponentDigits 1446 * @see #isExponentSignAlwaysShown 1447 * @see #setExponentSignAlwaysShown 1448 * @stable ICU 2.0 1449 */ 1450 virtual int8_t getMinimumExponentDigits(void) const; 1451 1452 /** 1453 * Set the minimum exponent digits that will be shown. This has no 1454 * effect unless scientific notation is in use. 1455 * @param minExpDig a value >= 1 indicating the fewest exponent digits 1456 * that will be shown. Values less than 1 will be treated as 1. 1457 * @see #setScientificNotation 1458 * @see #isScientificNotation 1459 * @see #getMinimumExponentDigits 1460 * @see #isExponentSignAlwaysShown 1461 * @see #setExponentSignAlwaysShown 1462 * @stable ICU 2.0 1463 */ 1464 virtual void setMinimumExponentDigits(int8_t minExpDig); 1465 1466 /** 1467 * Return whether the exponent sign is always shown. 1468 * @return TRUE if the exponent is always prefixed with either the 1469 * localized minus sign or the localized plus sign, false if only negative 1470 * exponents are prefixed with the localized minus sign. 1471 * @see #setScientificNotation 1472 * @see #isScientificNotation 1473 * @see #setMinimumExponentDigits 1474 * @see #getMinimumExponentDigits 1475 * @see #setExponentSignAlwaysShown 1476 * @stable ICU 2.0 1477 */ 1478 virtual UBool isExponentSignAlwaysShown(void); 1479 1480 /** 1481 * Set whether the exponent sign is always shown. This has no effect 1482 * unless scientific notation is in use. 1483 * @param expSignAlways TRUE if the exponent is always prefixed with either 1484 * the localized minus sign or the localized plus sign, false if only 1485 * negative exponents are prefixed with the localized minus sign. 1486 * @see #setScientificNotation 1487 * @see #isScientificNotation 1488 * @see #setMinimumExponentDigits 1489 * @see #getMinimumExponentDigits 1490 * @see #isExponentSignAlwaysShown 1491 * @stable ICU 2.0 1492 */ 1493 virtual void setExponentSignAlwaysShown(UBool expSignAlways); 1494 1495 /** 1496 * Return the grouping size. Grouping size is the number of digits between 1497 * grouping separators in the integer portion of a number. For example, 1498 * in the number "123,456.78", the grouping size is 3. 1499 * 1500 * @return the grouping size. 1501 * @see setGroupingSize 1502 * @see NumberFormat::isGroupingUsed 1503 * @see DecimalFormatSymbols::getGroupingSeparator 1504 * @stable ICU 2.0 1505 */ 1506 int32_t getGroupingSize(void) const; 1507 1508 /** 1509 * Set the grouping size. Grouping size is the number of digits between 1510 * grouping separators in the integer portion of a number. For example, 1511 * in the number "123,456.78", the grouping size is 3. 1512 * 1513 * @param newValue the new value of the grouping size. 1514 * @see getGroupingSize 1515 * @see NumberFormat::setGroupingUsed 1516 * @see DecimalFormatSymbols::setGroupingSeparator 1517 * @stable ICU 2.0 1518 */ 1519 virtual void setGroupingSize(int32_t newValue); 1520 1521 /** 1522 * Return the secondary grouping size. In some locales one 1523 * grouping interval is used for the least significant integer 1524 * digits (the primary grouping size), and another is used for all 1525 * others (the secondary grouping size). A formatter supporting a 1526 * secondary grouping size will return a positive integer unequal 1527 * to the primary grouping size returned by 1528 * getGroupingSize(). For example, if the primary 1529 * grouping size is 4, and the secondary grouping size is 2, then 1530 * the number 123456789 formats as "1,23,45,6789", and the pattern 1531 * appears as "#,##,###0". 1532 * @return the secondary grouping size, or a value less than 1533 * one if there is none 1534 * @see setSecondaryGroupingSize 1535 * @see NumberFormat::isGroupingUsed 1536 * @see DecimalFormatSymbols::getGroupingSeparator 1537 * @stable ICU 2.4 1538 */ 1539 int32_t getSecondaryGroupingSize(void) const; 1540 1541 /** 1542 * Set the secondary grouping size. If set to a value less than 1, 1543 * then secondary grouping is turned off, and the primary grouping 1544 * size is used for all intervals, not just the least significant. 1545 * 1546 * @param newValue the new value of the secondary grouping size. 1547 * @see getSecondaryGroupingSize 1548 * @see NumberFormat#setGroupingUsed 1549 * @see DecimalFormatSymbols::setGroupingSeparator 1550 * @stable ICU 2.4 1551 */ 1552 virtual void setSecondaryGroupingSize(int32_t newValue); 1553 1554 /** 1555 * Allows you to get the behavior of the decimal separator with integers. 1556 * (The decimal separator will always appear with decimals.) 1557 * 1558 * @return TRUE if the decimal separator always appear with decimals. 1559 * Example: Decimal ON: 12345 -> 12345.; OFF: 12345 -> 12345 1560 * @stable ICU 2.0 1561 */ 1562 UBool isDecimalSeparatorAlwaysShown(void) const; 1563 1564 /** 1565 * Allows you to set the behavior of the decimal separator with integers. 1566 * (The decimal separator will always appear with decimals.) 1567 * 1568 * @param newValue set TRUE if the decimal separator will always appear with decimals. 1569 * Example: Decimal ON: 12345 -> 12345.; OFF: 12345 -> 12345 1570 * @stable ICU 2.0 1571 */ 1572 virtual void setDecimalSeparatorAlwaysShown(UBool newValue); 1573 1574 /** 1575 * Synthesizes a pattern string that represents the current state 1576 * of this Format object. 1577 * 1578 * @param result Output param which will receive the pattern. 1579 * Previous contents are deleted. 1580 * @return A reference to 'result'. 1581 * @see applyPattern 1582 * @stable ICU 2.0 1583 */ 1584 virtual UnicodeString& toPattern(UnicodeString& result) const; 1585 1586 /** 1587 * Synthesizes a localized pattern string that represents the current 1588 * state of this Format object. 1589 * 1590 * @param result Output param which will receive the localized pattern. 1591 * Previous contents are deleted. 1592 * @return A reference to 'result'. 1593 * @see applyPattern 1594 * @stable ICU 2.0 1595 */ 1596 virtual UnicodeString& toLocalizedPattern(UnicodeString& result) const; 1597 1598 /** 1599 * Apply the given pattern to this Format object. A pattern is a 1600 * short-hand specification for the various formatting properties. 1601 * These properties can also be changed individually through the 1602 * various setter methods. 1603 * <P> 1604 * There is no limit to integer digits are set 1605 * by this routine, since that is the typical end-user desire; 1606 * use setMaximumInteger if you want to set a real value. 1607 * For negative numbers, use a second pattern, separated by a semicolon 1608 * <pre> 1609 * . Example "#,#00.0#" -> 1,234.56 1610 * </pre> 1611 * This means a minimum of 2 integer digits, 1 fraction digit, and 1612 * a maximum of 2 fraction digits. 1613 * <pre> 1614 * . Example: "#,#00.0#;(#,#00.0#)" for negatives in parantheses. 1615 * </pre> 1616 * In negative patterns, the minimum and maximum counts are ignored; 1617 * these are presumed to be set in the positive pattern. 1618 * 1619 * @param pattern The pattern to be applied. 1620 * @param parseError Struct to recieve information on position 1621 * of error if an error is encountered 1622 * @param status Output param set to success/failure code on 1623 * exit. If the pattern is invalid, this will be 1624 * set to a failure result. 1625 * @stable ICU 2.0 1626 */ 1627 virtual void applyPattern(const UnicodeString& pattern, 1628 UParseError& parseError, 1629 UErrorCode& status); 1630 /** 1631 * Sets the pattern. 1632 * @param pattern The pattern to be applied. 1633 * @param status Output param set to success/failure code on 1634 * exit. If the pattern is invalid, this will be 1635 * set to a failure result. 1636 * @stable ICU 2.0 1637 */ 1638 virtual void applyPattern(const UnicodeString& pattern, 1639 UErrorCode& status); 1640 1641 /** 1642 * Apply the given pattern to this Format object. The pattern 1643 * is assumed to be in a localized notation. A pattern is a 1644 * short-hand specification for the various formatting properties. 1645 * These properties can also be changed individually through the 1646 * various setter methods. 1647 * <P> 1648 * There is no limit to integer digits are set 1649 * by this routine, since that is the typical end-user desire; 1650 * use setMaximumInteger if you want to set a real value. 1651 * For negative numbers, use a second pattern, separated by a semicolon 1652 * <pre> 1653 * . Example "#,#00.0#" -> 1,234.56 1654 * </pre> 1655 * This means a minimum of 2 integer digits, 1 fraction digit, and 1656 * a maximum of 2 fraction digits. 1657 * 1658 * Example: "#,#00.0#;(#,#00.0#)" for negatives in parantheses. 1659 * 1660 * In negative patterns, the minimum and maximum counts are ignored; 1661 * these are presumed to be set in the positive pattern. 1662 * 1663 * @param pattern The localized pattern to be applied. 1664 * @param parseError Struct to recieve information on position 1665 * of error if an error is encountered 1666 * @param status Output param set to success/failure code on 1667 * exit. If the pattern is invalid, this will be 1668 * set to a failure result. 1669 * @stable ICU 2.0 1670 */ 1671 virtual void applyLocalizedPattern(const UnicodeString& pattern, 1672 UParseError& parseError, 1673 UErrorCode& status); 1674 1675 /** 1676 * Apply the given pattern to this Format object. 1677 * 1678 * @param pattern The localized pattern to be applied. 1679 * @param status Output param set to success/failure code on 1680 * exit. If the pattern is invalid, this will be 1681 * set to a failure result. 1682 * @stable ICU 2.0 1683 */ 1684 virtual void applyLocalizedPattern(const UnicodeString& pattern, 1685 UErrorCode& status); 1686 1687 1688 /** 1689 * Sets the maximum number of digits allowed in the integer portion of a 1690 * number. This override limits the integer digit count to 309. 1691 * 1692 * @param newValue the new value of the maximum number of digits 1693 * allowed in the integer portion of a number. 1694 * @see NumberFormat#setMaximumIntegerDigits 1695 * @stable ICU 2.0 1696 */ 1697 virtual void setMaximumIntegerDigits(int32_t newValue); 1698 1699 /** 1700 * Sets the minimum number of digits allowed in the integer portion of a 1701 * number. This override limits the integer digit count to 309. 1702 * 1703 * @param newValue the new value of the minimum number of digits 1704 * allowed in the integer portion of a number. 1705 * @see NumberFormat#setMinimumIntegerDigits 1706 * @stable ICU 2.0 1707 */ 1708 virtual void setMinimumIntegerDigits(int32_t newValue); 1709 1710 /** 1711 * Sets the maximum number of digits allowed in the fraction portion of a 1712 * number. This override limits the fraction digit count to 340. 1713 * 1714 * @param newValue the new value of the maximum number of digits 1715 * allowed in the fraction portion of a number. 1716 * @see NumberFormat#setMaximumFractionDigits 1717 * @stable ICU 2.0 1718 */ 1719 virtual void setMaximumFractionDigits(int32_t newValue); 1720 1721 /** 1722 * Sets the minimum number of digits allowed in the fraction portion of a 1723 * number. This override limits the fraction digit count to 340. 1724 * 1725 * @param newValue the new value of the minimum number of digits 1726 * allowed in the fraction portion of a number. 1727 * @see NumberFormat#setMinimumFractionDigits 1728 * @stable ICU 2.0 1729 */ 1730 virtual void setMinimumFractionDigits(int32_t newValue); 1731 1732 /** 1733 * Returns the minimum number of significant digits that will be 1734 * displayed. This value has no effect unless areSignificantDigitsUsed() 1735 * returns true. 1736 * @return the fewest significant digits that will be shown 1737 * @stable ICU 3.0 1738 */ 1739 int32_t getMinimumSignificantDigits() const; 1740 1741 /** 1742 * Returns the maximum number of significant digits that will be 1743 * displayed. This value has no effect unless areSignificantDigitsUsed() 1744 * returns true. 1745 * @return the most significant digits that will be shown 1746 * @stable ICU 3.0 1747 */ 1748 int32_t getMaximumSignificantDigits() const; 1749 1750 /** 1751 * Sets the minimum number of significant digits that will be 1752 * displayed. If <code>min</code> is less than one then it is set 1753 * to one. If the maximum significant digits count is less than 1754 * <code>min</code>, then it is set to <code>min</code>. This 1755 * value has no effect unless areSignificantDigits() returns true. 1756 * @param min the fewest significant digits to be shown 1757 * @stable ICU 3.0 1758 */ 1759 void setMinimumSignificantDigits(int32_t min); 1760 1761 /** 1762 * Sets the maximum number of significant digits that will be 1763 * displayed. If <code>max</code> is less than one then it is set 1764 * to one. If the minimum significant digits count is greater 1765 * than <code>max</code>, then it is set to <code>max</code>. 1766 * This value has no effect unless areSignificantDigits() returns 1767 * true. 1768 * @param max the most significant digits to be shown 1769 * @stable ICU 3.0 1770 */ 1771 void setMaximumSignificantDigits(int32_t max); 1772 1773 /** 1774 * Returns true if significant digits are in use, or false if 1775 * integer and fraction digit counts are in use. 1776 * @return true if significant digits are in use 1777 * @stable ICU 3.0 1778 */ 1779 UBool areSignificantDigitsUsed() const; 1780 1781 /** 1782 * Sets whether significant digits are in use, or integer and 1783 * fraction digit counts are in use. 1784 * @param useSignificantDigits true to use significant digits, or 1785 * false to use integer and fraction digit counts 1786 * @stable ICU 3.0 1787 */ 1788 void setSignificantDigitsUsed(UBool useSignificantDigits); 1789 1790 public: 1791 /** 1792 * Sets the currency used to display currency 1793 * amounts. This takes effect immediately, if this format is a 1794 * currency format. If this format is not a currency format, then 1795 * the currency is used if and when this object becomes a 1796 * currency format through the application of a new pattern. 1797 * @param theCurrency a 3-letter ISO code indicating new currency 1798 * to use. It need not be null-terminated. May be the empty 1799 * string or NULL to indicate no currency. 1800 * @param ec input-output error code 1801 * @stable ICU 3.0 1802 */ 1803 virtual void setCurrency(const UChar* theCurrency, UErrorCode& ec); 1804 1805 /** 1806 * Sets the currency used to display currency amounts. See 1807 * setCurrency(const UChar*, UErrorCode&). 1808 * @deprecated ICU 3.0. Use setCurrency(const UChar*, UErrorCode&). 1809 */ 1810 virtual void setCurrency(const UChar* theCurrency); 1811 1812 /** 1813 * The resource tags we use to retrieve decimal format data from 1814 * locale resource bundles. 1815 * @deprecated ICU 3.4. This string has no public purpose. Please don't use it. 1816 */ 1817 static const char fgNumberPatterns[]; 1818 1819 public: 1820 1821 /** 1822 * Return the class ID for this class. This is useful only for 1823 * comparing to a return value from getDynamicClassID(). For example: 1824 * <pre> 1825 * . Base* polymorphic_pointer = createPolymorphicObject(); 1826 * . if (polymorphic_pointer->getDynamicClassID() == 1827 * . Derived::getStaticClassID()) ... 1828 * </pre> 1829 * @return The class ID for all objects of this class. 1830 * @stable ICU 2.0 1831 */ 1832 static UClassID U_EXPORT2 getStaticClassID(void); 1833 1834 /** 1835 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. 1836 * This method is to implement a simple version of RTTI, since not all 1837 * C++ compilers support genuine RTTI. Polymorphic operator==() and 1838 * clone() methods call this method. 1839 * 1840 * @return The class ID for this object. All objects of a 1841 * given class have the same class ID. Objects of 1842 * other classes have different class IDs. 1843 * @stable ICU 2.0 1844 */ 1845 virtual UClassID getDynamicClassID(void) const; 1846 1847 private: 1848 1849 DecimalFormat(); // default constructor not implemented 1850 1851 int32_t precision() const; 1852 1853 /** 1854 * Initialize all fields of a new DecimalFormatter. 1855 * Common code for use by constructors. 1856 */ 1857 void init(); 1858 1859 /** 1860 * Do real work of constructing a new DecimalFormat. 1861 */ 1862 void construct(UErrorCode& status, 1863 UParseError& parseErr, 1864 const UnicodeString* pattern = 0, 1865 DecimalFormatSymbols* symbolsToAdopt = 0 1866 ); 1867 1868 /** 1869 * Does the real work of generating a pattern. 1870 * 1871 * @param result Output param which will receive the pattern. 1872 * Previous contents are deleted. 1873 * @param localized TRUE return localized pattern. 1874 * @return A reference to 'result'. 1875 */ 1876 UnicodeString& toPattern(UnicodeString& result, UBool localized) const; 1877 1878 /** 1879 * Does the real work of applying a pattern. 1880 * @param pattern The pattern to be applied. 1881 * @param localized If true, the pattern is localized; else false. 1882 * @param parseError Struct to recieve information on position 1883 * of error if an error is encountered 1884 * @param status Output param set to success/failure code on 1885 * exit. If the pattern is invalid, this will be 1886 * set to a failure result. 1887 */ 1888 void applyPattern(const UnicodeString& pattern, 1889 UBool localized, 1890 UParseError& parseError, 1891 UErrorCode& status); 1892 1893 /* 1894 * similar to applyPattern, but without re-gen affix for currency 1895 */ 1896 void applyPatternInternally(const UnicodeString& pluralCount, 1897 const UnicodeString& pattern, 1898 UBool localized, 1899 UParseError& parseError, 1900 UErrorCode& status); 1901 1902 /* 1903 * only apply pattern without expand affixes 1904 */ 1905 void applyPatternWithoutExpandAffix(const UnicodeString& pattern, 1906 UBool localized, 1907 UParseError& parseError, 1908 UErrorCode& status); 1909 1910 1911 /* 1912 * expand affixes (after apply patter) and re-compute fFormatWidth 1913 */ 1914 void expandAffixAdjustWidth(const UnicodeString* pluralCount); 1915 1916 1917 /** 1918 * Do the work of formatting a number, either a double or a long. 1919 * 1920 * @param appendTo Output parameter to receive result. 1921 * Result is appended to existing contents. 1922 * @param handler Records information about field positions. 1923 * @param digits the digits to be formatted. 1924 * @param isInteger if TRUE format the digits as Integer. 1925 * @return Reference to 'appendTo' parameter. 1926 */ 1927 UnicodeString& subformat(UnicodeString& appendTo, 1928 FieldPositionHandler& handler, 1929 DigitList& digits, 1930 UBool isInteger) const; 1931 1932 1933 void parse(const UnicodeString& text, 1934 Formattable& result, 1935 ParsePosition& pos, 1936 UBool parseCurrency) const; 1937 1938 enum { 1939 fgStatusInfinite, 1940 fgStatusLength // Leave last in list. 1941 } StatusFlags; 1942 1943 UBool subparse(const UnicodeString& text, 1944 const UnicodeString* negPrefix, 1945 const UnicodeString* negSuffix, 1946 const UnicodeString* posPrefix, 1947 const UnicodeString* posSuffix, 1948 UBool currencyParsing, 1949 int8_t type, 1950 ParsePosition& parsePosition, 1951 DigitList& digits, UBool* status, 1952 UChar* currency) const; 1953 1954 // Mixed style parsing for currency. 1955 // It parses against the current currency pattern 1956 // using complex affix comparison 1957 // parses against the currency plural patterns using complex affix comparison, 1958 // and parses against the current pattern using simple affix comparison. 1959 UBool parseForCurrency(const UnicodeString& text, 1960 ParsePosition& parsePosition, 1961 DigitList& digits, 1962 UBool* status, 1963 UChar* currency) const; 1964 1965 int32_t skipPadding(const UnicodeString& text, int32_t position) const; 1966 1967 int32_t compareAffix(const UnicodeString& input, 1968 int32_t pos, 1969 UBool isNegative, 1970 UBool isPrefix, 1971 const UnicodeString* affixPat, 1972 UBool currencyParsing, 1973 int8_t type, 1974 UChar* currency) const; 1975 1976 static int32_t compareSimpleAffix(const UnicodeString& affix, 1977 const UnicodeString& input, 1978 int32_t pos); 1979 1980 static int32_t skipRuleWhiteSpace(const UnicodeString& text, int32_t pos); 1981 1982 static int32_t skipUWhiteSpace(const UnicodeString& text, int32_t pos); 1983 1984 int32_t compareComplexAffix(const UnicodeString& affixPat, 1985 const UnicodeString& input, 1986 int32_t pos, 1987 int8_t type, 1988 UChar* currency) const; 1989 1990 static int32_t match(const UnicodeString& text, int32_t pos, UChar32 ch); 1991 1992 static int32_t match(const UnicodeString& text, int32_t pos, const UnicodeString& str); 1993 1994 /** 1995 * Get a decimal format symbol. 1996 * Returns a const reference to the symbol string. 1997 * @internal 1998 */ 1999 inline const UnicodeString &getConstSymbol(DecimalFormatSymbols::ENumberFormatSymbol symbol) const; 2000 2001 int32_t appendAffix(UnicodeString& buf, 2002 double number, 2003 FieldPositionHandler& handler, 2004 UBool isNegative, 2005 UBool isPrefix) const; 2006 2007 /** 2008 * Append an affix to the given UnicodeString, using quotes if 2009 * there are special characters. Single quotes themselves must be 2010 * escaped in either case. 2011 */ 2012 void appendAffixPattern(UnicodeString& appendTo, const UnicodeString& affix, 2013 UBool localized) const; 2014 2015 void appendAffixPattern(UnicodeString& appendTo, 2016 const UnicodeString* affixPattern, 2017 const UnicodeString& expAffix, UBool localized) const; 2018 2019 void expandAffix(const UnicodeString& pattern, 2020 UnicodeString& affix, 2021 double number, 2022 FieldPositionHandler& handler, 2023 UBool doFormat, 2024 const UnicodeString* pluralCount) const; 2025 2026 void expandAffixes(const UnicodeString* pluralCount); 2027 2028 void addPadding(UnicodeString& appendTo, 2029 FieldPositionHandler& handler, 2030 int32_t prefixLen, int32_t suffixLen) const; 2031 2032 UBool isGroupingPosition(int32_t pos) const; 2033 2034 void setCurrencyForSymbols(); 2035 2036 // similar to setCurrency without re-compute the affixes for currency. 2037 // If currency changes, the affix pattern for currency is not changed, 2038 // but the affix will be changed. So, affixes need to be 2039 // re-computed in setCurrency(), but not in setCurrencyInternally(). 2040 virtual void setCurrencyInternally(const UChar* theCurrency, UErrorCode& ec); 2041 2042 // set up currency affix patterns for mix parsing. 2043 // The patterns saved here are the affix patterns of default currency 2044 // pattern and the unique affix patterns of the plural currency patterns. 2045 // Those patterns are used by parseForCurrency(). 2046 void setupCurrencyAffixPatterns(UErrorCode& status); 2047 2048 // set up the currency affixes used in currency plural formatting. 2049 // It sets up both fAffixesForCurrency for currency pattern if the current 2050 // pattern contains 3 currency signs, 2051 // and it sets up fPluralAffixesForCurrency for currency plural patterns. 2052 void setupCurrencyAffixes(const UnicodeString& pattern, 2053 UBool setupForCurrentPattern, 2054 UBool setupForPluralPattern, 2055 UErrorCode& status); 2056 2057 // hashtable operations 2058 Hashtable* initHashForAffixPattern(UErrorCode& status); 2059 Hashtable* initHashForAffix(UErrorCode& status); 2060 2061 void deleteHashForAffixPattern(); 2062 void deleteHashForAffix(Hashtable*& table); 2063 2064 void copyHashForAffixPattern(const Hashtable* source, 2065 Hashtable* target, UErrorCode& status); 2066 void copyHashForAffix(const Hashtable* source, 2067 Hashtable* target, UErrorCode& status); 2068 2069 UnicodeString& _format(int64_t number, 2070 UnicodeString& appendTo, 2071 FieldPositionHandler& handler) const; 2072 UnicodeString& _format(double number, 2073 UnicodeString& appendTo, 2074 FieldPositionHandler& handler) const; 2075 UnicodeString& _format(const DigitList &number, 2076 UnicodeString& appendTo, 2077 FieldPositionHandler& handler, 2078 UErrorCode &status) const; 2079 2080 // currency sign count 2081 enum { 2082 fgCurrencySignCountZero, 2083 fgCurrencySignCountInSymbolFormat, 2084 fgCurrencySignCountInISOFormat, 2085 fgCurrencySignCountInPluralFormat 2086 } CurrencySignCount; 2087 2088 /** 2089 * Constants. 2090 */ 2091 2092 UnicodeString fPositivePrefix; 2093 UnicodeString fPositiveSuffix; 2094 UnicodeString fNegativePrefix; 2095 UnicodeString fNegativeSuffix; 2096 UnicodeString* fPosPrefixPattern; 2097 UnicodeString* fPosSuffixPattern; 2098 UnicodeString* fNegPrefixPattern; 2099 UnicodeString* fNegSuffixPattern; 2100 2101 /** 2102 * Formatter for ChoiceFormat-based currency names. If this field 2103 * is not null, then delegate to it to format currency symbols. 2104 * @since ICU 2.6 2105 */ 2106 ChoiceFormat* fCurrencyChoice; 2107 2108 DigitList * fMultiplier; // NULL for multiplier of one 2109 int32_t fGroupingSize; 2110 int32_t fGroupingSize2; 2111 UBool fDecimalSeparatorAlwaysShown; 2112 DecimalFormatSymbols* fSymbols; 2113 2114 UBool fUseSignificantDigits; 2115 int32_t fMinSignificantDigits; 2116 int32_t fMaxSignificantDigits; 2117 2118 UBool fUseExponentialNotation; 2119 int8_t fMinExponentDigits; 2120 UBool fExponentSignAlwaysShown; 2121 2122 DigitList* fRoundingIncrement; // NULL if no rounding increment specified. 2123 ERoundingMode fRoundingMode; 2124 2125 UChar32 fPad; 2126 int32_t fFormatWidth; 2127 EPadPosition fPadPosition; 2128 2129 /* 2130 * Following are used for currency format 2131 */ 2132 // pattern used in this formatter 2133 UnicodeString fFormatPattern; 2134 // style is only valid when decimal formatter is constructed by 2135 // DecimalFormat(pattern, decimalFormatSymbol, style) 2136 int fStyle; 2137 /* 2138 * Represents whether this is a currency format, and which 2139 * currency format style. 2140 * 0: not currency format type; 2141 * 1: currency style -- symbol name, such as "$" for US dollar. 2142 * 2: currency style -- ISO name, such as USD for US dollar. 2143 * 3: currency style -- plural long name, such as "US Dollar" for 2144 * "1.00 US Dollar", or "US Dollars" for 2145 * "3.00 US Dollars". 2146 */ 2147 int fCurrencySignCount; 2148 2149 2150 /* For currency parsing purose, 2151 * Need to remember all prefix patterns and suffix patterns of 2152 * every currency format pattern, 2153 * including the pattern of default currecny style 2154 * and plural currency style. And the patterns are set through applyPattern. 2155 */ 2156 // TODO: innerclass? 2157 /* This is not needed in the class declaration, so it is moved into decimfmp.cpp 2158 struct AffixPatternsForCurrency : public UMemory { 2159 // negative prefix pattern 2160 UnicodeString negPrefixPatternForCurrency; 2161 // negative suffix pattern 2162 UnicodeString negSuffixPatternForCurrency; 2163 // positive prefix pattern 2164 UnicodeString posPrefixPatternForCurrency; 2165 // positive suffix pattern 2166 UnicodeString posSuffixPatternForCurrency; 2167 int8_t patternType; 2168 2169 AffixPatternsForCurrency(const UnicodeString& negPrefix, 2170 const UnicodeString& negSuffix, 2171 const UnicodeString& posPrefix, 2172 const UnicodeString& posSuffix, 2173 int8_t type) { 2174 negPrefixPatternForCurrency = negPrefix; 2175 negSuffixPatternForCurrency = negSuffix; 2176 posPrefixPatternForCurrency = posPrefix; 2177 posSuffixPatternForCurrency = posSuffix; 2178 patternType = type; 2179 } 2180 }; 2181 */ 2182 2183 /* affix for currency formatting when the currency sign in the pattern 2184 * equals to 3, such as the pattern contains 3 currency sign or 2185 * the formatter style is currency plural format style. 2186 */ 2187 /* This is not needed in the class declaration, so it is moved into decimfmp.cpp 2188 struct AffixesForCurrency : public UMemory { 2189 // negative prefix 2190 UnicodeString negPrefixForCurrency; 2191 // negative suffix 2192 UnicodeString negSuffixForCurrency; 2193 // positive prefix 2194 UnicodeString posPrefixForCurrency; 2195 // positive suffix 2196 UnicodeString posSuffixForCurrency; 2197 2198 int32_t formatWidth; 2199 2200 AffixesForCurrency(const UnicodeString& negPrefix, 2201 const UnicodeString& negSuffix, 2202 const UnicodeString& posPrefix, 2203 const UnicodeString& posSuffix) { 2204 negPrefixForCurrency = negPrefix; 2205 negSuffixForCurrency = negSuffix; 2206 posPrefixForCurrency = posPrefix; 2207 posSuffixForCurrency = posSuffix; 2208 } 2209 }; 2210 */ 2211 2212 // Affix pattern set for currency. 2213 // It is a set of AffixPatternsForCurrency, 2214 // each element of the set saves the negative prefix pattern, 2215 // negative suffix pattern, positive prefix pattern, 2216 // and positive suffix pattern of a pattern. 2217 // It is used for currency mixed style parsing. 2218 // It is actually is a set. 2219 // The set contains the default currency pattern from the locale, 2220 // and the currency plural patterns. 2221 // Since it is a set, it does not contain duplicated items. 2222 // For example, if 2 currency plural patterns are the same, only one pattern 2223 // is included in the set. When parsing, we do not check whether the plural 2224 // count match or not. 2225 Hashtable* fAffixPatternsForCurrency; 2226 2227 // Following 2 are affixes for currency. 2228 // It is a hash map from plural count to AffixesForCurrency. 2229 // AffixesForCurrency saves the negative prefix, 2230 // negative suffix, positive prefix, and positive suffix of a pattern. 2231 // It is used during currency formatting only when the currency sign count 2232 // is 3. In which case, the affixes are getting from here, not 2233 // from the fNegativePrefix etc. 2234 Hashtable* fAffixesForCurrency; // for current pattern 2235 Hashtable* fPluralAffixesForCurrency; // for plural pattern 2236 2237 // Information needed for DecimalFormat to format/parse currency plural. 2238 CurrencyPluralInfo* fCurrencyPluralInfo; 2239 2240 protected: 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(UChar* 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 inline UnicodeString& 2275 DecimalFormat::format(const Formattable& obj, 2276 UnicodeString& appendTo, 2277 UErrorCode& status) const { 2278 // Don't use Format:: - use immediate base class only, 2279 // in case immediate base modifies behavior later. 2280 return NumberFormat::format(obj, appendTo, status); 2281 } 2282 2283 inline UnicodeString& 2284 DecimalFormat::format(double number, 2285 UnicodeString& appendTo) const { 2286 FieldPosition pos(0); 2287 return format(number, appendTo, pos); 2288 } 2289 2290 inline UnicodeString& 2291 DecimalFormat::format(int32_t number, 2292 UnicodeString& appendTo) const { 2293 FieldPosition pos(0); 2294 return format((int64_t)number, appendTo, pos); 2295 } 2296 2297 inline const UnicodeString & 2298 DecimalFormat::getConstSymbol(DecimalFormatSymbols::ENumberFormatSymbol symbol) const { 2299 return fSymbols->getConstSymbol(symbol); 2300 } 2301 2302 U_NAMESPACE_END 2303 2304 #endif /* #if !UCONFIG_NO_FORMATTING */ 2305 2306 #endif // _DECIMFMT 2307 //eof 2308