]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/unicode/decimfmt.h
ICU-491.11.1.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / decimfmt.h
1 /*
2 ********************************************************************************
3 * Copyright (C) 1997-2012, 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 #include "unicode/curramt.h"
41
42 U_NAMESPACE_BEGIN
43
44 class DigitList;
45 class ChoiceFormat;
46 class CurrencyPluralInfo;
47 class Hashtable;
48 class UnicodeSet;
49 class FieldPositionHandler;
50
51 /**
52 * DecimalFormat is a concrete subclass of NumberFormat that formats decimal
53 * numbers. It has a variety of features designed to make it possible to parse
54 * and format numbers in any locale, including support for Western, Arabic, or
55 * Indic digits. It also supports different flavors of numbers, including
56 * integers ("123"), fixed-point numbers ("123.4"), scientific notation
57 * ("1.23E4"), percentages ("12%"), and currency amounts ("$123", "USD123",
58 * "123 US dollars"). All of these flavors can be easily localized.
59 *
60 * <p>To obtain a NumberFormat for a specific locale (including the default
61 * locale) call one of NumberFormat's factory methods such as
62 * createInstance(). Do not call the DecimalFormat constructors directly, unless
63 * you know what you are doing, since the NumberFormat factory methods may
64 * return subclasses other than DecimalFormat.
65 *
66 * <p><strong>Example Usage</strong>
67 *
68 * \code
69 * // Normally we would have a GUI with a menu for this
70 * int32_t locCount;
71 * const Locale* locales = NumberFormat::getAvailableLocales(locCount);
72 *
73 * double myNumber = -1234.56;
74 * UErrorCode success = U_ZERO_ERROR;
75 * NumberFormat* form;
76 *
77 * // Print out a number with the localized number, currency and percent
78 * // format for each locale.
79 * UnicodeString countryName;
80 * UnicodeString displayName;
81 * UnicodeString str;
82 * UnicodeString pattern;
83 * Formattable fmtable;
84 * for (int32_t j = 0; j < 3; ++j) {
85 * cout << endl << "FORMAT " << j << endl;
86 * for (int32_t i = 0; i < locCount; ++i) {
87 * if (locales[i].getCountry(countryName).size() == 0) {
88 * // skip language-only
89 * continue;
90 * }
91 * switch (j) {
92 * case 0:
93 * form = NumberFormat::createInstance(locales[i], success ); break;
94 * case 1:
95 * form = NumberFormat::createCurrencyInstance(locales[i], success ); break;
96 * default:
97 * form = NumberFormat::createPercentInstance(locales[i], success ); break;
98 * }
99 * if (form) {
100 * str.remove();
101 * pattern = ((DecimalFormat*)form)->toPattern(pattern);
102 * cout << locales[i].getDisplayName(displayName) << ": " << pattern;
103 * cout << " -> " << form->format(myNumber,str) << endl;
104 * form->parse(form->format(myNumber,str), fmtable, success);
105 * delete form;
106 * }
107 * }
108 * }
109 * \endcode
110 * <P>
111 * Another example use createInstance(style)
112 * <P>
113 * <pre>
114 * <strong>// Print out a number using the localized number, currency,
115 * // percent, scientific, integer, iso currency, and plural currency
116 * // format for each locale</strong>
117 * Locale* locale = new Locale("en", "US");
118 * double myNumber = 1234.56;
119 * UErrorCode success = U_ZERO_ERROR;
120 * UnicodeString str;
121 * Formattable fmtable;
122 * for (int j=NumberFormat::kNumberStyle;
123 * j<=NumberFormat::kPluralCurrencyStyle;
124 * ++j) {
125 * NumberFormat* format = NumberFormat::createInstance(locale, j, success);
126 * str.remove();
127 * cout << "format result " << form->format(myNumber, str) << endl;
128 * format->parse(form->format(myNumber, str), fmtable, success);
129 * }</pre>
130 *
131 *
132 * <p><strong>Patterns</strong>
133 *
134 * <p>A DecimalFormat consists of a <em>pattern</em> and a set of
135 * <em>symbols</em>. The pattern may be set directly using
136 * applyPattern(), or indirectly using other API methods which
137 * manipulate aspects of the pattern, such as the minimum number of integer
138 * digits. The symbols are stored in a DecimalFormatSymbols
139 * object. When using the NumberFormat factory methods, the
140 * pattern and symbols are read from ICU's locale data.
141 *
142 * <p><strong>Special Pattern Characters</strong>
143 *
144 * <p>Many characters in a pattern are taken literally; they are matched during
145 * parsing and output unchanged during formatting. Special characters, on the
146 * other hand, stand for other characters, strings, or classes of characters.
147 * For example, the '#' character is replaced by a localized digit. Often the
148 * replacement character is the same as the pattern character; in the U.S. locale,
149 * the ',' grouping character is replaced by ','. However, the replacement is
150 * still happening, and if the symbols are modified, the grouping character
151 * changes. Some special characters affect the behavior of the formatter by
152 * their presence; for example, if the percent character is seen, then the
153 * value is multiplied by 100 before being displayed.
154 *
155 * <p>To insert a special character in a pattern as a literal, that is, without
156 * any special meaning, the character must be quoted. There are some exceptions to
157 * this which are noted below.
158 *
159 * <p>The characters listed here are used in non-localized patterns. Localized
160 * patterns use the corresponding characters taken from this formatter's
161 * DecimalFormatSymbols object instead, and these characters lose
162 * their special status. Two exceptions are the currency sign and quote, which
163 * are not localized.
164 *
165 * <table border=0 cellspacing=3 cellpadding=0>
166 * <tr bgcolor="#ccccff">
167 * <td align=left><strong>Symbol</strong>
168 * <td align=left><strong>Location</strong>
169 * <td align=left><strong>Localized?</strong>
170 * <td align=left><strong>Meaning</strong>
171 * <tr valign=top>
172 * <td><code>0</code>
173 * <td>Number
174 * <td>Yes
175 * <td>Digit
176 * <tr valign=top bgcolor="#eeeeff">
177 * <td><code>1-9</code>
178 * <td>Number
179 * <td>Yes
180 * <td>'1' through '9' indicate rounding.
181 * <tr valign=top>
182 * <td><code>\htmlonly&#x40;\endhtmlonly</code> <!--doxygen doesn't like @-->
183 * <td>Number
184 * <td>No
185 * <td>Significant digit
186 * <tr valign=top bgcolor="#eeeeff">
187 * <td><code>#</code>
188 * <td>Number
189 * <td>Yes
190 * <td>Digit, zero shows as absent
191 * <tr valign=top>
192 * <td><code>.</code>
193 * <td>Number
194 * <td>Yes
195 * <td>Decimal separator or monetary decimal separator
196 * <tr valign=top bgcolor="#eeeeff">
197 * <td><code>-</code>
198 * <td>Number
199 * <td>Yes
200 * <td>Minus sign
201 * <tr valign=top>
202 * <td><code>,</code>
203 * <td>Number
204 * <td>Yes
205 * <td>Grouping separator
206 * <tr valign=top bgcolor="#eeeeff">
207 * <td><code>E</code>
208 * <td>Number
209 * <td>Yes
210 * <td>Separates mantissa and exponent in scientific notation.
211 * <em>Need not be quoted in prefix or suffix.</em>
212 * <tr valign=top>
213 * <td><code>+</code>
214 * <td>Exponent
215 * <td>Yes
216 * <td>Prefix positive exponents with localized plus sign.
217 * <em>Need not be quoted in prefix or suffix.</em>
218 * <tr valign=top bgcolor="#eeeeff">
219 * <td><code>;</code>
220 * <td>Subpattern boundary
221 * <td>Yes
222 * <td>Separates positive and negative subpatterns
223 * <tr valign=top>
224 * <td><code>\%</code>
225 * <td>Prefix or suffix
226 * <td>Yes
227 * <td>Multiply by 100 and show as percentage
228 * <tr valign=top bgcolor="#eeeeff">
229 * <td><code>\\u2030</code>
230 * <td>Prefix or suffix
231 * <td>Yes
232 * <td>Multiply by 1000 and show as per mille
233 * <tr valign=top>
234 * <td><code>\htmlonly&curren;\endhtmlonly</code> (<code>\\u00A4</code>)
235 * <td>Prefix or suffix
236 * <td>No
237 * <td>Currency sign, replaced by currency symbol. If
238 * doubled, replaced by international currency symbol.
239 * If tripled, replaced by currency plural names, for example,
240 * "US dollar" or "US dollars" for America.
241 * If present in a pattern, the monetary decimal separator
242 * is used instead of the decimal separator.
243 * <tr valign=top bgcolor="#eeeeff">
244 * <td><code>'</code>
245 * <td>Prefix or suffix
246 * <td>No
247 * <td>Used to quote special characters in a prefix or suffix,
248 * for example, <code>"'#'#"</code> formats 123 to
249 * <code>"#123"</code>. To create a single quote
250 * itself, use two in a row: <code>"# o''clock"</code>.
251 * <tr valign=top>
252 * <td><code>*</code>
253 * <td>Prefix or suffix boundary
254 * <td>Yes
255 * <td>Pad escape, precedes pad character
256 * </table>
257 *
258 * <p>A DecimalFormat pattern contains a postive and negative
259 * subpattern, for example, "#,##0.00;(#,##0.00)". Each subpattern has a
260 * prefix, a numeric part, and a suffix. If there is no explicit negative
261 * subpattern, the negative subpattern is the localized minus sign prefixed to the
262 * positive subpattern. That is, "0.00" alone is equivalent to "0.00;-0.00". If there
263 * is an explicit negative subpattern, it serves only to specify the negative
264 * prefix and suffix; the number of digits, minimal digits, and other
265 * characteristics are ignored in the negative subpattern. That means that
266 * "#,##0.0#;(#)" has precisely the same result as "#,##0.0#;(#,##0.0#)".
267 *
268 * <p>The prefixes, suffixes, and various symbols used for infinity, digits,
269 * thousands separators, decimal separators, etc. may be set to arbitrary
270 * values, and they will appear properly during formatting. However, care must
271 * be taken that the symbols and strings do not conflict, or parsing will be
272 * unreliable. For example, either the positive and negative prefixes or the
273 * suffixes must be distinct for parse() to be able
274 * to distinguish positive from negative values. Another example is that the
275 * decimal separator and thousands separator should be distinct characters, or
276 * parsing will be impossible.
277 *
278 * <p>The <em>grouping separator</em> is a character that separates clusters of
279 * integer digits to make large numbers more legible. It commonly used for
280 * thousands, but in some locales it separates ten-thousands. The <em>grouping
281 * size</em> is the number of digits between the grouping separators, such as 3
282 * for "100,000,000" or 4 for "1 0000 0000". There are actually two different
283 * grouping sizes: One used for the least significant integer digits, the
284 * <em>primary grouping size</em>, and one used for all others, the
285 * <em>secondary grouping size</em>. In most locales these are the same, but
286 * sometimes they are different. For example, if the primary grouping interval
287 * is 3, and the secondary is 2, then this corresponds to the pattern
288 * "#,##,##0", and the number 123456789 is formatted as "12,34,56,789". If a
289 * pattern contains multiple grouping separators, the interval between the last
290 * one and the end of the integer defines the primary grouping size, and the
291 * interval between the last two defines the secondary grouping size. All others
292 * are ignored, so "#,##,###,####" == "###,###,####" == "##,#,###,####".
293 *
294 * <p>Illegal patterns, such as "#.#.#" or "#.###,###", will cause
295 * DecimalFormat to set a failing UErrorCode.
296 *
297 * <p><strong>Pattern BNF</strong>
298 *
299 * <pre>
300 * pattern := subpattern (';' subpattern)?
301 * subpattern := prefix? number exponent? suffix?
302 * number := (integer ('.' fraction)?) | sigDigits
303 * prefix := '\\u0000'..'\\uFFFD' - specialCharacters
304 * suffix := '\\u0000'..'\\uFFFD' - specialCharacters
305 * integer := '#'* '0'* '0'
306 * fraction := '0'* '#'*
307 * sigDigits := '#'* '@' '@'* '#'*
308 * exponent := 'E' '+'? '0'* '0'
309 * padSpec := '*' padChar
310 * padChar := '\\u0000'..'\\uFFFD' - quote
311 * &nbsp;
312 * Notation:
313 * X* 0 or more instances of X
314 * X? 0 or 1 instances of X
315 * X|Y either X or Y
316 * C..D any character from C up to D, inclusive
317 * S-T characters in S, except those in T
318 * </pre>
319 * The first subpattern is for positive numbers. The second (optional)
320 * subpattern is for negative numbers.
321 *
322 * <p>Not indicated in the BNF syntax above:
323 *
324 * <ul><li>The grouping separator ',' can occur inside the integer and
325 * sigDigits elements, between any two pattern characters of that
326 * element, as long as the integer or sigDigits element is not
327 * followed by the exponent element.
328 *
329 * <li>Two grouping intervals are recognized: That between the
330 * decimal point and the first grouping symbol, and that
331 * between the first and second grouping symbols. These
332 * intervals are identical in most locales, but in some
333 * locales they differ. For example, the pattern
334 * &quot;#,##,###&quot; formats the number 123456789 as
335 * &quot;12,34,56,789&quot;.</li>
336 *
337 * <li>The pad specifier <code>padSpec</code> may appear before the prefix,
338 * after the prefix, before the suffix, after the suffix, or not at all.
339 *
340 * <li>In place of '0', the digits '1' through '9' may be used to
341 * indicate a rounding increment.
342 * </ul>
343 *
344 * <p><strong>Parsing</strong>
345 *
346 * <p>DecimalFormat parses all Unicode characters that represent
347 * decimal digits, as defined by u_charDigitValue(). In addition,
348 * DecimalFormat also recognizes as digits the ten consecutive
349 * characters starting with the localized zero digit defined in the
350 * DecimalFormatSymbols object. During formatting, the
351 * DecimalFormatSymbols-based digits are output.
352 *
353 * <p>During parsing, grouping separators are ignored.
354 *
355 * <p>For currency parsing, the formatter is able to parse every currency
356 * style formats no matter which style the formatter is constructed with.
357 * For example, a formatter instance gotten from
358 * NumberFormat.getInstance(ULocale, NumberFormat.CURRENCYSTYLE) can parse
359 * formats such as "USD1.00" and "3.00 US dollars".
360 *
361 * <p>If parse(UnicodeString&,Formattable&,ParsePosition&)
362 * fails to parse a string, it leaves the parse position unchanged.
363 * The convenience method parse(UnicodeString&,Formattable&,UErrorCode&)
364 * indicates parse failure by setting a failing
365 * UErrorCode.
366 *
367 * <p><strong>Formatting</strong>
368 *
369 * <p>Formatting is guided by several parameters, all of which can be
370 * specified either using a pattern or using the API. The following
371 * description applies to formats that do not use <a href="#sci">scientific
372 * notation</a> or <a href="#sigdig">significant digits</a>.
373 *
374 * <ul><li>If the number of actual integer digits exceeds the
375 * <em>maximum integer digits</em>, then only the least significant
376 * digits are shown. For example, 1997 is formatted as "97" if the
377 * maximum integer digits is set to 2.
378 *
379 * <li>If the number of actual integer digits is less than the
380 * <em>minimum integer digits</em>, then leading zeros are added. For
381 * example, 1997 is formatted as "01997" if the minimum integer digits
382 * is set to 5.
383 *
384 * <li>If the number of actual fraction digits exceeds the <em>maximum
385 * fraction digits</em>, then rounding is performed to the
386 * maximum fraction digits. For example, 0.125 is formatted as "0.12"
387 * if the maximum fraction digits is 2. This behavior can be changed
388 * by specifying a rounding increment and/or a rounding mode.
389 *
390 * <li>If the number of actual fraction digits is less than the
391 * <em>minimum fraction digits</em>, then trailing zeros are added.
392 * For example, 0.125 is formatted as "0.1250" if the mimimum fraction
393 * digits is set to 4.
394 *
395 * <li>Trailing fractional zeros are not displayed if they occur
396 * <em>j</em> positions after the decimal, where <em>j</em> is less
397 * than the maximum fraction digits. For example, 0.10004 is
398 * formatted as "0.1" if the maximum fraction digits is four or less.
399 * </ul>
400 *
401 * <p><strong>Special Values</strong>
402 *
403 * <p><code>NaN</code> is represented as a single character, typically
404 * <code>\\uFFFD</code>. This character is determined by the
405 * DecimalFormatSymbols object. This is the only value for which
406 * the prefixes and suffixes are not used.
407 *
408 * <p>Infinity is represented as a single character, typically
409 * <code>\\u221E</code>, with the positive or negative prefixes and suffixes
410 * applied. The infinity character is determined by the
411 * DecimalFormatSymbols object.
412 *
413 * <a name="sci"><strong>Scientific Notation</strong></a>
414 *
415 * <p>Numbers in scientific notation are expressed as the product of a mantissa
416 * and a power of ten, for example, 1234 can be expressed as 1.234 x 10<sup>3</sup>. The
417 * mantissa is typically in the half-open interval [1.0, 10.0) or sometimes [0.0, 1.0),
418 * but it need not be. DecimalFormat supports arbitrary mantissas.
419 * DecimalFormat can be instructed to use scientific
420 * notation through the API or through the pattern. In a pattern, the exponent
421 * character immediately followed by one or more digit characters indicates
422 * scientific notation. Example: "0.###E0" formats the number 1234 as
423 * "1.234E3".
424 *
425 * <ul>
426 * <li>The number of digit characters after the exponent character gives the
427 * minimum exponent digit count. There is no maximum. Negative exponents are
428 * formatted using the localized minus sign, <em>not</em> the prefix and suffix
429 * from the pattern. This allows patterns such as "0.###E0 m/s". To prefix
430 * positive exponents with a localized plus sign, specify '+' between the
431 * exponent and the digits: "0.###E+0" will produce formats "1E+1", "1E+0",
432 * "1E-1", etc. (In localized patterns, use the localized plus sign rather than
433 * '+'.)
434 *
435 * <li>The minimum number of integer digits is achieved by adjusting the
436 * exponent. Example: 0.00123 formatted with "00.###E0" yields "12.3E-4". This
437 * only happens if there is no maximum number of integer digits. If there is a
438 * maximum, then the minimum number of integer digits is fixed at one.
439 *
440 * <li>The maximum number of integer digits, if present, specifies the exponent
441 * grouping. The most common use of this is to generate <em>engineering
442 * notation</em>, in which the exponent is a multiple of three, e.g.,
443 * "##0.###E0". The number 12345 is formatted using "##0.####E0" as "12.345E3".
444 *
445 * <li>When using scientific notation, the formatter controls the
446 * digit counts using significant digits logic. The maximum number of
447 * significant digits limits the total number of integer and fraction
448 * digits that will be shown in the mantissa; it does not affect
449 * parsing. For example, 12345 formatted with "##0.##E0" is "12.3E3".
450 * See the section on significant digits for more details.
451 *
452 * <li>The number of significant digits shown is determined as
453 * follows: If areSignificantDigitsUsed() returns false, then the
454 * minimum number of significant digits shown is one, and the maximum
455 * number of significant digits shown is the sum of the <em>minimum
456 * integer</em> and <em>maximum fraction</em> digits, and is
457 * unaffected by the maximum integer digits. If this sum is zero,
458 * then all significant digits are shown. If
459 * areSignificantDigitsUsed() returns true, then the significant digit
460 * counts are specified by getMinimumSignificantDigits() and
461 * getMaximumSignificantDigits(). In this case, the number of
462 * integer digits is fixed at one, and there is no exponent grouping.
463 *
464 * <li>Exponential patterns may not contain grouping separators.
465 * </ul>
466 *
467 * <a name="sigdig"><strong>Significant Digits</strong></a>
468 *
469 * <code>DecimalFormat</code> has two ways of controlling how many
470 * digits are shows: (a) significant digits counts, or (b) integer and
471 * fraction digit counts. Integer and fraction digit counts are
472 * described above. When a formatter is using significant digits
473 * counts, the number of integer and fraction digits is not specified
474 * directly, and the formatter settings for these counts are ignored.
475 * Instead, the formatter uses however many integer and fraction
476 * digits are required to display the specified number of significant
477 * digits. Examples:
478 *
479 * <table border=0 cellspacing=3 cellpadding=0>
480 * <tr bgcolor="#ccccff">
481 * <td align=left>Pattern
482 * <td align=left>Minimum significant digits
483 * <td align=left>Maximum significant digits
484 * <td align=left>Number
485 * <td align=left>Output of format()
486 * <tr valign=top>
487 * <td><code>\@\@\@</code>
488 * <td>3
489 * <td>3
490 * <td>12345
491 * <td><code>12300</code>
492 * <tr valign=top bgcolor="#eeeeff">
493 * <td><code>\@\@\@</code>
494 * <td>3
495 * <td>3
496 * <td>0.12345
497 * <td><code>0.123</code>
498 * <tr valign=top>
499 * <td><code>\@\@##</code>
500 * <td>2
501 * <td>4
502 * <td>3.14159
503 * <td><code>3.142</code>
504 * <tr valign=top bgcolor="#eeeeff">
505 * <td><code>\@\@##</code>
506 * <td>2
507 * <td>4
508 * <td>1.23004
509 * <td><code>1.23</code>
510 * </table>
511 *
512 * <ul>
513 * <li>Significant digit counts may be expressed using patterns that
514 * specify a minimum and maximum number of significant digits. These
515 * are indicated by the <code>'@'</code> and <code>'#'</code>
516 * characters. The minimum number of significant digits is the number
517 * of <code>'@'</code> characters. The maximum number of significant
518 * digits is the number of <code>'@'</code> characters plus the number
519 * of <code>'#'</code> characters following on the right. For
520 * example, the pattern <code>"@@@"</code> indicates exactly 3
521 * significant digits. The pattern <code>"@##"</code> indicates from
522 * 1 to 3 significant digits. Trailing zero digits to the right of
523 * the decimal separator are suppressed after the minimum number of
524 * significant digits have been shown. For example, the pattern
525 * <code>"@##"</code> formats the number 0.1203 as
526 * <code>"0.12"</code>.
527 *
528 * <li>If a pattern uses significant digits, it may not contain a
529 * decimal separator, nor the <code>'0'</code> pattern character.
530 * Patterns such as <code>"@00"</code> or <code>"@.###"</code> are
531 * disallowed.
532 *
533 * <li>Any number of <code>'#'</code> characters may be prepended to
534 * the left of the leftmost <code>'@'</code> character. These have no
535 * effect on the minimum and maximum significant digits counts, but
536 * may be used to position grouping separators. For example,
537 * <code>"#,#@#"</code> indicates a minimum of one significant digits,
538 * a maximum of two significant digits, and a grouping size of three.
539 *
540 * <li>In order to enable significant digits formatting, use a pattern
541 * containing the <code>'@'</code> pattern character. Alternatively,
542 * call setSignificantDigitsUsed(TRUE).
543 *
544 * <li>In order to disable significant digits formatting, use a
545 * pattern that does not contain the <code>'@'</code> pattern
546 * character. Alternatively, call setSignificantDigitsUsed(FALSE).
547 *
548 * <li>The number of significant digits has no effect on parsing.
549 *
550 * <li>Significant digits may be used together with exponential notation. Such
551 * patterns are equivalent to a normal exponential pattern with a minimum and
552 * maximum integer digit count of one, a minimum fraction digit count of
553 * <code>getMinimumSignificantDigits() - 1</code>, and a maximum fraction digit
554 * count of <code>getMaximumSignificantDigits() - 1</code>. For example, the
555 * pattern <code>"@@###E0"</code> is equivalent to <code>"0.0###E0"</code>.
556 *
557 * <li>If signficant digits are in use, then the integer and fraction
558 * digit counts, as set via the API, are ignored. If significant
559 * digits are not in use, then the signficant digit counts, as set via
560 * the API, are ignored.
561 *
562 * </ul>
563 *
564 * <p><strong>Padding</strong>
565 *
566 * <p>DecimalFormat supports padding the result of
567 * format() to a specific width. Padding may be specified either
568 * through the API or through the pattern syntax. In a pattern the pad escape
569 * character, followed by a single pad character, causes padding to be parsed
570 * and formatted. The pad escape character is '*' in unlocalized patterns, and
571 * can be localized using DecimalFormatSymbols::setSymbol() with a
572 * DecimalFormatSymbols::kPadEscapeSymbol
573 * selector. For example, <code>"$*x#,##0.00"</code> formats 123 to
574 * <code>"$xx123.00"</code>, and 1234 to <code>"$1,234.00"</code>.
575 *
576 * <ul>
577 * <li>When padding is in effect, the width of the positive subpattern,
578 * including prefix and suffix, determines the format width. For example, in
579 * the pattern <code>"* #0 o''clock"</code>, the format width is 10.
580 *
581 * <li>The width is counted in 16-bit code units (UChars).
582 *
583 * <li>Some parameters which usually do not matter have meaning when padding is
584 * used, because the pattern width is significant with padding. In the pattern
585 * "* ##,##,#,##0.##", the format width is 14. The initial characters "##,##,"
586 * do not affect the grouping size or maximum integer digits, but they do affect
587 * the format width.
588 *
589 * <li>Padding may be inserted at one of four locations: before the prefix,
590 * after the prefix, before the suffix, or after the suffix. If padding is
591 * specified in any other location, applyPattern()
592 * sets a failing UErrorCode. If there is no prefix,
593 * before the prefix and after the prefix are equivalent, likewise for the
594 * suffix.
595 *
596 * <li>When specified in a pattern, the 32-bit code point immediately
597 * following the pad escape is the pad character. This may be any character,
598 * including a special pattern character. That is, the pad escape
599 * <em>escapes</em> the following character. If there is no character after
600 * the pad escape, then the pattern is illegal.
601 *
602 * </ul>
603 *
604 * <p><strong>Rounding</strong>
605 *
606 * <p>DecimalFormat supports rounding to a specific increment. For
607 * example, 1230 rounded to the nearest 50 is 1250. 1.234 rounded to the
608 * nearest 0.65 is 1.3. The rounding increment may be specified through the API
609 * or in a pattern. To specify a rounding increment in a pattern, include the
610 * increment in the pattern itself. "#,#50" specifies a rounding increment of
611 * 50. "#,##0.05" specifies a rounding increment of 0.05.
612 *
613 * <p>In the absense of an explicit rounding increment numbers are
614 * rounded to their formatted width.
615 *
616 * <ul>
617 * <li>Rounding only affects the string produced by formatting. It does
618 * not affect parsing or change any numerical values.
619 *
620 * <li>A <em>rounding mode</em> determines how values are rounded; see
621 * DecimalFormat::ERoundingMode. The default rounding mode is
622 * DecimalFormat::kRoundHalfEven. The rounding mode can only be set
623 * through the API; it can not be set with a pattern.
624 *
625 * <li>Some locales use rounding in their currency formats to reflect the
626 * smallest currency denomination.
627 *
628 * <li>In a pattern, digits '1' through '9' specify rounding, but otherwise
629 * behave identically to digit '0'.
630 * </ul>
631 *
632 * <p><strong>Synchronization</strong>
633 *
634 * <p>DecimalFormat objects are not synchronized. Multiple
635 * threads should not access one formatter concurrently.
636 *
637 * <p><strong>Subclassing</strong>
638 *
639 * <p><em>User subclasses are not supported.</em> While clients may write
640 * subclasses, such code will not necessarily work and will not be
641 * guaranteed to work stably from release to release.
642 */
643 class U_I18N_API DecimalFormat: public NumberFormat {
644 public:
645 /**
646 * Rounding mode.
647 * @stable ICU 2.4
648 */
649 enum ERoundingMode {
650 kRoundCeiling, /**< Round towards positive infinity */
651 kRoundFloor, /**< Round towards negative infinity */
652 kRoundDown, /**< Round towards zero */
653 kRoundUp, /**< Round away from zero */
654 kRoundHalfEven, /**< Round towards the nearest integer, or
655 towards the nearest even integer if equidistant */
656 kRoundHalfDown, /**< Round towards the nearest integer, or
657 towards zero if equidistant */
658 kRoundHalfUp, /**< Round towards the nearest integer, or
659 away from zero if equidistant */
660 /**
661 * Return U_FORMAT_INEXACT_ERROR if number does not format exactly.
662 * @stable ICU 4.8
663 */
664 kRoundUnnecessary
665 };
666
667 /**
668 * Pad position.
669 * @stable ICU 2.4
670 */
671 enum EPadPosition {
672 kPadBeforePrefix,
673 kPadAfterPrefix,
674 kPadBeforeSuffix,
675 kPadAfterSuffix
676 };
677
678 /**
679 * Create a DecimalFormat using the default pattern and symbols
680 * for the default locale. This is a convenient way to obtain a
681 * DecimalFormat when internationalization is not the main concern.
682 * <P>
683 * To obtain standard formats for a given locale, use the factory methods
684 * on NumberFormat such as createInstance. These factories will
685 * return the most appropriate sub-class of NumberFormat for a given
686 * locale.
687 * @param status Output param set to success/failure code. If the
688 * pattern is invalid this will be set to a failure code.
689 * @stable ICU 2.0
690 */
691 DecimalFormat(UErrorCode& status);
692
693 /**
694 * Create a DecimalFormat from the given pattern and the symbols
695 * for the default locale. This is a convenient way to obtain a
696 * DecimalFormat when internationalization is not the main concern.
697 * <P>
698 * To obtain standard formats for a given locale, use the factory methods
699 * on NumberFormat such as createInstance. These factories will
700 * return the most appropriate sub-class of NumberFormat for a given
701 * locale.
702 * @param pattern A non-localized pattern string.
703 * @param status Output param set to success/failure code. If the
704 * pattern is invalid this will be set to a failure code.
705 * @stable ICU 2.0
706 */
707 DecimalFormat(const UnicodeString& pattern,
708 UErrorCode& status);
709
710 /**
711 * Create a DecimalFormat from the given pattern and symbols.
712 * Use this constructor when you need to completely customize the
713 * behavior of the format.
714 * <P>
715 * To obtain standard formats for a given
716 * locale, use the factory methods on NumberFormat such as
717 * createInstance or createCurrencyInstance. If you need only minor adjustments
718 * to a standard format, you can modify the format returned by
719 * a NumberFormat factory method.
720 *
721 * @param pattern a non-localized pattern string
722 * @param symbolsToAdopt the set of symbols to be used. The caller should not
723 * delete this object after making this call.
724 * @param status Output param set to success/failure code. If the
725 * pattern is invalid this will be set to a failure code.
726 * @stable ICU 2.0
727 */
728 DecimalFormat( const UnicodeString& pattern,
729 DecimalFormatSymbols* symbolsToAdopt,
730 UErrorCode& status);
731
732 #ifndef U_HIDE_INTERNAL_API
733 /**
734 * This API is for ICU use only.
735 * Create a DecimalFormat from the given pattern, symbols, and style.
736 *
737 * @param pattern a non-localized pattern string
738 * @param symbolsToAdopt the set of symbols to be used. The caller should not
739 * delete this object after making this call.
740 * @param style style of decimal format
741 * @param status Output param set to success/failure code. If the
742 * pattern is invalid this will be set to a failure code.
743 * @internal ICU 4.2
744 */
745 DecimalFormat( const UnicodeString& pattern,
746 DecimalFormatSymbols* symbolsToAdopt,
747 UNumberFormatStyle style,
748 UErrorCode& status);
749 #endif /* U_HIDE_INTERNAL_API */
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 /* Cannot use #ifndef U_HIDE_DRAFT_API for the following draft method since it is virtual */
1110 /**
1111 * Parses text from the given string as a currency amount. Unlike
1112 * the parse() method, this method will attempt to parse a generic
1113 * currency name, searching for a match of this object's locale's
1114 * currency display names, or for a 3-letter ISO currency code.
1115 * This method will fail if this format is not a currency format,
1116 * that is, if it does not contain the currency pattern symbol
1117 * (U+00A4) in its prefix or suffix.
1118 *
1119 * @param text the string to parse
1120 * @param pos input-output position; on input, the position within text
1121 * to match; must have 0 <= pos.getIndex() < text.length();
1122 * on output, the position after the last matched character.
1123 * If the parse fails, the position in unchanged upon output.
1124 * @return if parse succeeds, a pointer to a newly-created CurrencyAmount
1125 * object (owned by the caller) containing information about
1126 * the parsed currency; if parse fails, this is NULL.
1127 * @draft ICU 49
1128 */
1129 virtual CurrencyAmount* parseCurrency(const UnicodeString& text,
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 UChar* currency) 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&
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&
2307 DecimalFormat::format(double number,
2308 UnicodeString& appendTo) const {
2309 FieldPosition pos(0);
2310 return format(number, appendTo, pos);
2311 }
2312
2313 inline UnicodeString&
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 #ifndef U_HIDE_INTERNAL_API
2321 inline const UnicodeString &
2322 DecimalFormat::getConstSymbol(DecimalFormatSymbols::ENumberFormatSymbol symbol) const {
2323 return fSymbols->getConstSymbol(symbol);
2324 }
2325 #endif
2326
2327 U_NAMESPACE_END
2328
2329 #endif /* #if !UCONFIG_NO_FORMATTING */
2330
2331 #endif // _DECIMFMT
2332 //eof