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