2 ********************************************************************************
3 * Copyright (C) 1997-2009, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ********************************************************************************
9 * Modification History:
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
19 * 09/09/97 aliu Ported over support for exponential formats.
20 * 07/20/98 stephen Changed documentation
21 ********************************************************************************
27 #include "unicode/utypes.h"
30 * \brief C++ API: Formats decimal numbers.
33 #if !UCONFIG_NO_FORMATTING
35 #include "unicode/dcfmtsym.h"
36 #include "unicode/numfmt.h"
37 #include "unicode/locid.h"
46 * DecimalFormat is a concrete subclass of NumberFormat that formats decimal
47 * numbers. It has a variety of features designed to make it possible to parse
48 * and format numbers in any locale, including support for Western, Arabic, or
49 * Indic digits. It also supports different flavors of numbers, including
50 * integers ("123"), fixed-point numbers ("123.4"), scientific notation
51 * ("1.23E4"), percentages ("12%"), and currency amounts ("$123"). All of these
52 * flavors can be easily localized.
54 * <p>To obtain a NumberFormat for a specific locale (including the default
55 * locale) call one of NumberFormat's factory methods such as
56 * createInstance(). Do not call the DecimalFormat constructors directly, unless
57 * you know what you are doing, since the NumberFormat factory methods may
58 * return subclasses other than DecimalFormat.
60 * <p><strong>Example Usage</strong>
63 * // Normally we would have a GUI with a menu for this
65 * const Locale* locales = NumberFormat::getAvailableLocales(locCount);
67 * double myNumber = -1234.56;
68 * UErrorCode success = U_ZERO_ERROR;
71 * // Print out a number with the localized number, currency and percent
72 * // format for each locale.
73 * UnicodeString countryName;
74 * UnicodeString displayName;
76 * UnicodeString pattern;
77 * Formattable fmtable;
78 * for (int32_t j = 0; j < 3; ++j) {
79 * cout << endl << "FORMAT " << j << endl;
80 * for (int32_t i = 0; i < locCount; ++i) {
81 * if (locales[i].getCountry(countryName).size() == 0) {
82 * // skip language-only
87 * form = NumberFormat::createInstance(locales[i], success ); break;
89 * form = NumberFormat::createCurrencyInstance(locales[i], success ); break;
91 * form = NumberFormat::createPercentInstance(locales[i], success ); break;
95 * pattern = ((DecimalFormat*)form)->toPattern(pattern);
96 * cout << locales[i].getDisplayName(displayName) << ": " << pattern;
97 * cout << " -> " << form->format(myNumber,str) << endl;
98 * form->parse(form->format(myNumber,str), fmtable, success);
105 * <p><strong>Patterns</strong>
107 * <p>A DecimalFormat consists of a <em>pattern</em> and a set of
108 * <em>symbols</em>. The pattern may be set directly using
109 * applyPattern(), or indirectly using other API methods which
110 * manipulate aspects of the pattern, such as the minimum number of integer
111 * digits. The symbols are stored in a DecimalFormatSymbols
112 * object. When using the NumberFormat factory methods, the
113 * pattern and symbols are read from ICU's locale data.
115 * <p><strong>Special Pattern Characters</strong>
117 * <p>Many characters in a pattern are taken literally; they are matched during
118 * parsing and output unchanged during formatting. Special characters, on the
119 * other hand, stand for other characters, strings, or classes of characters.
120 * For example, the '#' character is replaced by a localized digit. Often the
121 * replacement character is the same as the pattern character; in the U.S. locale,
122 * the ',' grouping character is replaced by ','. However, the replacement is
123 * still happening, and if the symbols are modified, the grouping character
124 * changes. Some special characters affect the behavior of the formatter by
125 * their presence; for example, if the percent character is seen, then the
126 * value is multiplied by 100 before being displayed.
128 * <p>To insert a special character in a pattern as a literal, that is, without
129 * any special meaning, the character must be quoted. There are some exceptions to
130 * this which are noted below.
132 * <p>The characters listed here are used in non-localized patterns. Localized
133 * patterns use the corresponding characters taken from this formatter's
134 * DecimalFormatSymbols object instead, and these characters lose
135 * their special status. Two exceptions are the currency sign and quote, which
138 * <table border=0 cellspacing=3 cellpadding=0>
139 * <tr bgcolor="#ccccff">
140 * <td align=left><strong>Symbol</strong>
141 * <td align=left><strong>Location</strong>
142 * <td align=left><strong>Localized?</strong>
143 * <td align=left><strong>Meaning</strong>
149 * <tr valign=top bgcolor="#eeeeff">
150 * <td><code>1-9</code>
153 * <td>'1' through '9' indicate rounding.
155 * <td><code>\htmlonly@\endhtmlonly</code> <!--doxygen doesn't like @-->
158 * <td>Significant digit
159 * <tr valign=top bgcolor="#eeeeff">
163 * <td>Digit, zero shows as absent
168 * <td>Decimal separator or monetary decimal separator
169 * <tr valign=top bgcolor="#eeeeff">
178 * <td>Grouping separator
179 * <tr valign=top bgcolor="#eeeeff">
183 * <td>Separates mantissa and exponent in scientific notation.
184 * <em>Need not be quoted in prefix or suffix.</em>
189 * <td>Prefix positive exponents with localized plus sign.
190 * <em>Need not be quoted in prefix or suffix.</em>
191 * <tr valign=top bgcolor="#eeeeff">
193 * <td>Subpattern boundary
195 * <td>Separates positive and negative subpatterns
197 * <td><code>\%</code>
198 * <td>Prefix or suffix
200 * <td>Multiply by 100 and show as percentage
201 * <tr valign=top bgcolor="#eeeeff">
202 * <td><code>\\u2030</code>
203 * <td>Prefix or suffix
205 * <td>Multiply by 1000 and show as per mille
207 * <td><code>\htmlonly¤\endhtmlonly</code> (<code>\\u00A4</code>)
208 * <td>Prefix or suffix
210 * <td>Currency sign, replaced by currency symbol. If
211 * doubled, replaced by international currency symbol.
212 * If present in a pattern, the monetary decimal separator
213 * is used instead of the decimal separator.
214 * <tr valign=top bgcolor="#eeeeff">
216 * <td>Prefix or suffix
218 * <td>Used to quote special characters in a prefix or suffix,
219 * for example, <code>"'#'#"</code> formats 123 to
220 * <code>"#123"</code>. To create a single quote
221 * itself, use two in a row: <code>"# o''clock"</code>.
224 * <td>Prefix or suffix boundary
226 * <td>Pad escape, precedes pad character
229 * <p>A DecimalFormat pattern contains a postive and negative
230 * subpattern, for example, "#,##0.00;(#,##0.00)". Each subpattern has a
231 * prefix, a numeric part, and a suffix. If there is no explicit negative
232 * subpattern, the negative subpattern is the localized minus sign prefixed to the
233 * positive subpattern. That is, "0.00" alone is equivalent to "0.00;-0.00". If there
234 * is an explicit negative subpattern, it serves only to specify the negative
235 * prefix and suffix; the number of digits, minimal digits, and other
236 * characteristics are ignored in the negative subpattern. That means that
237 * "#,##0.0#;(#)" has precisely the same result as "#,##0.0#;(#,##0.0#)".
239 * <p>The prefixes, suffixes, and various symbols used for infinity, digits,
240 * thousands separators, decimal separators, etc. may be set to arbitrary
241 * values, and they will appear properly during formatting. However, care must
242 * be taken that the symbols and strings do not conflict, or parsing will be
243 * unreliable. For example, either the positive and negative prefixes or the
244 * suffixes must be distinct for parse() to be able
245 * to distinguish positive from negative values. Another example is that the
246 * decimal separator and thousands separator should be distinct characters, or
247 * parsing will be impossible.
249 * <p>The <em>grouping separator</em> is a character that separates clusters of
250 * integer digits to make large numbers more legible. It commonly used for
251 * thousands, but in some locales it separates ten-thousands. The <em>grouping
252 * size</em> is the number of digits between the grouping separators, such as 3
253 * for "100,000,000" or 4 for "1 0000 0000". There are actually two different
254 * grouping sizes: One used for the least significant integer digits, the
255 * <em>primary grouping size</em>, and one used for all others, the
256 * <em>secondary grouping size</em>. In most locales these are the same, but
257 * sometimes they are different. For example, if the primary grouping interval
258 * is 3, and the secondary is 2, then this corresponds to the pattern
259 * "#,##,##0", and the number 123456789 is formatted as "12,34,56,789". If a
260 * pattern contains multiple grouping separators, the interval between the last
261 * one and the end of the integer defines the primary grouping size, and the
262 * interval between the last two defines the secondary grouping size. All others
263 * are ignored, so "#,##,###,####" == "###,###,####" == "##,#,###,####".
265 * <p>Illegal patterns, such as "#.#.#" or "#.###,###", will cause
266 * DecimalFormat to set a failing UErrorCode.
268 * <p><strong>Pattern BNF</strong>
271 * pattern := subpattern (';' subpattern)?
272 * subpattern := prefix? number exponent? suffix?
273 * number := (integer ('.' fraction)?) | sigDigits
274 * prefix := '\\u0000'..'\\uFFFD' - specialCharacters
275 * suffix := '\\u0000'..'\\uFFFD' - specialCharacters
276 * integer := '#'* '0'* '0'
277 * fraction := '0'* '#'*
278 * sigDigits := '#'* '@' '@'* '#'*
279 * exponent := 'E' '+'? '0'* '0'
280 * padSpec := '*' padChar
281 * padChar := '\\u0000'..'\\uFFFD' - quote
284 * X* 0 or more instances of X
285 * X? 0 or 1 instances of X
287 * C..D any character from C up to D, inclusive
288 * S-T characters in S, except those in T
290 * The first subpattern is for positive numbers. The second (optional)
291 * subpattern is for negative numbers.
293 * <p>Not indicated in the BNF syntax above:
295 * <ul><li>The grouping separator ',' can occur inside the integer and
296 * sigDigits elements, between any two pattern characters of that
297 * element, as long as the integer or sigDigits element is not
298 * followed by the exponent element.
300 * <li>Two grouping intervals are recognized: That between the
301 * decimal point and the first grouping symbol, and that
302 * between the first and second grouping symbols. These
303 * intervals are identical in most locales, but in some
304 * locales they differ. For example, the pattern
305 * "#,##,###" formats the number 123456789 as
306 * "12,34,56,789".</li>
308 * <li>The pad specifier <code>padSpec</code> may appear before the prefix,
309 * after the prefix, before the suffix, after the suffix, or not at all.
311 * <li>In place of '0', the digits '1' through '9' may be used to
312 * indicate a rounding increment.
315 * <p><strong>Parsing</strong>
317 * <p>DecimalFormat parses all Unicode characters that represent
318 * decimal digits, as defined by u_charDigitValue(). In addition,
319 * DecimalFormat also recognizes as digits the ten consecutive
320 * characters starting with the localized zero digit defined in the
321 * DecimalFormatSymbols object. During formatting, the
322 * DecimalFormatSymbols-based digits are output.
324 * <p>During parsing, grouping separators are ignored.
326 * <p>If parse(UnicodeString&,Formattable&,ParsePosition&)
327 * fails to parse a string, it leaves the parse position unchanged.
328 * The convenience method parse(UnicodeString&,Formattable&,UErrorCode&)
329 * indicates parse failure by setting a failing
332 * <p><strong>Formatting</strong>
334 * <p>Formatting is guided by several parameters, all of which can be
335 * specified either using a pattern or using the API. The following
336 * description applies to formats that do not use <a href="#sci">scientific
337 * notation</a> or <a href="#sigdig">significant digits</a>.
339 * <ul><li>If the number of actual integer digits exceeds the
340 * <em>maximum integer digits</em>, then only the least significant
341 * digits are shown. For example, 1997 is formatted as "97" if the
342 * maximum integer digits is set to 2.
344 * <li>If the number of actual integer digits is less than the
345 * <em>minimum integer digits</em>, then leading zeros are added. For
346 * example, 1997 is formatted as "01997" if the minimum integer digits
349 * <li>If the number of actual fraction digits exceeds the <em>maximum
350 * fraction digits</em>, then half-even rounding it performed to the
351 * maximum fraction digits. For example, 0.125 is formatted as "0.12"
352 * if the maximum fraction digits is 2. This behavior can be changed
353 * by specifying a rounding increment and a rounding mode.
355 * <li>If the number of actual fraction digits is less than the
356 * <em>minimum fraction digits</em>, then trailing zeros are added.
357 * For example, 0.125 is formatted as "0.1250" if the mimimum fraction
358 * digits is set to 4.
360 * <li>Trailing fractional zeros are not displayed if they occur
361 * <em>j</em> positions after the decimal, where <em>j</em> is less
362 * than the maximum fraction digits. For example, 0.10004 is
363 * formatted as "0.1" if the maximum fraction digits is four or less.
366 * <p><strong>Special Values</strong>
368 * <p><code>NaN</code> is represented as a single character, typically
369 * <code>\\uFFFD</code>. This character is determined by the
370 * DecimalFormatSymbols object. This is the only value for which
371 * the prefixes and suffixes are not used.
373 * <p>Infinity is represented as a single character, typically
374 * <code>\\u221E</code>, with the positive or negative prefixes and suffixes
375 * applied. The infinity character is determined by the
376 * DecimalFormatSymbols object.
378 * <a name="sci"><strong>Scientific Notation</strong></a>
380 * <p>Numbers in scientific notation are expressed as the product of a mantissa
381 * and a power of ten, for example, 1234 can be expressed as 1.234 x 10<sup>3</sup>. The
382 * mantissa is typically in the half-open interval [1.0, 10.0) or sometimes [0.0, 1.0),
383 * but it need not be. DecimalFormat supports arbitrary mantissas.
384 * DecimalFormat can be instructed to use scientific
385 * notation through the API or through the pattern. In a pattern, the exponent
386 * character immediately followed by one or more digit characters indicates
387 * scientific notation. Example: "0.###E0" formats the number 1234 as
391 * <li>The number of digit characters after the exponent character gives the
392 * minimum exponent digit count. There is no maximum. Negative exponents are
393 * formatted using the localized minus sign, <em>not</em> the prefix and suffix
394 * from the pattern. This allows patterns such as "0.###E0 m/s". To prefix
395 * positive exponents with a localized plus sign, specify '+' between the
396 * exponent and the digits: "0.###E+0" will produce formats "1E+1", "1E+0",
397 * "1E-1", etc. (In localized patterns, use the localized plus sign rather than
400 * <li>The minimum number of integer digits is achieved by adjusting the
401 * exponent. Example: 0.00123 formatted with "00.###E0" yields "12.3E-4". This
402 * only happens if there is no maximum number of integer digits. If there is a
403 * maximum, then the minimum number of integer digits is fixed at one.
405 * <li>The maximum number of integer digits, if present, specifies the exponent
406 * grouping. The most common use of this is to generate <em>engineering
407 * notation</em>, in which the exponent is a multiple of three, e.g.,
408 * "##0.###E0". The number 12345 is formatted using "##0.####E0" as "12.345E3".
410 * <li>When using scientific notation, the formatter controls the
411 * digit counts using significant digits logic. The maximum number of
412 * significant digits limits the total number of integer and fraction
413 * digits that will be shown in the mantissa; it does not affect
414 * parsing. For example, 12345 formatted with "##0.##E0" is "12.3E3".
415 * See the section on significant digits for more details.
417 * <li>The number of significant digits shown is determined as
418 * follows: If areSignificantDigitsUsed() returns false, then the
419 * minimum number of significant digits shown is one, and the maximum
420 * number of significant digits shown is the sum of the <em>minimum
421 * integer</em> and <em>maximum fraction</em> digits, and is
422 * unaffected by the maximum integer digits. If this sum is zero,
423 * then all significant digits are shown. If
424 * areSignificantDigitsUsed() returns true, then the significant digit
425 * counts are specified by getMinimumSignificantDigits() and
426 * getMaximumSignificantDigits(). In this case, the number of
427 * integer digits is fixed at one, and there is no exponent grouping.
429 * <li>Exponential patterns may not contain grouping separators.
432 * <a name="sigdig"><strong>Significant Digits</strong></a>
434 * <code>DecimalFormat</code> has two ways of controlling how many
435 * digits are shows: (a) significant digits counts, or (b) integer and
436 * fraction digit counts. Integer and fraction digit counts are
437 * described above. When a formatter is using significant digits
438 * counts, the number of integer and fraction digits is not specified
439 * directly, and the formatter settings for these counts are ignored.
440 * Instead, the formatter uses however many integer and fraction
441 * digits are required to display the specified number of significant
444 * <table border=0 cellspacing=3 cellpadding=0>
445 * <tr bgcolor="#ccccff">
446 * <td align=left>Pattern
447 * <td align=left>Minimum significant digits
448 * <td align=left>Maximum significant digits
449 * <td align=left>Number
450 * <td align=left>Output of format()
452 * <td><code>\@\@\@</code>
456 * <td><code>12300</code>
457 * <tr valign=top bgcolor="#eeeeff">
458 * <td><code>\@\@\@</code>
462 * <td><code>0.123</code>
464 * <td><code>\@\@##</code>
468 * <td><code>3.142</code>
469 * <tr valign=top bgcolor="#eeeeff">
470 * <td><code>\@\@##</code>
474 * <td><code>1.23</code>
478 * <li>Significant digit counts may be expressed using patterns that
479 * specify a minimum and maximum number of significant digits. These
480 * are indicated by the <code>'@'</code> and <code>'#'</code>
481 * characters. The minimum number of significant digits is the number
482 * of <code>'@'</code> characters. The maximum number of significant
483 * digits is the number of <code>'@'</code> characters plus the number
484 * of <code>'#'</code> characters following on the right. For
485 * example, the pattern <code>"@@@"</code> indicates exactly 3
486 * significant digits. The pattern <code>"@##"</code> indicates from
487 * 1 to 3 significant digits. Trailing zero digits to the right of
488 * the decimal separator are suppressed after the minimum number of
489 * significant digits have been shown. For example, the pattern
490 * <code>"@##"</code> formats the number 0.1203 as
491 * <code>"0.12"</code>.
493 * <li>If a pattern uses significant digits, it may not contain a
494 * decimal separator, nor the <code>'0'</code> pattern character.
495 * Patterns such as <code>"@00"</code> or <code>"@.###"</code> are
498 * <li>Any number of <code>'#'</code> characters may be prepended to
499 * the left of the leftmost <code>'@'</code> character. These have no
500 * effect on the minimum and maximum significant digits counts, but
501 * may be used to position grouping separators. For example,
502 * <code>"#,#@#"</code> indicates a minimum of one significant digits,
503 * a maximum of two significant digits, and a grouping size of three.
505 * <li>In order to enable significant digits formatting, use a pattern
506 * containing the <code>'@'</code> pattern character. Alternatively,
507 * call setSignificantDigitsUsed(TRUE).
509 * <li>In order to disable significant digits formatting, use a
510 * pattern that does not contain the <code>'@'</code> pattern
511 * character. Alternatively, call setSignificantDigitsUsed(FALSE).
513 * <li>The number of significant digits has no effect on parsing.
515 * <li>Significant digits may be used together with exponential notation. Such
516 * patterns are equivalent to a normal exponential pattern with a minimum and
517 * maximum integer digit count of one, a minimum fraction digit count of
518 * <code>getMinimumSignificantDigits() - 1</code>, and a maximum fraction digit
519 * count of <code>getMaximumSignificantDigits() - 1</code>. For example, the
520 * pattern <code>"@@###E0"</code> is equivalent to <code>"0.0###E0"</code>.
522 * <li>If signficant digits are in use, then the integer and fraction
523 * digit counts, as set via the API, are ignored. If significant
524 * digits are not in use, then the signficant digit counts, as set via
525 * the API, are ignored.
529 * <p><strong>Padding</strong>
531 * <p>DecimalFormat supports padding the result of
532 * format() to a specific width. Padding may be specified either
533 * through the API or through the pattern syntax. In a pattern the pad escape
534 * character, followed by a single pad character, causes padding to be parsed
535 * and formatted. The pad escape character is '*' in unlocalized patterns, and
536 * can be localized using DecimalFormatSymbols::setSymbol() with a
537 * DecimalFormatSymbols::kPadEscapeSymbol
538 * selector. For example, <code>"$*x#,##0.00"</code> formats 123 to
539 * <code>"$xx123.00"</code>, and 1234 to <code>"$1,234.00"</code>.
542 * <li>When padding is in effect, the width of the positive subpattern,
543 * including prefix and suffix, determines the format width. For example, in
544 * the pattern <code>"* #0 o''clock"</code>, the format width is 10.
546 * <li>The width is counted in 16-bit code units (UChars).
548 * <li>Some parameters which usually do not matter have meaning when padding is
549 * used, because the pattern width is significant with padding. In the pattern
550 * "* ##,##,#,##0.##", the format width is 14. The initial characters "##,##,"
551 * do not affect the grouping size or maximum integer digits, but they do affect
554 * <li>Padding may be inserted at one of four locations: before the prefix,
555 * after the prefix, before the suffix, or after the suffix. If padding is
556 * specified in any other location, applyPattern()
557 * sets a failing UErrorCode. If there is no prefix,
558 * before the prefix and after the prefix are equivalent, likewise for the
561 * <li>When specified in a pattern, the 32-bit code point immediately
562 * following the pad escape is the pad character. This may be any character,
563 * including a special pattern character. That is, the pad escape
564 * <em>escapes</em> the following character. If there is no character after
565 * the pad escape, then the pattern is illegal.
569 * <p><strong>Rounding</strong>
571 * <p>DecimalFormat supports rounding to a specific increment. For
572 * example, 1230 rounded to the nearest 50 is 1250. 1.234 rounded to the
573 * nearest 0.65 is 1.3. The rounding increment may be specified through the API
574 * or in a pattern. To specify a rounding increment in a pattern, include the
575 * increment in the pattern itself. "#,#50" specifies a rounding increment of
576 * 50. "#,##0.05" specifies a rounding increment of 0.05.
579 * <li>Rounding only affects the string produced by formatting. It does
580 * not affect parsing or change any numerical values.
582 * <li>A <em>rounding mode</em> determines how values are rounded; see
583 * DecimalFormat::ERoundingMode. Rounding increments specified in
584 * patterns use the default mode, DecimalFormat::kRoundHalfEven.
586 * <li>Some locales use rounding in their currency formats to reflect the
587 * smallest currency denomination.
589 * <li>In a pattern, digits '1' through '9' specify rounding, but otherwise
590 * behave identically to digit '0'.
593 * <p><strong>Synchronization</strong>
595 * <p>DecimalFormat objects are not synchronized. Multiple
596 * threads should not access one formatter concurrently.
598 * <p><strong>Subclassing</strong>
600 * <p><em>User subclasses are not supported.</em> While clients may write
601 * subclasses, such code will not necessarily work and will not be
602 * guaranteed to work stably from release to release.
604 class U_I18N_API DecimalFormat
: public NumberFormat
{
611 kRoundCeiling
, /**< Round towards positive infinity */
612 kRoundFloor
, /**< Round towards negative infinity */
613 kRoundDown
, /**< Round towards zero */
614 kRoundUp
, /**< Round away from zero */
615 kRoundHalfEven
, /**< Round towards the nearest integer, or
616 towards the nearest even integer if equidistant */
617 kRoundHalfDown
, /**< Round towards the nearest integer, or
618 towards zero if equidistant */
619 kRoundHalfUp
/**< Round towards the nearest integer, or
620 away from zero if equidistant */
621 // We don't support ROUND_UNNECESSARY
636 * Create a DecimalFormat using the default pattern and symbols
637 * for the default locale. This is a convenient way to obtain a
638 * DecimalFormat when internationalization is not the main concern.
640 * To obtain standard formats for a given locale, use the factory methods
641 * on NumberFormat such as createInstance. These factories will
642 * return the most appropriate sub-class of NumberFormat for a given
644 * @param status Output param set to success/failure code. If the
645 * pattern is invalid this will be set to a failure code.
648 DecimalFormat(UErrorCode
& status
);
651 * Create a DecimalFormat from the given pattern and the symbols
652 * for the default locale. This is a convenient way to obtain a
653 * DecimalFormat when internationalization is not the main concern.
655 * To obtain standard formats for a given locale, use the factory methods
656 * on NumberFormat such as createInstance. These factories will
657 * return the most appropriate sub-class of NumberFormat for a given
659 * @param pattern A non-localized pattern string.
660 * @param status Output param set to success/failure code. If the
661 * pattern is invalid this will be set to a failure code.
664 DecimalFormat(const UnicodeString
& pattern
,
668 * Create a DecimalFormat from the given pattern and symbols.
669 * Use this constructor when you need to completely customize the
670 * behavior of the format.
672 * To obtain standard formats for a given
673 * locale, use the factory methods on NumberFormat such as
674 * createInstance or createCurrencyInstance. If you need only minor adjustments
675 * to a standard format, you can modify the format returned by
676 * a NumberFormat factory method.
678 * @param pattern a non-localized pattern string
679 * @param symbolsToAdopt the set of symbols to be used. The caller should not
680 * delete this object after making this call.
681 * @param status Output param set to success/failure code. If the
682 * pattern is invalid this will be set to a failure code.
685 DecimalFormat( const UnicodeString
& pattern
,
686 DecimalFormatSymbols
* symbolsToAdopt
,
690 * Create a DecimalFormat from the given pattern and symbols.
691 * Use this constructor when you need to completely customize the
692 * behavior of the format.
694 * To obtain standard formats for a given
695 * locale, use the factory methods on NumberFormat such as
696 * createInstance or createCurrencyInstance. If you need only minor adjustments
697 * to a standard format, you can modify the format returned by
698 * a NumberFormat factory method.
700 * @param pattern a non-localized pattern string
701 * @param symbolsToAdopt the set of symbols to be used. The caller should not
702 * delete this object after making this call.
703 * @param parseError Output param to receive errors occured during parsing
704 * @param status Output param set to success/failure code. If the
705 * pattern is invalid this will be set to a failure code.
708 DecimalFormat( const UnicodeString
& pattern
,
709 DecimalFormatSymbols
* symbolsToAdopt
,
710 UParseError
& parseError
,
713 * Create a DecimalFormat from the given pattern and symbols.
714 * Use this constructor when you need to completely customize the
715 * behavior of the format.
717 * To obtain standard formats for a given
718 * locale, use the factory methods on NumberFormat such as
719 * createInstance or createCurrencyInstance. If you need only minor adjustments
720 * to a standard format, you can modify the format returned by
721 * a NumberFormat factory method.
723 * @param pattern a non-localized pattern string
724 * @param symbols the set of symbols to be used
725 * @param status Output param set to success/failure code. If the
726 * pattern is invalid this will be set to a failure code.
729 DecimalFormat( const UnicodeString
& pattern
,
730 const DecimalFormatSymbols
& symbols
,
736 * @param source the DecimalFormat object to be copied from.
739 DecimalFormat(const DecimalFormat
& source
);
742 * Assignment operator.
744 * @param rhs the DecimalFormat object to be copied.
747 DecimalFormat
& operator=(const DecimalFormat
& rhs
);
753 virtual ~DecimalFormat();
756 * Clone this Format object polymorphically. The caller owns the
757 * result and should delete it when done.
759 * @return a polymorphic copy of this DecimalFormat.
762 virtual Format
* clone(void) const;
765 * Return true if the given Format objects are semantically equal.
766 * Objects of different subclasses are considered unequal.
768 * @param other the object to be compared with.
769 * @return true if the given Format objects are semantically equal.
772 virtual UBool
operator==(const Format
& other
) const;
775 * Format a double or long number using base-10 representation.
777 * @param number The value to be formatted.
778 * @param appendTo Output parameter to receive result.
779 * Result is appended to existing contents.
780 * @param pos On input: an alignment field, if desired.
781 * On output: the offsets of the alignment field.
782 * @return Reference to 'appendTo' parameter.
785 virtual UnicodeString
& format(double number
,
786 UnicodeString
& appendTo
,
787 FieldPosition
& pos
) const;
789 * Format a long number using base-10 representation.
791 * @param number The value to be formatted.
792 * @param appendTo Output parameter to receive result.
793 * Result is appended to existing contents.
794 * @param pos On input: an alignment field, if desired.
795 * On output: the offsets of the alignment field.
796 * @return Reference to 'appendTo' parameter.
799 virtual UnicodeString
& format(int32_t number
,
800 UnicodeString
& appendTo
,
801 FieldPosition
& pos
) const;
803 * Format an int64 number using base-10 representation.
805 * @param number The value to be formatted.
806 * @param appendTo Output parameter to receive result.
807 * Result is appended to existing contents.
808 * @param pos On input: an alignment field, if desired.
809 * On output: the offsets of the alignment field.
810 * @return Reference to 'appendTo' parameter.
813 virtual UnicodeString
& format(int64_t number
,
814 UnicodeString
& appendTo
,
815 FieldPosition
& pos
) const;
818 * Format a Formattable using base-10 representation.
820 * @param obj The value to be formatted.
821 * @param appendTo Output parameter to receive result.
822 * Result is appended to existing contents.
823 * @param pos On input: an alignment field, if desired.
824 * On output: the offsets of the alignment field.
825 * @param status Error code indicating success or failure.
826 * @return Reference to 'appendTo' parameter.
829 virtual UnicodeString
& format(const Formattable
& obj
,
830 UnicodeString
& appendTo
,
832 UErrorCode
& status
) const;
835 * Redeclared NumberFormat method.
836 * Formats an object to produce a string.
838 * @param obj The object to format.
839 * @param appendTo Output parameter to receive result.
840 * Result is appended to existing contents.
841 * @param status Output parameter filled in with success or failure status.
842 * @return Reference to 'appendTo' parameter.
845 UnicodeString
& format(const Formattable
& obj
,
846 UnicodeString
& appendTo
,
847 UErrorCode
& status
) const;
850 * Redeclared NumberFormat method.
851 * Format a double number.
853 * @param number The value to be formatted.
854 * @param appendTo Output parameter to receive result.
855 * Result is appended to existing contents.
856 * @return Reference to 'appendTo' parameter.
859 UnicodeString
& format(double number
,
860 UnicodeString
& appendTo
) const;
863 * Redeclared NumberFormat method.
864 * Format a long number. These methods call the NumberFormat
865 * pure virtual format() methods with the default FieldPosition.
867 * @param number The value to be formatted.
868 * @param appendTo Output parameter to receive result.
869 * Result is appended to existing contents.
870 * @return Reference to 'appendTo' parameter.
873 UnicodeString
& format(int32_t number
,
874 UnicodeString
& appendTo
) const;
877 * Redeclared NumberFormat method.
878 * Format an int64 number. These methods call the NumberFormat
879 * pure virtual format() methods with the default FieldPosition.
881 * @param number The value to be formatted.
882 * @param appendTo Output parameter to receive result.
883 * Result is appended to existing contents.
884 * @return Reference to 'appendTo' parameter.
887 UnicodeString
& format(int64_t number
,
888 UnicodeString
& appendTo
) const;
890 * Parse the given string using this object's choices. The method
891 * does string comparisons to try to find an optimal match.
892 * If no object can be parsed, index is unchanged, and NULL is
893 * returned. The result is returned as the most parsimonious
894 * type of Formattable that will accomodate all of the
895 * necessary precision. For example, if the result is exactly 12,
896 * it will be returned as a long. However, if it is 1.5, it will
897 * be returned as a double.
899 * @param text The text to be parsed.
900 * @param result Formattable to be set to the parse result.
901 * If parse fails, return contents are undefined.
902 * @param parsePosition The position to start parsing at on input.
903 * On output, moved to after the last successfully
904 * parse character. On parse failure, does not change.
908 virtual void parse(const UnicodeString
& text
,
910 ParsePosition
& parsePosition
) const;
912 // Declare here again to get rid of function hiding problems.
914 * Parse the given string using this object's choices.
916 * @param text The text to be parsed.
917 * @param result Formattable to be set to the parse result.
918 * @param status Output parameter filled in with success or failure status.
921 virtual void parse(const UnicodeString
& text
,
923 UErrorCode
& status
) const;
926 * Parses text from the given string as a currency amount. Unlike
927 * the parse() method, this method will attempt to parse a generic
928 * currency name, searching for a match of this object's locale's
929 * currency display names, or for a 3-letter ISO currency code.
930 * This method will fail if this format is not a currency format,
931 * that is, if it does not contain the currency pattern symbol
932 * (U+00A4) in its prefix or suffix.
934 * @param text the string to parse
935 * @param result output parameter to receive result. This will have
936 * its currency set to the parsed ISO currency code.
937 * @param pos input-output position; on input, the position within
938 * text to match; must have 0 <= pos.getIndex() < text.length();
939 * on output, the position after the last matched character. If
940 * the parse fails, the position in unchanged upon output.
941 * @return a reference to result
944 virtual Formattable
& parseCurrency(const UnicodeString
& text
,
946 ParsePosition
& pos
) const;
949 * Returns the decimal format symbols, which is generally not changed
950 * by the programmer or user.
951 * @return desired DecimalFormatSymbols
952 * @see DecimalFormatSymbols
955 virtual const DecimalFormatSymbols
* getDecimalFormatSymbols(void) const;
958 * Sets the decimal format symbols, which is generally not changed
959 * by the programmer or user.
960 * @param symbolsToAdopt DecimalFormatSymbols to be adopted.
963 virtual void adoptDecimalFormatSymbols(DecimalFormatSymbols
* symbolsToAdopt
);
966 * Sets the decimal format symbols, which is generally not changed
967 * by the programmer or user.
968 * @param symbols DecimalFormatSymbols.
971 virtual void setDecimalFormatSymbols(const DecimalFormatSymbols
& symbols
);
975 * Get the positive prefix.
977 * @param result Output param which will receive the positive prefix.
978 * @return A reference to 'result'.
979 * Examples: +123, $123, sFr123
982 UnicodeString
& getPositivePrefix(UnicodeString
& result
) const;
985 * Set the positive prefix.
987 * @param newValue the new value of the the positive prefix to be set.
988 * Examples: +123, $123, sFr123
991 virtual void setPositivePrefix(const UnicodeString
& newValue
);
994 * Get the negative prefix.
996 * @param result Output param which will receive the negative prefix.
997 * @return A reference to 'result'.
998 * Examples: -123, ($123) (with negative suffix), sFr-123
1001 UnicodeString
& getNegativePrefix(UnicodeString
& result
) const;
1004 * Set the negative prefix.
1006 * @param newValue the new value of the the negative prefix to be set.
1007 * Examples: -123, ($123) (with negative suffix), sFr-123
1010 virtual void setNegativePrefix(const UnicodeString
& newValue
);
1013 * Get the positive suffix.
1015 * @param result Output param which will receive the positive suffix.
1016 * @return A reference to 'result'.
1020 UnicodeString
& getPositiveSuffix(UnicodeString
& result
) const;
1023 * Set the positive suffix.
1025 * @param newValue the new value of the positive suffix to be set.
1029 virtual void setPositiveSuffix(const UnicodeString
& newValue
);
1032 * Get the negative suffix.
1034 * @param result Output param which will receive the negative suffix.
1035 * @return A reference to 'result'.
1036 * Examples: -123%, ($123) (with positive suffixes)
1039 UnicodeString
& getNegativeSuffix(UnicodeString
& result
) const;
1042 * Set the negative suffix.
1044 * @param newValue the new value of the negative suffix to be set.
1048 virtual void setNegativeSuffix(const UnicodeString
& newValue
);
1051 * Get the multiplier for use in percent, permill, etc.
1052 * For a percentage, set the suffixes to have "%" and the multiplier to be 100.
1053 * (For Arabic, use arabic percent symbol).
1054 * For a permill, set the suffixes to have "\\u2031" and the multiplier to be 1000.
1056 * @return the multiplier for use in percent, permill, etc.
1057 * Examples: with 100, 1.23 -> "123", and "123" -> 1.23
1060 int32_t getMultiplier(void) const;
1063 * Set the multiplier for use in percent, permill, etc.
1064 * For a percentage, set the suffixes to have "%" and the multiplier to be 100.
1065 * (For Arabic, use arabic percent symbol).
1066 * For a permill, set the suffixes to have "\\u2031" and the multiplier to be 1000.
1068 * @param newValue the new value of the multiplier for use in percent, permill, etc.
1069 * Examples: with 100, 1.23 -> "123", and "123" -> 1.23
1072 virtual void setMultiplier(int32_t newValue
);
1075 * Get the rounding increment.
1076 * @return A positive rounding increment, or 0.0 if rounding
1078 * @see #setRoundingIncrement
1079 * @see #getRoundingMode
1080 * @see #setRoundingMode
1083 virtual double getRoundingIncrement(void) const;
1086 * Set the rounding increment. This method also controls whether
1087 * rounding is enabled.
1088 * @param newValue A positive rounding increment, or 0.0 to disable rounding.
1089 * Negative increments are equivalent to 0.0.
1090 * @see #getRoundingIncrement
1091 * @see #getRoundingMode
1092 * @see #setRoundingMode
1095 virtual void setRoundingIncrement(double newValue
);
1098 * Get the rounding mode.
1099 * @return A rounding mode
1100 * @see #setRoundingIncrement
1101 * @see #getRoundingIncrement
1102 * @see #setRoundingMode
1105 virtual ERoundingMode
getRoundingMode(void) const;
1108 * Set the rounding mode. This has no effect unless the rounding
1109 * increment is greater than zero.
1110 * @param roundingMode A rounding mode
1111 * @see #setRoundingIncrement
1112 * @see #getRoundingIncrement
1113 * @see #getRoundingMode
1116 virtual void setRoundingMode(ERoundingMode roundingMode
);
1119 * Get the width to which the output of format() is padded.
1120 * The width is counted in 16-bit code units.
1121 * @return the format width, or zero if no padding is in effect
1122 * @see #setFormatWidth
1123 * @see #getPadCharacterString
1124 * @see #setPadCharacter
1125 * @see #getPadPosition
1126 * @see #setPadPosition
1129 virtual int32_t getFormatWidth(void) const;
1132 * Set the width to which the output of format() is padded.
1133 * The width is counted in 16-bit code units.
1134 * This method also controls whether padding is enabled.
1135 * @param width the width to which to pad the result of
1136 * format(), or zero to disable padding. A negative
1137 * width is equivalent to 0.
1138 * @see #getFormatWidth
1139 * @see #getPadCharacterString
1140 * @see #setPadCharacter
1141 * @see #getPadPosition
1142 * @see #setPadPosition
1145 virtual void setFormatWidth(int32_t width
);
1148 * Get the pad character used to pad to the format width. The
1150 * @return a string containing the pad character. This will always
1151 * have a length of one 32-bit code point.
1152 * @see #setFormatWidth
1153 * @see #getFormatWidth
1154 * @see #setPadCharacter
1155 * @see #getPadPosition
1156 * @see #setPadPosition
1159 virtual UnicodeString
getPadCharacterString() const;
1162 * Set the character used to pad to the format width. If padding
1163 * is not enabled, then this will take effect if padding is later
1165 * @param padChar a string containing the pad charcter. If the string
1166 * has length 0, then the pad characer is set to ' '. Otherwise
1167 * padChar.char32At(0) will be used as the pad character.
1168 * @see #setFormatWidth
1169 * @see #getFormatWidth
1170 * @see #getPadCharacterString
1171 * @see #getPadPosition
1172 * @see #setPadPosition
1175 virtual void setPadCharacter(const UnicodeString
&padChar
);
1178 * Get the position at which padding will take place. This is the location
1179 * at which padding will be inserted if the result of format()
1180 * is shorter than the format width.
1181 * @return the pad position, one of kPadBeforePrefix,
1182 * kPadAfterPrefix, kPadBeforeSuffix, or
1184 * @see #setFormatWidth
1185 * @see #getFormatWidth
1186 * @see #setPadCharacter
1187 * @see #getPadCharacterString
1188 * @see #setPadPosition
1189 * @see #EPadPosition
1192 virtual EPadPosition
getPadPosition(void) const;
1195 * Set the position at which padding will take place. This is the location
1196 * at which padding will be inserted if the result of format()
1197 * is shorter than the format width. This has no effect unless padding is
1199 * @param padPos the pad position, one of kPadBeforePrefix,
1200 * kPadAfterPrefix, kPadBeforeSuffix, or
1202 * @see #setFormatWidth
1203 * @see #getFormatWidth
1204 * @see #setPadCharacter
1205 * @see #getPadCharacterString
1206 * @see #getPadPosition
1207 * @see #EPadPosition
1210 virtual void setPadPosition(EPadPosition padPos
);
1213 * Return whether or not scientific notation is used.
1214 * @return TRUE if this object formats and parses scientific notation
1215 * @see #setScientificNotation
1216 * @see #getMinimumExponentDigits
1217 * @see #setMinimumExponentDigits
1218 * @see #isExponentSignAlwaysShown
1219 * @see #setExponentSignAlwaysShown
1222 virtual UBool
isScientificNotation(void);
1225 * Set whether or not scientific notation is used. When scientific notation
1226 * is used, the effective maximum number of integer digits is <= 8. If the
1227 * maximum number of integer digits is set to more than 8, the effective
1228 * maximum will be 1. This allows this call to generate a 'default' scientific
1229 * number format without additional changes.
1230 * @param useScientific TRUE if this object formats and parses scientific
1232 * @see #isScientificNotation
1233 * @see #getMinimumExponentDigits
1234 * @see #setMinimumExponentDigits
1235 * @see #isExponentSignAlwaysShown
1236 * @see #setExponentSignAlwaysShown
1239 virtual void setScientificNotation(UBool useScientific
);
1242 * Return the minimum exponent digits that will be shown.
1243 * @return the minimum exponent digits that will be shown
1244 * @see #setScientificNotation
1245 * @see #isScientificNotation
1246 * @see #setMinimumExponentDigits
1247 * @see #isExponentSignAlwaysShown
1248 * @see #setExponentSignAlwaysShown
1251 virtual int8_t getMinimumExponentDigits(void) const;
1254 * Set the minimum exponent digits that will be shown. This has no
1255 * effect unless scientific notation is in use.
1256 * @param minExpDig a value >= 1 indicating the fewest exponent digits
1257 * that will be shown. Values less than 1 will be treated as 1.
1258 * @see #setScientificNotation
1259 * @see #isScientificNotation
1260 * @see #getMinimumExponentDigits
1261 * @see #isExponentSignAlwaysShown
1262 * @see #setExponentSignAlwaysShown
1265 virtual void setMinimumExponentDigits(int8_t minExpDig
);
1268 * Return whether the exponent sign is always shown.
1269 * @return TRUE if the exponent is always prefixed with either the
1270 * localized minus sign or the localized plus sign, false if only negative
1271 * exponents are prefixed with the localized minus sign.
1272 * @see #setScientificNotation
1273 * @see #isScientificNotation
1274 * @see #setMinimumExponentDigits
1275 * @see #getMinimumExponentDigits
1276 * @see #setExponentSignAlwaysShown
1279 virtual UBool
isExponentSignAlwaysShown(void);
1282 * Set whether the exponent sign is always shown. This has no effect
1283 * unless scientific notation is in use.
1284 * @param expSignAlways TRUE if the exponent is always prefixed with either
1285 * the localized minus sign or the localized plus sign, false if only
1286 * negative exponents are prefixed with the localized minus sign.
1287 * @see #setScientificNotation
1288 * @see #isScientificNotation
1289 * @see #setMinimumExponentDigits
1290 * @see #getMinimumExponentDigits
1291 * @see #isExponentSignAlwaysShown
1294 virtual void setExponentSignAlwaysShown(UBool expSignAlways
);
1297 * Return the grouping size. Grouping size is the number of digits between
1298 * grouping separators in the integer portion of a number. For example,
1299 * in the number "123,456.78", the grouping size is 3.
1301 * @return the grouping size.
1302 * @see setGroupingSize
1303 * @see NumberFormat::isGroupingUsed
1304 * @see DecimalFormatSymbols::getGroupingSeparator
1307 int32_t getGroupingSize(void) const;
1310 * Set the grouping size. Grouping size is the number of digits between
1311 * grouping separators in the integer portion of a number. For example,
1312 * in the number "123,456.78", the grouping size is 3.
1314 * @param newValue the new value of the grouping size.
1315 * @see getGroupingSize
1316 * @see NumberFormat::setGroupingUsed
1317 * @see DecimalFormatSymbols::setGroupingSeparator
1320 virtual void setGroupingSize(int32_t newValue
);
1323 * Return the secondary grouping size. In some locales one
1324 * grouping interval is used for the least significant integer
1325 * digits (the primary grouping size), and another is used for all
1326 * others (the secondary grouping size). A formatter supporting a
1327 * secondary grouping size will return a positive integer unequal
1328 * to the primary grouping size returned by
1329 * getGroupingSize(). For example, if the primary
1330 * grouping size is 4, and the secondary grouping size is 2, then
1331 * the number 123456789 formats as "1,23,45,6789", and the pattern
1332 * appears as "#,##,###0".
1333 * @return the secondary grouping size, or a value less than
1334 * one if there is none
1335 * @see setSecondaryGroupingSize
1336 * @see NumberFormat::isGroupingUsed
1337 * @see DecimalFormatSymbols::getGroupingSeparator
1340 int32_t getSecondaryGroupingSize(void) const;
1343 * Set the secondary grouping size. If set to a value less than 1,
1344 * then secondary grouping is turned off, and the primary grouping
1345 * size is used for all intervals, not just the least significant.
1347 * @param newValue the new value of the secondary grouping size.
1348 * @see getSecondaryGroupingSize
1349 * @see NumberFormat#setGroupingUsed
1350 * @see DecimalFormatSymbols::setGroupingSeparator
1353 virtual void setSecondaryGroupingSize(int32_t newValue
);
1356 * Allows you to get the behavior of the decimal separator with integers.
1357 * (The decimal separator will always appear with decimals.)
1359 * @return TRUE if the decimal separator always appear with decimals.
1360 * Example: Decimal ON: 12345 -> 12345.; OFF: 12345 -> 12345
1363 UBool
isDecimalSeparatorAlwaysShown(void) const;
1366 * Allows you to set the behavior of the decimal separator with integers.
1367 * (The decimal separator will always appear with decimals.)
1369 * @param newValue set TRUE if the decimal separator will always appear with decimals.
1370 * Example: Decimal ON: 12345 -> 12345.; OFF: 12345 -> 12345
1373 virtual void setDecimalSeparatorAlwaysShown(UBool newValue
);
1376 * Synthesizes a pattern string that represents the current state
1377 * of this Format object.
1379 * @param result Output param which will receive the pattern.
1380 * Previous contents are deleted.
1381 * @return A reference to 'result'.
1385 virtual UnicodeString
& toPattern(UnicodeString
& result
) const;
1388 * Synthesizes a localized pattern string that represents the current
1389 * state of this Format object.
1391 * @param result Output param which will receive the localized pattern.
1392 * Previous contents are deleted.
1393 * @return A reference to 'result'.
1397 virtual UnicodeString
& toLocalizedPattern(UnicodeString
& result
) const;
1400 * Apply the given pattern to this Format object. A pattern is a
1401 * short-hand specification for the various formatting properties.
1402 * These properties can also be changed individually through the
1403 * various setter methods.
1405 * There is no limit to integer digits are set
1406 * by this routine, since that is the typical end-user desire;
1407 * use setMaximumInteger if you want to set a real value.
1408 * For negative numbers, use a second pattern, separated by a semicolon
1410 * . Example "#,#00.0#" -> 1,234.56
1412 * This means a minimum of 2 integer digits, 1 fraction digit, and
1413 * a maximum of 2 fraction digits.
1415 * . Example: "#,#00.0#;(#,#00.0#)" for negatives in parantheses.
1417 * In negative patterns, the minimum and maximum counts are ignored;
1418 * these are presumed to be set in the positive pattern.
1420 * @param pattern The pattern to be applied.
1421 * @param parseError Struct to recieve information on position
1422 * of error if an error is encountered
1423 * @param status Output param set to success/failure code on
1424 * exit. If the pattern is invalid, this will be
1425 * set to a failure result.
1428 virtual void applyPattern(const UnicodeString
& pattern
,
1429 UParseError
& parseError
,
1430 UErrorCode
& status
);
1433 * @param pattern The pattern to be applied.
1434 * @param status Output param set to success/failure code on
1435 * exit. If the pattern is invalid, this will be
1436 * set to a failure result.
1439 virtual void applyPattern(const UnicodeString
& pattern
,
1440 UErrorCode
& status
);
1443 * Apply the given pattern to this Format object. The pattern
1444 * is assumed to be in a localized notation. A pattern is a
1445 * short-hand specification for the various formatting properties.
1446 * These properties can also be changed individually through the
1447 * various setter methods.
1449 * There is no limit to integer digits are set
1450 * by this routine, since that is the typical end-user desire;
1451 * use setMaximumInteger if you want to set a real value.
1452 * For negative numbers, use a second pattern, separated by a semicolon
1454 * . Example "#,#00.0#" -> 1,234.56
1456 * This means a minimum of 2 integer digits, 1 fraction digit, and
1457 * a maximum of 2 fraction digits.
1459 * Example: "#,#00.0#;(#,#00.0#)" for negatives in parantheses.
1461 * In negative patterns, the minimum and maximum counts are ignored;
1462 * these are presumed to be set in the positive pattern.
1464 * @param pattern The localized pattern to be applied.
1465 * @param parseError Struct to recieve information on position
1466 * of error if an error is encountered
1467 * @param status Output param set to success/failure code on
1468 * exit. If the pattern is invalid, this will be
1469 * set to a failure result.
1472 virtual void applyLocalizedPattern(const UnicodeString
& pattern
,
1473 UParseError
& parseError
,
1474 UErrorCode
& status
);
1477 * Apply the given pattern to this Format object.
1479 * @param pattern The localized pattern to be applied.
1480 * @param status Output param set to success/failure code on
1481 * exit. If the pattern is invalid, this will be
1482 * set to a failure result.
1485 virtual void applyLocalizedPattern(const UnicodeString
& pattern
,
1486 UErrorCode
& status
);
1490 * Sets the maximum number of digits allowed in the integer portion of a
1491 * number. This override limits the integer digit count to 309.
1493 * @param newValue the new value of the maximum number of digits
1494 * allowed in the integer portion of a number.
1495 * @see NumberFormat#setMaximumIntegerDigits
1498 virtual void setMaximumIntegerDigits(int32_t newValue
);
1501 * Sets the minimum number of digits allowed in the integer portion of a
1502 * number. This override limits the integer digit count to 309.
1504 * @param newValue the new value of the minimum number of digits
1505 * allowed in the integer portion of a number.
1506 * @see NumberFormat#setMinimumIntegerDigits
1509 virtual void setMinimumIntegerDigits(int32_t newValue
);
1512 * Sets the maximum number of digits allowed in the fraction portion of a
1513 * number. This override limits the fraction digit count to 340.
1515 * @param newValue the new value of the maximum number of digits
1516 * allowed in the fraction portion of a number.
1517 * @see NumberFormat#setMaximumFractionDigits
1520 virtual void setMaximumFractionDigits(int32_t newValue
);
1523 * Sets the minimum number of digits allowed in the fraction portion of a
1524 * number. This override limits the fraction digit count to 340.
1526 * @param newValue the new value of the minimum number of digits
1527 * allowed in the fraction portion of a number.
1528 * @see NumberFormat#setMinimumFractionDigits
1531 virtual void setMinimumFractionDigits(int32_t newValue
);
1534 * Returns the minimum number of significant digits that will be
1535 * displayed. This value has no effect unless areSignificantDigitsUsed()
1537 * @return the fewest significant digits that will be shown
1540 int32_t getMinimumSignificantDigits() const;
1543 * Returns the maximum number of significant digits that will be
1544 * displayed. This value has no effect unless areSignificantDigitsUsed()
1546 * @return the most significant digits that will be shown
1549 int32_t getMaximumSignificantDigits() const;
1552 * Sets the minimum number of significant digits that will be
1553 * displayed. If <code>min</code> is less than one then it is set
1554 * to one. If the maximum significant digits count is less than
1555 * <code>min</code>, then it is set to <code>min</code>. This
1556 * value has no effect unless areSignificantDigits() returns true.
1557 * @param min the fewest significant digits to be shown
1560 void setMinimumSignificantDigits(int32_t min
);
1563 * Sets the maximum number of significant digits that will be
1564 * displayed. If <code>max</code> is less than one then it is set
1565 * to one. If the minimum significant digits count is greater
1566 * than <code>max</code>, then it is set to <code>max</code>.
1567 * This value has no effect unless areSignificantDigits() returns
1569 * @param max the most significant digits to be shown
1572 void setMaximumSignificantDigits(int32_t max
);
1575 * Returns true if significant digits are in use, or false if
1576 * integer and fraction digit counts are in use.
1577 * @return true if significant digits are in use
1580 UBool
areSignificantDigitsUsed() const;
1583 * Sets whether significant digits are in use, or integer and
1584 * fraction digit counts are in use.
1585 * @param useSignificantDigits true to use significant digits, or
1586 * false to use integer and fraction digit counts
1589 void setSignificantDigitsUsed(UBool useSignificantDigits
);
1593 * Sets the currency used to display currency
1594 * amounts. This takes effect immediately, if this format is a
1595 * currency format. If this format is not a currency format, then
1596 * the currency is used if and when this object becomes a
1597 * currency format through the application of a new pattern.
1598 * @param theCurrency a 3-letter ISO code indicating new currency
1599 * to use. It need not be null-terminated. May be the empty
1600 * string or NULL to indicate no currency.
1601 * @param ec input-output error code
1604 virtual void setCurrency(const UChar
* theCurrency
, UErrorCode
& ec
);
1607 * Sets the currency used to display currency amounts. See
1608 * setCurrency(const UChar*, UErrorCode&).
1609 * @deprecated ICU 3.0. Use setCurrency(const UChar*, UErrorCode&).
1611 virtual void setCurrency(const UChar
* theCurrency
);
1614 * The resource tags we use to retrieve decimal format data from
1615 * locale resource bundles.
1616 * @deprecated ICU 3.4. This string has no public purpose. Please don't use it.
1618 static const char fgNumberPatterns
[];
1623 * Return the class ID for this class. This is useful only for
1624 * comparing to a return value from getDynamicClassID(). For example:
1626 * . Base* polymorphic_pointer = createPolymorphicObject();
1627 * . if (polymorphic_pointer->getDynamicClassID() ==
1628 * . Derived::getStaticClassID()) ...
1630 * @return The class ID for all objects of this class.
1633 static UClassID U_EXPORT2
getStaticClassID(void);
1636 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override.
1637 * This method is to implement a simple version of RTTI, since not all
1638 * C++ compilers support genuine RTTI. Polymorphic operator==() and
1639 * clone() methods call this method.
1641 * @return The class ID for this object. All objects of a
1642 * given class have the same class ID. Objects of
1643 * other classes have different class IDs.
1646 virtual UClassID
getDynamicClassID(void) const;
1649 DecimalFormat(); // default constructor not implemented
1651 int32_t precision(UBool isIntegral
) const;
1654 * Do real work of constructing a new DecimalFormat.
1656 void construct(UErrorCode
& status
,
1657 UParseError
& parseErr
,
1658 const UnicodeString
* pattern
= 0,
1659 DecimalFormatSymbols
* symbolsToAdopt
= 0
1663 * Does the real work of generating a pattern.
1665 * @param result Output param which will receive the pattern.
1666 * Previous contents are deleted.
1667 * @param localized TRUE return localized pattern.
1668 * @return A reference to 'result'.
1670 UnicodeString
& toPattern(UnicodeString
& result
, UBool localized
) const;
1673 * Does the real work of applying a pattern.
1674 * @param pattern The pattern to be applied.
1675 * @param localized If true, the pattern is localized; else false.
1676 * @param parseError Struct to recieve information on position
1677 * of error if an error is encountered
1678 * @param status Output param set to success/failure code on
1679 * exit. If the pattern is invalid, this will be
1680 * set to a failure result.
1682 void applyPattern(const UnicodeString
& pattern
,
1684 UParseError
& parseError
,
1685 UErrorCode
& status
);
1687 * Do the work of formatting a number, either a double or a long.
1689 * @param appendTo Output parameter to receive result.
1690 * Result is appended to existing contents.
1691 * @param fieldPosition On input: an alignment field, if desired.
1692 * On output: the offsets of the alignment field.
1693 * @param digits the digits to be formatted.
1694 * @param isInteger if TRUE format the digits as Integer.
1695 * @return Reference to 'appendTo' parameter.
1697 UnicodeString
& subformat(UnicodeString
& appendTo
,
1698 FieldPosition
& fieldPosition
,
1700 UBool isInteger
) const;
1702 void parse(const UnicodeString
& text
,
1703 Formattable
& result
,
1705 UBool parseCurrency
) const;
1709 fgStatusLength
// Leave last in list.
1712 UBool
subparse(const UnicodeString
& text
, ParsePosition
& parsePosition
,
1713 DigitList
& digits
, UBool
* status
,
1714 UChar
* currency
) const;
1716 int32_t skipPadding(const UnicodeString
& text
, int32_t position
) const;
1718 int32_t compareAffix(const UnicodeString
& input
,
1722 UChar
* currency
) const;
1724 static int32_t compareSimpleAffix(const UnicodeString
& affix
,
1725 const UnicodeString
& input
,
1729 static int32_t skipRuleWhiteSpace(const UnicodeString
& text
, int32_t pos
);
1731 static int32_t skipUWhiteSpace(const UnicodeString
& text
, int32_t pos
);
1733 int32_t compareComplexAffix(const UnicodeString
& affixPat
,
1734 const UnicodeString
& input
,
1736 UChar
* currency
) const;
1738 static int32_t match(const UnicodeString
& text
, int32_t pos
, UChar32 ch
);
1740 static int32_t match(const UnicodeString
& text
, int32_t pos
, const UnicodeString
& str
);
1742 static UBool
matchSymbol(const UnicodeString
&text
, int32_t position
, int32_t length
, const UnicodeString
&symbol
,
1743 UnicodeSet
*sset
, UChar32 schar
);
1746 * Get a decimal format symbol.
1747 * Returns a const reference to the symbol string.
1750 inline const UnicodeString
&getConstSymbol(DecimalFormatSymbols::ENumberFormatSymbol symbol
) const;
1752 int32_t appendAffix(UnicodeString
& buf
, double number
,
1753 UBool isNegative
, UBool isPrefix
) const;
1756 * Append an affix to the given UnicodeString, using quotes if
1757 * there are special characters. Single quotes themselves must be
1758 * escaped in either case.
1760 void appendAffixPattern(UnicodeString
& appendTo
, const UnicodeString
& affix
,
1761 UBool localized
) const;
1763 void appendAffixPattern(UnicodeString
& appendTo
,
1764 const UnicodeString
* affixPattern
,
1765 const UnicodeString
& expAffix
, UBool localized
) const;
1767 void expandAffix(const UnicodeString
& pattern
,
1768 UnicodeString
& affix
,
1770 UBool doFormat
) const;
1772 void expandAffixes();
1774 static double round(double a
, ERoundingMode mode
, UBool isNegative
);
1776 void addPadding(UnicodeString
& appendTo
,
1777 FieldPosition
& fieldPosition
,
1778 int32_t prefixLen
, int32_t suffixLen
) const;
1780 UBool
isGroupingPosition(int32_t pos
) const;
1782 void setCurrencyForSymbols();
1787 //static const int8_t fgMaxDigit; // The largest digit, in this case 9
1789 /*transient*/ //DigitList* fDigitList;
1791 UnicodeString fPositivePrefix
;
1792 UnicodeString fPositiveSuffix
;
1793 UnicodeString fNegativePrefix
;
1794 UnicodeString fNegativeSuffix
;
1795 UnicodeString
* fPosPrefixPattern
;
1796 UnicodeString
* fPosSuffixPattern
;
1797 UnicodeString
* fNegPrefixPattern
;
1798 UnicodeString
* fNegSuffixPattern
;
1801 * Formatter for ChoiceFormat-based currency names. If this field
1802 * is not null, then delegate to it to format currency symbols.
1805 ChoiceFormat
* fCurrencyChoice
;
1807 int32_t fMultiplier
;
1808 int32_t fGroupingSize
;
1809 int32_t fGroupingSize2
;
1810 UBool fDecimalSeparatorAlwaysShown
;
1811 /*transient*/ UBool fIsCurrencyFormat
;
1812 DecimalFormatSymbols
* fSymbols
;
1814 UBool fUseSignificantDigits
;
1815 int32_t fMinSignificantDigits
;
1816 int32_t fMaxSignificantDigits
;
1818 UBool fUseExponentialNotation
;
1819 int8_t fMinExponentDigits
;
1820 UBool fExponentSignAlwaysShown
;
1822 /* If fRoundingIncrement is NULL, there is no rounding. Otherwise, round to
1823 * fRoundingIncrement.getDouble(). Since this operation may be expensive,
1824 * we cache the result in fRoundingDouble. All methods that update
1825 * fRoundingIncrement also update fRoundingDouble. */
1826 DigitList
* fRoundingIncrement
;
1827 /*transient*/ double fRoundingDouble
;
1828 ERoundingMode fRoundingMode
;
1831 int32_t fFormatWidth
;
1832 EPadPosition fPadPosition
;
1837 * Returns the currency in effect for this formatter. Subclasses
1838 * should override this method as needed. Unlike getCurrency(),
1839 * this method should never return "".
1840 * @result output parameter for null-terminated result, which must
1841 * have a capacity of at least 4
1844 virtual void getEffectiveCurrency(UChar
* result
, UErrorCode
& ec
) const;
1846 /** number of integer digits
1849 static const int32_t kDoubleIntegerDigits
;
1850 /** number of fraction digits
1853 static const int32_t kDoubleFractionDigits
;
1856 * When someone turns on scientific mode, we assume that more than this
1857 * number of digits is due to flipping from some other mode that didn't
1858 * restrict the maximum, and so we force 1 integer digit. We don't bother
1859 * to track and see if someone is using exponential notation with more than
1860 * this number, it wouldn't make sense anyway, and this is just to make sure
1861 * that someone turning on scientific mode with default settings doesn't
1862 * end up with lots of zeroes.
1865 static const int32_t kMaxScientificIntegerDigits
;
1868 inline UnicodeString
&
1869 DecimalFormat::format(const Formattable
& obj
,
1870 UnicodeString
& appendTo
,
1871 UErrorCode
& status
) const {
1872 // Don't use Format:: - use immediate base class only,
1873 // in case immediate base modifies behavior later.
1874 return NumberFormat::format(obj
, appendTo
, status
);
1877 inline UnicodeString
&
1878 DecimalFormat::format(double number
,
1879 UnicodeString
& appendTo
) const {
1880 FieldPosition
pos(0);
1881 return format(number
, appendTo
, pos
);
1884 inline UnicodeString
&
1885 DecimalFormat::format(int32_t number
,
1886 UnicodeString
& appendTo
) const {
1887 FieldPosition
pos(0);
1888 return format((int64_t)number
, appendTo
, pos
);
1891 inline const UnicodeString
&
1892 DecimalFormat::getConstSymbol(DecimalFormatSymbols::ENumberFormatSymbol symbol
) const {
1893 return fSymbols
->getConstSymbol(symbol
);
1898 #endif /* #if !UCONFIG_NO_FORMATTING */