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