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