2 ********************************************************************************
3 * Copyright (C) 1997-2006, 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"
45 * DecimalFormat is a concrete subclass of NumberFormat that formats decimal
46 * numbers. It has a variety of features designed to make it possible to parse
47 * and format numbers in any locale, including support for Western, Arabic, or
48 * Indic digits. It also supports different flavors of numbers, including
49 * integers ("123"), fixed-point numbers ("123.4"), scientific notation
50 * ("1.23E4"), percentages ("12%"), and currency amounts ("$123"). All of these
51 * flavors can be easily localized.
53 * <p>To obtain a NumberFormat for a specific locale (including the default
54 * locale) call one of NumberFormat's factory methods such as
55 * createInstance(). Do not call the DecimalFormat constructors directly, unless
56 * you know what you are doing, since the NumberFormat factory methods may
57 * return subclasses other than DecimalFormat.
59 * <p><strong>Example Usage</strong>
62 * // Normally we would have a GUI with a menu for this
64 * const Locale* locales = NumberFormat::getAvailableLocales(locCount);
66 * double myNumber = -1234.56;
67 * UErrorCode success = U_ZERO_ERROR;
70 * // Print out a number with the localized number, currency and percent
71 * // format for each locale.
72 * UnicodeString countryName;
73 * UnicodeString displayName;
75 * UnicodeString pattern;
76 * Formattable fmtable;
77 * for (int32_t j = 0; j < 3; ++j) {
78 * cout << endl << "FORMAT " << j << endl;
79 * for (int32_t i = 0; i < locCount; ++i) {
80 * if (locales[i].getCountry(countryName).size() == 0) {
81 * // skip language-only
86 * form = NumberFormat::createInstance(locales[i], success ); break;
88 * form = NumberFormat::createCurrencyInstance(locales[i], success ); break;
90 * form = NumberFormat::createPercentInstance(locales[i], success ); break;
94 * pattern = ((DecimalFormat*)form)->toPattern(pattern);
95 * cout << locales[i].getDisplayName(displayName) << ": " << pattern;
96 * cout << " -> " << form->format(myNumber,str) << endl;
97 * form->parse(form->format(myNumber,str), fmtable, success);
104 * <p><strong>Patterns</strong>
106 * <p>A DecimalFormat consists of a <em>pattern</em> and a set of
107 * <em>symbols</em>. The pattern may be set directly using
108 * applyPattern(), or indirectly using other API methods which
109 * manipulate aspects of the pattern, such as the minimum number of integer
110 * digits. The symbols are stored in a DecimalFormatSymbols
111 * object. When using the NumberFormat factory methods, the
112 * pattern and symbols are read from ICU's locale data.
114 * <p><strong>Special Pattern Characters</strong>
116 * <p>Many characters in a pattern are taken literally; they are matched during
117 * parsing and output unchanged during formatting. Special characters, on the
118 * other hand, stand for other characters, strings, or classes of characters.
119 * For example, the '#' character is replaced by a localized digit. Often the
120 * replacement character is the same as the pattern character; in the U.S. locale,
121 * the ',' grouping character is replaced by ','. However, the replacement is
122 * still happening, and if the symbols are modified, the grouping character
123 * changes. Some special characters affect the behavior of the formatter by
124 * their presence; for example, if the percent character is seen, then the
125 * value is multiplied by 100 before being displayed.
127 * <p>To insert a special character in a pattern as a literal, that is, without
128 * any special meaning, the character must be quoted. There are some exceptions to
129 * this which are noted below.
131 * <p>The characters listed here are used in non-localized patterns. Localized
132 * patterns use the corresponding characters taken from this formatter's
133 * DecimalFormatSymbols object instead, and these characters lose
134 * their special status. Two exceptions are the currency sign and quote, which
137 * <table border=0 cellspacing=3 cellpadding=0>
138 * <tr bgcolor="#ccccff">
139 * <td align=left><strong>Symbol</strong>
140 * <td align=left><strong>Location</strong>
141 * <td align=left><strong>Localized?</strong>
142 * <td align=left><strong>Meaning</strong>
148 * <tr valign=top bgcolor="#eeeeff">
149 * <td><code>1-9</code>
152 * <td>'1' through '9' indicate rounding.
154 * <td><code>\htmlonly@\endhtmlonly</code> <!--doxygen doesn't like @-->
157 * <td>Significant digit
158 * <tr valign=top bgcolor="#eeeeff">
162 * <td>Digit, zero shows as absent
167 * <td>Decimal separator or monetary decimal separator
168 * <tr valign=top bgcolor="#eeeeff">
177 * <td>Grouping separator
178 * <tr valign=top bgcolor="#eeeeff">
182 * <td>Separates mantissa and exponent in scientific notation.
183 * <em>Need not be quoted in prefix or suffix.</em>
188 * <td>Prefix positive exponents with localized plus sign.
189 * <em>Need not be quoted in prefix or suffix.</em>
190 * <tr valign=top bgcolor="#eeeeff">
192 * <td>Subpattern boundary
194 * <td>Separates positive and negative subpatterns
196 * <td><code>\%</code>
197 * <td>Prefix or suffix
199 * <td>Multiply by 100 and show as percentage
200 * <tr valign=top bgcolor="#eeeeff">
201 * <td><code>\\u2030</code>
202 * <td>Prefix or suffix
204 * <td>Multiply by 1000 and show as per mille
206 * <td><code>\htmlonly¤\endhtmlonly</code> (<code>\\u00A4</code>)
207 * <td>Prefix or suffix
209 * <td>Currency sign, replaced by currency symbol. If
210 * doubled, replaced by international currency symbol.
211 * If present in a pattern, the monetary decimal separator
212 * is used instead of the decimal separator.
213 * <tr valign=top bgcolor="#eeeeff">
215 * <td>Prefix or suffix
217 * <td>Used to quote special characters in a prefix or suffix,
218 * for example, <code>"'#'#"</code> formats 123 to
219 * <code>"#123"</code>. To create a single quote
220 * itself, use two in a row: <code>"# o''clock"</code>.
223 * <td>Prefix or suffix boundary
225 * <td>Pad escape, precedes pad character
228 * <p>A DecimalFormat pattern contains a postive and negative
229 * subpattern, for example, "#,##0.00;(#,##0.00)". Each subpattern has a
230 * prefix, a numeric part, and a suffix. If there is no explicit negative
231 * subpattern, the negative subpattern is the localized minus sign prefixed to the
232 * positive subpattern. That is, "0.00" alone is equivalent to "0.00;-0.00". If there
233 * is an explicit negative subpattern, it serves only to specify the negative
234 * prefix and suffix; the number of digits, minimal digits, and other
235 * characteristics are ignored in the negative subpattern. That means that
236 * "#,##0.0#;(#)" has precisely the same result as "#,##0.0#;(#,##0.0#)".
238 * <p>The prefixes, suffixes, and various symbols used for infinity, digits,
239 * thousands separators, decimal separators, etc. may be set to arbitrary
240 * values, and they will appear properly during formatting. However, care must
241 * be taken that the symbols and strings do not conflict, or parsing will be
242 * unreliable. For example, either the positive and negative prefixes or the
243 * suffixes must be distinct for parse() to be able
244 * to distinguish positive from negative values. Another example is that the
245 * decimal separator and thousands separator should be distinct characters, or
246 * parsing will be impossible.
248 * <p>The <em>grouping separator</em> is a character that separates clusters of
249 * integer digits to make large numbers more legible. It commonly used for
250 * thousands, but in some locales it separates ten-thousands. The <em>grouping
251 * size</em> is the number of digits between the grouping separators, such as 3
252 * for "100,000,000" or 4 for "1 0000 0000". There are actually two different
253 * grouping sizes: One used for the least significant integer digits, the
254 * <em>primary grouping size</em>, and one used for all others, the
255 * <em>secondary grouping size</em>. In most locales these are the same, but
256 * sometimes they are different. For example, if the primary grouping interval
257 * is 3, and the secondary is 2, then this corresponds to the pattern
258 * "#,##,##0", and the number 123456789 is formatted as "12,34,56,789". If a
259 * pattern contains multiple grouping separators, the interval between the last
260 * one and the end of the integer defines the primary grouping size, and the
261 * interval between the last two defines the secondary grouping size. All others
262 * are ignored, so "#,##,###,####" == "###,###,####" == "##,#,###,####".
264 * <p>Illegal patterns, such as "#.#.#" or "#.###,###", will cause
265 * DecimalFormat to set a failing UErrorCode.
267 * <p><strong>Pattern BNF</strong>
270 * pattern := subpattern (';' subpattern)?
271 * subpattern := prefix? number exponent? suffix?
272 * number := (integer ('.' fraction)?) | sigDigits
273 * prefix := '\\u0000'..'\\uFFFD' - specialCharacters
274 * suffix := '\\u0000'..'\\uFFFD' - specialCharacters
275 * integer := '#'* '0'* '0'
276 * fraction := '0'* '#'*
277 * sigDigits := '#'* '@' '@'* '#'*
278 * exponent := 'E' '+'? '0'* '0'
279 * padSpec := '*' padChar
280 * padChar := '\\u0000'..'\\uFFFD' - quote
283 * X* 0 or more instances of X
284 * X? 0 or 1 instances of X
286 * C..D any character from C up to D, inclusive
287 * S-T characters in S, except those in T
289 * The first subpattern is for positive numbers. The second (optional)
290 * subpattern is for negative numbers.
292 * <p>Not indicated in the BNF syntax above:
294 * <ul><li>The grouping separator ',' can occur inside the integer and
295 * sigDigits elements, between any two pattern characters of that
296 * element, as long as the integer or sigDigits element is not
297 * followed by the exponent element.
299 * <li>Two grouping intervals are recognized: That between the
300 * decimal point and the first grouping symbol, and that
301 * between the first and second grouping symbols. These
302 * intervals are identical in most locales, but in some
303 * locales they differ. For example, the pattern
304 * "#,##,###" formats the number 123456789 as
305 * "12,34,56,789".</li>
307 * <li>The pad specifier <code>padSpec</code> may appear before the prefix,
308 * after the prefix, before the suffix, after the suffix, or not at all.
310 * <li>In place of '0', the digits '1' through '9' may be used to
311 * indicate a rounding increment.
314 * <p><strong>Parsing</strong>
316 * <p>DecimalFormat parses all Unicode characters that represent
317 * decimal digits, as defined by u_charDigitValue(). In addition,
318 * DecimalFormat also recognizes as digits the ten consecutive
319 * characters starting with the localized zero digit defined in the
320 * DecimalFormatSymbols object. During formatting, the
321 * DecimalFormatSymbols-based digits are output.
323 * <p>During parsing, grouping separators are ignored.
325 * <p>If parse(UnicodeString&,Formattable&,ParsePosition&)
326 * fails to parse a string, it leaves the parse position unchanged.
327 * The convenience method parse(UnicodeString&,Formattable&,UErrorCode&)
328 * indicates parse failure by setting a failing
331 * <p><strong>Formatting</strong>
333 * <p>Formatting is guided by several parameters, all of which can be
334 * specified either using a pattern or using the API. The following
335 * description applies to formats that do not use <a href="#sci">scientific
336 * notation</a> or <a href="#sigdig">significant digits</a>.
338 * <ul><li>If the number of actual integer digits exceeds the
339 * <em>maximum integer digits</em>, then only the least significant
340 * digits are shown. For example, 1997 is formatted as "97" if the
341 * maximum integer digits is set to 2.
343 * <li>If the number of actual integer digits is less than the
344 * <em>minimum integer digits</em>, then leading zeros are added. For
345 * example, 1997 is formatted as "01997" if the minimum integer digits
348 * <li>If the number of actual fraction digits exceeds the <em>maximum
349 * fraction digits</em>, then half-even rounding it performed to the
350 * maximum fraction digits. For example, 0.125 is formatted as "0.12"
351 * if the maximum fraction digits is 2. This behavior can be changed
352 * by specifying a rounding increment and a rounding mode.
354 * <li>If the number of actual fraction digits is less than the
355 * <em>minimum fraction digits</em>, then trailing zeros are added.
356 * For example, 0.125 is formatted as "0.1250" if the mimimum fraction
357 * digits is set to 4.
359 * <li>Trailing fractional zeros are not displayed if they occur
360 * <em>j</em> positions after the decimal, where <em>j</em> is less
361 * than the maximum fraction digits. For example, 0.10004 is
362 * formatted as "0.1" if the maximum fraction digits is four or less.
365 * <p><strong>Special Values</strong>
367 * <p><code>NaN</code> is represented as a single character, typically
368 * <code>\\uFFFD</code>. This character is determined by the
369 * DecimalFormatSymbols object. This is the only value for which
370 * the prefixes and suffixes are not used.
372 * <p>Infinity is represented as a single character, typically
373 * <code>\\u221E</code>, with the positive or negative prefixes and suffixes
374 * applied. The infinity character is determined by the
375 * DecimalFormatSymbols object.
377 * <a name="sci"><strong>Scientific Notation</strong></a>
379 * <p>Numbers in scientific notation are expressed as the product of a mantissa
380 * and a power of ten, for example, 1234 can be expressed as 1.234 x 10<sup>3</sup>. The
381 * mantissa is typically in the half-open interval [1.0, 10.0) or sometimes [0.0, 1.0),
382 * but it need not be. DecimalFormat supports arbitrary mantissas.
383 * DecimalFormat can be instructed to use scientific
384 * notation through the API or through the pattern. In a pattern, the exponent
385 * character immediately followed by one or more digit characters indicates
386 * scientific notation. Example: "0.###E0" formats the number 1234 as
390 * <li>The number of digit characters after the exponent character gives the
391 * minimum exponent digit count. There is no maximum. Negative exponents are
392 * formatted using the localized minus sign, <em>not</em> the prefix and suffix
393 * from the pattern. This allows patterns such as "0.###E0 m/s". To prefix
394 * positive exponents with a localized plus sign, specify '+' between the
395 * exponent and the digits: "0.###E+0" will produce formats "1E+1", "1E+0",
396 * "1E-1", etc. (In localized patterns, use the localized plus sign rather than
399 * <li>The minimum number of integer digits is achieved by adjusting the
400 * exponent. Example: 0.00123 formatted with "00.###E0" yields "12.3E-4". This
401 * only happens if there is no maximum number of integer digits. If there is a
402 * maximum, then the minimum number of integer digits is fixed at one.
404 * <li>The maximum number of integer digits, if present, specifies the exponent
405 * grouping. The most common use of this is to generate <em>engineering
406 * notation</em>, in which the exponent is a multiple of three, e.g.,
407 * "##0.###E0". The number 12345 is formatted using "##0.####E0" as "12.345E3".
409 * <li>When using scientific notation, the formatter controls the
410 * digit counts using significant digits logic. The maximum number of
411 * significant digits limits the total number of integer and fraction
412 * digits that will be shown in the mantissa; it does not affect
413 * parsing. For example, 12345 formatted with "##0.##E0" is "12.3E3".
414 * See the section on significant digits for more details.
416 * <li>The number of significant digits shown is determined as
417 * follows: If areSignificantDigitsUsed() returns false, then the
418 * minimum number of significant digits shown is one, and the maximum
419 * number of significant digits shown is the sum of the <em>minimum
420 * integer</em> and <em>maximum fraction</em> digits, and is
421 * unaffected by the maximum integer digits. If this sum is zero,
422 * then all significant digits are shown. If
423 * areSignificantDigitsUsed() returns true, then the significant digit
424 * counts are specified by getMinimumSignificantDigits() and
425 * getMaximumSignificantDigits(). In this case, the number of
426 * integer digits is fixed at one, and there is no exponent grouping.
428 * <li>Exponential patterns may not contain grouping separators.
431 * <a name="sigdig"><strong>Significant Digits</strong></a>
433 * <code>DecimalFormat</code> has two ways of controlling how many
434 * digits are shows: (a) significant digits counts, or (b) integer and
435 * fraction digit counts. Integer and fraction digit counts are
436 * described above. When a formatter is using significant digits
437 * counts, the number of integer and fraction digits is not specified
438 * directly, and the formatter settings for these counts are ignored.
439 * Instead, the formatter uses however many integer and fraction
440 * digits are required to display the specified number of significant
443 * <table border=0 cellspacing=3 cellpadding=0>
444 * <tr bgcolor="#ccccff">
445 * <td align=left>Pattern
446 * <td align=left>Minimum significant digits
447 * <td align=left>Maximum significant digits
448 * <td align=left>Number
449 * <td align=left>Output of format()
451 * <td><code>\@\@\@</code>
455 * <td><code>12300</code>
456 * <tr valign=top bgcolor="#eeeeff">
457 * <td><code>\@\@\@</code>
461 * <td><code>0.123</code>
463 * <td><code>\@\@##</code>
467 * <td><code>3.142</code>
468 * <tr valign=top bgcolor="#eeeeff">
469 * <td><code>\@\@##</code>
473 * <td><code>1.23</code>
477 * <li>Significant digit counts may be expressed using patterns that
478 * specify a minimum and maximum number of significant digits. These
479 * are indicated by the <code>'@'</code> and <code>'#'</code>
480 * characters. The minimum number of significant digits is the number
481 * of <code>'@'</code> characters. The maximum number of significant
482 * digits is the number of <code>'@'</code> characters plus the number
483 * of <code>'#'</code> characters following on the right. For
484 * example, the pattern <code>"@@@"</code> indicates exactly 3
485 * significant digits. The pattern <code>"@##"</code> indicates from
486 * 1 to 3 significant digits. Trailing zero digits to the right of
487 * the decimal separator are suppressed after the minimum number of
488 * significant digits have been shown. For example, the pattern
489 * <code>"@##"</code> formats the number 0.1203 as
490 * <code>"0.12"</code>.
492 * <li>If a pattern uses significant digits, it may not contain a
493 * decimal separator, nor the <code>'0'</code> pattern character.
494 * Patterns such as <code>"@00"</code> or <code>"@.###"</code> are
497 * <li>Any number of <code>'#'</code> characters may be prepended to
498 * the left of the leftmost <code>'@'</code> character. These have no
499 * effect on the minimum and maximum significant digits counts, but
500 * may be used to position grouping separators. For example,
501 * <code>"#,#@#"</code> indicates a minimum of one significant digits,
502 * a maximum of two significant digits, and a grouping size of three.
504 * <li>In order to enable significant digits formatting, use a pattern
505 * containing the <code>'@'</code> pattern character. Alternatively,
506 * call setSignificantDigitsUsed(TRUE).
508 * <li>In order to disable significant digits formatting, use a
509 * pattern that does not contain the <code>'@'</code> pattern
510 * character. Alternatively, call setSignificantDigitsUsed(FALSE).
512 * <li>The number of significant digits has no effect on parsing.
514 * <li>Significant digits may be used together with exponential notation. Such
515 * patterns are equivalent to a normal exponential pattern with a minimum and
516 * maximum integer digit count of one, a minimum fraction digit count of
517 * <code>getMinimumSignificantDigits() - 1</code>, and a maximum fraction digit
518 * count of <code>getMaximumSignificantDigits() - 1</code>. For example, the
519 * pattern <code>"@@###E0"</code> is equivalent to <code>"0.0###E0"</code>.
521 * <li>If signficant digits are in use, then the integer and fraction
522 * digit counts, as set via the API, are ignored. If significant
523 * digits are not in use, then the signficant digit counts, as set via
524 * the API, are ignored.
528 * <p><strong>Padding</strong>
530 * <p>DecimalFormat supports padding the result of
531 * format() to a specific width. Padding may be specified either
532 * through the API or through the pattern syntax. In a pattern the pad escape
533 * character, followed by a single pad character, causes padding to be parsed
534 * and formatted. The pad escape character is '*' in unlocalized patterns, and
535 * can be localized using DecimalFormatSymbols::setSymbol() with a
536 * DecimalFormatSymbols::kPadEscapeSymbol
537 * selector. For example, <code>"$*x#,##0.00"</code> formats 123 to
538 * <code>"$xx123.00"</code>, and 1234 to <code>"$1,234.00"</code>.
541 * <li>When padding is in effect, the width of the positive subpattern,
542 * including prefix and suffix, determines the format width. For example, in
543 * the pattern <code>"* #0 o''clock"</code>, the format width is 10.
545 * <li>The width is counted in 16-bit code units (UChars).
547 * <li>Some parameters which usually do not matter have meaning when padding is
548 * used, because the pattern width is significant with padding. In the pattern
549 * "* ##,##,#,##0.##", the format width is 14. The initial characters "##,##,"
550 * do not affect the grouping size or maximum integer digits, but they do affect
553 * <li>Padding may be inserted at one of four locations: before the prefix,
554 * after the prefix, before the suffix, or after the suffix. If padding is
555 * specified in any other location, applyPattern()
556 * sets a failing UErrorCode. If there is no prefix,
557 * before the prefix and after the prefix are equivalent, likewise for the
560 * <li>When specified in a pattern, the 32-bit code point immediately
561 * following the pad escape is the pad character. This may be any character,
562 * including a special pattern character. That is, the pad escape
563 * <em>escapes</em> the following character. If there is no character after
564 * the pad escape, then the pattern is illegal.
568 * <p><strong>Rounding</strong>
570 * <p>DecimalFormat supports rounding to a specific increment. For
571 * example, 1230 rounded to the nearest 50 is 1250. 1.234 rounded to the
572 * nearest 0.65 is 1.3. The rounding increment may be specified through the API
573 * or in a pattern. To specify a rounding increment in a pattern, include the
574 * increment in the pattern itself. "#,#50" specifies a rounding increment of
575 * 50. "#,##0.05" specifies a rounding increment of 0.05.
578 * <li>Rounding only affects the string produced by formatting. It does
579 * not affect parsing or change any numerical values.
581 * <li>A <em>rounding mode</em> determines how values are rounded; see
582 * DecimalFormat::ERoundingMode. Rounding increments specified in
583 * patterns use the default mode, DecimalFormat::kRoundHalfEven.
585 * <li>Some locales use rounding in their currency formats to reflect the
586 * smallest currency denomination.
588 * <li>In a pattern, digits '1' through '9' specify rounding, but otherwise
589 * behave identically to digit '0'.
592 * <p><strong>Synchronization</strong>
594 * <p>DecimalFormat objects are not synchronized. Multiple
595 * threads should not access one formatter concurrently.
597 * <p><strong>Subclassing</strong>
599 * <p><em>User subclasses are not supported.</em> While clients may write
600 * subclasses, such code will not necessarily work and will not be
601 * guaranteed to work stably from release to release.
603 class U_I18N_API DecimalFormat
: public NumberFormat
{
610 kRoundCeiling
, /**< Round towards positive infinity */
611 kRoundFloor
, /**< Round towards negative infinity */
612 kRoundDown
, /**< Round towards zero */
613 kRoundUp
, /**< Round away from zero */
614 kRoundHalfEven
, /**< Round towards the nearest integer, or
615 towards the nearest even integer if equidistant */
616 kRoundHalfDown
, /**< Round towards the nearest integer, or
617 towards zero if equidistant */
618 kRoundHalfUp
/**< Round towards the nearest integer, or
619 away from zero if equidistant */
620 // We don't support ROUND_UNNECESSARY
635 * Create a DecimalFormat using the default pattern and symbols
636 * for the default locale. This is a convenient way to obtain a
637 * DecimalFormat when internationalization is not the main concern.
639 * To obtain standard formats for a given locale, use the factory methods
640 * on NumberFormat such as createInstance. These factories will
641 * return the most appropriate sub-class of NumberFormat for a given
643 * @param status Output param set to success/failure code. If the
644 * pattern is invalid this will be set to a failure code.
647 DecimalFormat(UErrorCode
& status
);
650 * Create a DecimalFormat from the given pattern and the symbols
651 * for the default locale. This is a convenient way to obtain a
652 * DecimalFormat when internationalization is not the main concern.
654 * To obtain standard formats for a given locale, use the factory methods
655 * on NumberFormat such as createInstance. These factories will
656 * return the most appropriate sub-class of NumberFormat for a given
658 * @param pattern A non-localized pattern string.
659 * @param status Output param set to success/failure code. If the
660 * pattern is invalid this will be set to a failure code.
663 DecimalFormat(const UnicodeString
& pattern
,
667 * Create a DecimalFormat from the given pattern and symbols.
668 * Use this constructor when you need to completely customize the
669 * behavior of the format.
671 * To obtain standard formats for a given
672 * locale, use the factory methods on NumberFormat such as
673 * createInstance or createCurrencyInstance. If you need only minor adjustments
674 * to a standard format, you can modify the format returned by
675 * a NumberFormat factory method.
677 * @param pattern a non-localized pattern string
678 * @param symbolsToAdopt the set of symbols to be used. The caller should not
679 * delete this object after making this call.
680 * @param status Output param set to success/failure code. If the
681 * pattern is invalid this will be set to a failure code.
684 DecimalFormat( const UnicodeString
& pattern
,
685 DecimalFormatSymbols
* symbolsToAdopt
,
689 * Create a DecimalFormat from the given pattern and symbols.
690 * Use this constructor when you need to completely customize the
691 * behavior of the format.
693 * To obtain standard formats for a given
694 * locale, use the factory methods on NumberFormat such as
695 * createInstance or createCurrencyInstance. If you need only minor adjustments
696 * to a standard format, you can modify the format returned by
697 * a NumberFormat factory method.
699 * @param pattern a non-localized pattern string
700 * @param symbolsToAdopt the set of symbols to be used. The caller should not
701 * delete this object after making this call.
702 * @param parseError Output param to receive errors occured during parsing
703 * @param status Output param set to success/failure code. If the
704 * pattern is invalid this will be set to a failure code.
707 DecimalFormat( const UnicodeString
& pattern
,
708 DecimalFormatSymbols
* symbolsToAdopt
,
709 UParseError
& parseError
,
712 * Create a DecimalFormat from the given pattern and symbols.
713 * Use this constructor when you need to completely customize the
714 * behavior of the format.
716 * To obtain standard formats for a given
717 * locale, use the factory methods on NumberFormat such as
718 * createInstance or createCurrencyInstance. If you need only minor adjustments
719 * to a standard format, you can modify the format returned by
720 * a NumberFormat factory method.
722 * @param pattern a non-localized pattern string
723 * @param symbols the set of symbols to be used
724 * @param status Output param set to success/failure code. If the
725 * pattern is invalid this will be set to a failure code.
728 DecimalFormat( const UnicodeString
& pattern
,
729 const DecimalFormatSymbols
& symbols
,
735 * @param source the DecimalFormat object to be copied from.
738 DecimalFormat(const DecimalFormat
& source
);
741 * Assignment operator.
743 * @param rhs the DecimalFormat object to be copied.
746 DecimalFormat
& operator=(const DecimalFormat
& rhs
);
752 virtual ~DecimalFormat();
755 * Clone this Format object polymorphically. The caller owns the
756 * result and should delete it when done.
758 * @return a polymorphic copy of this DecimalFormat.
761 virtual Format
* clone(void) const;
764 * Return true if the given Format objects are semantically equal.
765 * Objects of different subclasses are considered unequal.
767 * @param other the object to be compared with.
768 * @return true if the given Format objects are semantically equal.
771 virtual UBool
operator==(const Format
& other
) const;
774 * Format a double or long number using base-10 representation.
776 * @param number The value to be formatted.
777 * @param appendTo Output parameter to receive result.
778 * Result is appended to existing contents.
779 * @param pos On input: an alignment field, if desired.
780 * On output: the offsets of the alignment field.
781 * @return Reference to 'appendTo' parameter.
784 virtual UnicodeString
& format(double number
,
785 UnicodeString
& appendTo
,
786 FieldPosition
& pos
) const;
788 * Format a long number using base-10 representation.
790 * @param number The value to be formatted.
791 * @param appendTo Output parameter to receive result.
792 * Result is appended to existing contents.
793 * @param pos On input: an alignment field, if desired.
794 * On output: the offsets of the alignment field.
795 * @return Reference to 'appendTo' parameter.
798 virtual UnicodeString
& format(int32_t number
,
799 UnicodeString
& appendTo
,
800 FieldPosition
& pos
) const;
802 * Format an int64 number using base-10 representation.
804 * @param number The value to be formatted.
805 * @param appendTo Output parameter to receive result.
806 * Result is appended to existing contents.
807 * @param pos On input: an alignment field, if desired.
808 * On output: the offsets of the alignment field.
809 * @return Reference to 'appendTo' parameter.
812 virtual UnicodeString
& format(int64_t number
,
813 UnicodeString
& appendTo
,
814 FieldPosition
& pos
) const;
817 * Format a Formattable using base-10 representation.
819 * @param obj The value to be formatted.
820 * @param appendTo Output parameter to receive result.
821 * Result is appended to existing contents.
822 * @param pos On input: an alignment field, if desired.
823 * On output: the offsets of the alignment field.
824 * @param status Error code indicating success or failure.
825 * @return Reference to 'appendTo' parameter.
828 virtual UnicodeString
& format(const Formattable
& obj
,
829 UnicodeString
& appendTo
,
831 UErrorCode
& status
) const;
834 * Redeclared NumberFormat method.
835 * Formats an object to produce a string.
837 * @param obj The object to format.
838 * @param appendTo Output parameter to receive result.
839 * Result is appended to existing contents.
840 * @param status Output parameter filled in with success or failure status.
841 * @return Reference to 'appendTo' parameter.
844 UnicodeString
& format(const Formattable
& obj
,
845 UnicodeString
& appendTo
,
846 UErrorCode
& status
) const;
849 * Redeclared NumberFormat method.
850 * Format a double number.
852 * @param number The value to be formatted.
853 * @param appendTo Output parameter to receive result.
854 * Result is appended to existing contents.
855 * @return Reference to 'appendTo' parameter.
858 UnicodeString
& format(double number
,
859 UnicodeString
& appendTo
) const;
862 * Redeclared NumberFormat method.
863 * Format a long number. These methods call the NumberFormat
864 * pure virtual format() methods with the default FieldPosition.
866 * @param number The value to be formatted.
867 * @param appendTo Output parameter to receive result.
868 * Result is appended to existing contents.
869 * @return Reference to 'appendTo' parameter.
872 UnicodeString
& format(int32_t number
,
873 UnicodeString
& appendTo
) const;
876 * Redeclared NumberFormat method.
877 * Format an int64 number. These methods call the NumberFormat
878 * pure virtual format() methods with the default FieldPosition.
880 * @param number The value to be formatted.
881 * @param appendTo Output parameter to receive result.
882 * Result is appended to existing contents.
883 * @return Reference to 'appendTo' parameter.
886 UnicodeString
& format(int64_t number
,
887 UnicodeString
& appendTo
) const;
889 * Parse the given string using this object's choices. The method
890 * does string comparisons to try to find an optimal match.
891 * If no object can be parsed, index is unchanged, and NULL is
892 * returned. The result is returned as the most parsimonious
893 * type of Formattable that will accomodate all of the
894 * necessary precision. For example, if the result is exactly 12,
895 * it will be returned as a long. However, if it is 1.5, it will
896 * be returned as a double.
898 * @param text The text to be parsed.
899 * @param result Formattable to be set to the parse result.
900 * If parse fails, return contents are undefined.
901 * @param parsePosition The position to start parsing at on input.
902 * On output, moved to after the last successfully
903 * parse character. On parse failure, does not change.
907 virtual void parse(const UnicodeString
& text
,
909 ParsePosition
& parsePosition
) const;
911 // Declare here again to get rid of function hiding problems.
913 * Parse the given string using this object's choices.
915 * @param text The text to be parsed.
916 * @param result Formattable to be set to the parse result.
917 * @param status Output parameter filled in with success or failure status.
920 virtual void parse(const UnicodeString
& text
,
922 UErrorCode
& status
) const;
925 * Parses text from the given string as a currency amount. Unlike
926 * the parse() method, this method will attempt to parse a generic
927 * currency name, searching for a match of this object's locale's
928 * currency display names, or for a 3-letter ISO currency code.
929 * This method will fail if this format is not a currency format,
930 * that is, if it does not contain the currency pattern symbol
931 * (U+00A4) in its prefix or suffix.
933 * @param text the string to parse
934 * @param result output parameter to receive result. This will have
935 * its currency set to the parsed ISO currency code.
936 * @param pos input-output position; on input, the position within
937 * text to match; must have 0 <= pos.getIndex() < text.length();
938 * on output, the position after the last matched character. If
939 * the parse fails, the position in unchanged upon output.
940 * @return a reference to result
943 virtual Formattable
& parseCurrency(const UnicodeString
& text
,
945 ParsePosition
& pos
) const;
948 * Returns the decimal format symbols, which is generally not changed
949 * by the programmer or user.
950 * @return desired DecimalFormatSymbols
951 * @see DecimalFormatSymbols
954 virtual const DecimalFormatSymbols
* getDecimalFormatSymbols(void) const;
957 * Sets the decimal format symbols, which is generally not changed
958 * by the programmer or user.
959 * @param symbolsToAdopt DecimalFormatSymbols to be adopted.
962 virtual void adoptDecimalFormatSymbols(DecimalFormatSymbols
* symbolsToAdopt
);
965 * Sets the decimal format symbols, which is generally not changed
966 * by the programmer or user.
967 * @param symbols DecimalFormatSymbols.
970 virtual void setDecimalFormatSymbols(const DecimalFormatSymbols
& symbols
);
974 * Get the positive prefix.
976 * @param result Output param which will receive the positive prefix.
977 * @return A reference to 'result'.
978 * Examples: +123, $123, sFr123
981 UnicodeString
& getPositivePrefix(UnicodeString
& result
) const;
984 * Set the positive prefix.
986 * @param newValue the new value of the the positive prefix to be set.
987 * Examples: +123, $123, sFr123
990 virtual void setPositivePrefix(const UnicodeString
& newValue
);
993 * Get the negative prefix.
995 * @param result Output param which will receive the negative prefix.
996 * @return A reference to 'result'.
997 * Examples: -123, ($123) (with negative suffix), sFr-123
1000 UnicodeString
& getNegativePrefix(UnicodeString
& result
) const;
1003 * Set the negative prefix.
1005 * @param newValue the new value of the the negative prefix to be set.
1006 * Examples: -123, ($123) (with negative suffix), sFr-123
1009 virtual void setNegativePrefix(const UnicodeString
& newValue
);
1012 * Get the positive suffix.
1014 * @param result Output param which will receive the positive suffix.
1015 * @return A reference to 'result'.
1019 UnicodeString
& getPositiveSuffix(UnicodeString
& result
) const;
1022 * Set the positive suffix.
1024 * @param newValue the new value of the positive suffix to be set.
1028 virtual void setPositiveSuffix(const UnicodeString
& newValue
);
1031 * Get the negative suffix.
1033 * @param result Output param which will receive the negative suffix.
1034 * @return A reference to 'result'.
1035 * Examples: -123%, ($123) (with positive suffixes)
1038 UnicodeString
& getNegativeSuffix(UnicodeString
& result
) const;
1041 * Set the negative suffix.
1043 * @param newValue the new value of the negative suffix to be set.
1047 virtual void setNegativeSuffix(const UnicodeString
& newValue
);
1050 * Get the multiplier for use in percent, permill, etc.
1051 * For a percentage, set the suffixes to have "%" and the multiplier to be 100.
1052 * (For Arabic, use arabic percent symbol).
1053 * For a permill, set the suffixes to have "\\u2031" and the multiplier to be 1000.
1055 * @return the multiplier for use in percent, permill, etc.
1056 * Examples: with 100, 1.23 -> "123", and "123" -> 1.23
1059 int32_t getMultiplier(void) const;
1062 * Set the multiplier for use in percent, permill, etc.
1063 * For a percentage, set the suffixes to have "%" and the multiplier to be 100.
1064 * (For Arabic, use arabic percent symbol).
1065 * For a permill, set the suffixes to have "\\u2031" and the multiplier to be 1000.
1067 * @param newValue the new value of the multiplier for use in percent, permill, etc.
1068 * Examples: with 100, 1.23 -> "123", and "123" -> 1.23
1071 virtual void setMultiplier(int32_t newValue
);
1074 * Get the rounding increment.
1075 * @return A positive rounding increment, or 0.0 if rounding
1077 * @see #setRoundingIncrement
1078 * @see #getRoundingMode
1079 * @see #setRoundingMode
1082 virtual double getRoundingIncrement(void) const;
1085 * Set the rounding increment. This method also controls whether
1086 * rounding is enabled.
1087 * @param newValue A positive rounding increment, or 0.0 to disable rounding.
1088 * Negative increments are equivalent to 0.0.
1089 * @see #getRoundingIncrement
1090 * @see #getRoundingMode
1091 * @see #setRoundingMode
1094 virtual void setRoundingIncrement(double newValue
);
1097 * Get the rounding mode.
1098 * @return A rounding mode
1099 * @see #setRoundingIncrement
1100 * @see #getRoundingIncrement
1101 * @see #setRoundingMode
1104 virtual ERoundingMode
getRoundingMode(void) const;
1107 * Set the rounding mode. This has no effect unless the rounding
1108 * increment is greater than zero.
1109 * @param roundingMode A rounding mode
1110 * @see #setRoundingIncrement
1111 * @see #getRoundingIncrement
1112 * @see #getRoundingMode
1115 virtual void setRoundingMode(ERoundingMode roundingMode
);
1118 * Get the width to which the output of format() is padded.
1119 * The width is counted in 16-bit code units.
1120 * @return the format width, or zero if no padding is in effect
1121 * @see #setFormatWidth
1122 * @see #getPadCharacterString
1123 * @see #setPadCharacter
1124 * @see #getPadPosition
1125 * @see #setPadPosition
1128 virtual int32_t getFormatWidth(void) const;
1131 * Set the width to which the output of format() is padded.
1132 * The width is counted in 16-bit code units.
1133 * This method also controls whether padding is enabled.
1134 * @param width the width to which to pad the result of
1135 * format(), or zero to disable padding. A negative
1136 * width is equivalent to 0.
1137 * @see #getFormatWidth
1138 * @see #getPadCharacterString
1139 * @see #setPadCharacter
1140 * @see #getPadPosition
1141 * @see #setPadPosition
1144 virtual void setFormatWidth(int32_t width
);
1147 * Get the pad character used to pad to the format width. The
1149 * @return a string containing the pad character. This will always
1150 * have a length of one 32-bit code point.
1151 * @see #setFormatWidth
1152 * @see #getFormatWidth
1153 * @see #setPadCharacter
1154 * @see #getPadPosition
1155 * @see #setPadPosition
1158 virtual UnicodeString
getPadCharacterString() const;
1161 * Set the character used to pad to the format width. If padding
1162 * is not enabled, then this will take effect if padding is later
1164 * @param padChar a string containing the pad charcter. If the string
1165 * has length 0, then the pad characer is set to ' '. Otherwise
1166 * padChar.char32At(0) will be used as the pad character.
1167 * @see #setFormatWidth
1168 * @see #getFormatWidth
1169 * @see #getPadCharacterString
1170 * @see #getPadPosition
1171 * @see #setPadPosition
1174 virtual void setPadCharacter(const UnicodeString
&padChar
);
1177 * Get the position at which padding will take place. This is the location
1178 * at which padding will be inserted if the result of format()
1179 * is shorter than the format width.
1180 * @return the pad position, one of kPadBeforePrefix,
1181 * kPadAfterPrefix, kPadBeforeSuffix, or
1183 * @see #setFormatWidth
1184 * @see #getFormatWidth
1185 * @see #setPadCharacter
1186 * @see #getPadCharacterString
1187 * @see #setPadPosition
1188 * @see #kPadBeforePrefix
1189 * @see #kPadAfterPrefix
1190 * @see #kPadBeforeSuffix
1191 * @see #kPadAfterSuffix
1194 virtual EPadPosition
getPadPosition(void) const;
1197 * Set the position at which padding will take place. This is the location
1198 * at which padding will be inserted if the result of format()
1199 * is shorter than the format width. This has no effect unless padding is
1201 * @param padPos the pad position, one of kPadBeforePrefix,
1202 * kPadAfterPrefix, kPadBeforeSuffix, or
1204 * @see #setFormatWidth
1205 * @see #getFormatWidth
1206 * @see #setPadCharacter
1207 * @see #getPadCharacterString
1208 * @see #getPadPosition
1209 * @see #kPadBeforePrefix
1210 * @see #kPadAfterPrefix
1211 * @see #kPadBeforeSuffix
1212 * @see #kPadAfterSuffix
1215 virtual void setPadPosition(EPadPosition padPos
);
1218 * Return whether or not scientific notation is used.
1219 * @return TRUE if this object formats and parses scientific notation
1220 * @see #setScientificNotation
1221 * @see #getMinimumExponentDigits
1222 * @see #setMinimumExponentDigits
1223 * @see #isExponentSignAlwaysShown
1224 * @see #setExponentSignAlwaysShown
1227 virtual UBool
isScientificNotation(void);
1230 * Set whether or not scientific notation is used. When scientific notation
1231 * is used, the effective maximum number of integer digits is <= 8. If the
1232 * maximum number of integer digits is set to more than 8, the effective
1233 * maximum will be 1. This allows this call to generate a 'default' scientific
1234 * number format without additional changes.
1235 * @param useScientific TRUE if this object formats and parses scientific
1237 * @see #isScientificNotation
1238 * @see #getMinimumExponentDigits
1239 * @see #setMinimumExponentDigits
1240 * @see #isExponentSignAlwaysShown
1241 * @see #setExponentSignAlwaysShown
1244 virtual void setScientificNotation(UBool useScientific
);
1247 * Return the minimum exponent digits that will be shown.
1248 * @return the minimum exponent digits that will be shown
1249 * @see #setScientificNotation
1250 * @see #isScientificNotation
1251 * @see #setMinimumExponentDigits
1252 * @see #isExponentSignAlwaysShown
1253 * @see #setExponentSignAlwaysShown
1256 virtual int8_t getMinimumExponentDigits(void) const;
1259 * Set the minimum exponent digits that will be shown. This has no
1260 * effect unless scientific notation is in use.
1261 * @param minExpDig a value >= 1 indicating the fewest exponent digits
1262 * that will be shown. Values less than 1 will be treated as 1.
1263 * @see #setScientificNotation
1264 * @see #isScientificNotation
1265 * @see #getMinimumExponentDigits
1266 * @see #isExponentSignAlwaysShown
1267 * @see #setExponentSignAlwaysShown
1270 virtual void setMinimumExponentDigits(int8_t minExpDig
);
1273 * Return whether the exponent sign is always shown.
1274 * @return TRUE if the exponent is always prefixed with either the
1275 * localized minus sign or the localized plus sign, false if only negative
1276 * exponents are prefixed with the localized minus sign.
1277 * @see #setScientificNotation
1278 * @see #isScientificNotation
1279 * @see #setMinimumExponentDigits
1280 * @see #getMinimumExponentDigits
1281 * @see #setExponentSignAlwaysShown
1284 virtual UBool
isExponentSignAlwaysShown(void);
1287 * Set whether the exponent sign is always shown. This has no effect
1288 * unless scientific notation is in use.
1289 * @param expSignAlways TRUE if the exponent is always prefixed with either
1290 * the localized minus sign or the localized plus sign, false if only
1291 * negative exponents are prefixed with the localized minus sign.
1292 * @see #setScientificNotation
1293 * @see #isScientificNotation
1294 * @see #setMinimumExponentDigits
1295 * @see #getMinimumExponentDigits
1296 * @see #isExponentSignAlwaysShown
1299 virtual void setExponentSignAlwaysShown(UBool expSignAlways
);
1302 * Return the grouping size. Grouping size is the number of digits between
1303 * grouping separators in the integer portion of a number. For example,
1304 * in the number "123,456.78", the grouping size is 3.
1306 * @return the grouping size.
1307 * @see setGroupingSize
1308 * @see NumberFormat::isGroupingUsed
1309 * @see DecimalFormatSymbols::getGroupingSeparator
1312 int32_t getGroupingSize(void) const;
1315 * Set the grouping size. Grouping size is the number of digits between
1316 * grouping separators in the integer portion of a number. For example,
1317 * in the number "123,456.78", the grouping size is 3.
1319 * @param newValue the new value of the grouping size.
1320 * @see getGroupingSize
1321 * @see NumberFormat::setGroupingUsed
1322 * @see DecimalFormatSymbols::setGroupingSeparator
1325 virtual void setGroupingSize(int32_t newValue
);
1328 * Return the secondary grouping size. In some locales one
1329 * grouping interval is used for the least significant integer
1330 * digits (the primary grouping size), and another is used for all
1331 * others (the secondary grouping size). A formatter supporting a
1332 * secondary grouping size will return a positive integer unequal
1333 * to the primary grouping size returned by
1334 * getGroupingSize(). For example, if the primary
1335 * grouping size is 4, and the secondary grouping size is 2, then
1336 * the number 123456789 formats as "1,23,45,6789", and the pattern
1337 * appears as "#,##,###0".
1338 * @return the secondary grouping size, or a value less than
1339 * one if there is none
1340 * @see setSecondaryGroupingSize
1341 * @see NumberFormat::isGroupingUsed
1342 * @see DecimalFormatSymbols::getGroupingSeparator
1345 int32_t getSecondaryGroupingSize(void) const;
1348 * Set the secondary grouping size. If set to a value less than 1,
1349 * then secondary grouping is turned off, and the primary grouping
1350 * size is used for all intervals, not just the least significant.
1352 * @param newValue the new value of the secondary grouping size.
1353 * @see getSecondaryGroupingSize
1354 * @see NumberFormat#setGroupingUsed
1355 * @see DecimalFormatSymbols::setGroupingSeparator
1358 virtual void setSecondaryGroupingSize(int32_t newValue
);
1361 * Allows you to get the behavior of the decimal separator with integers.
1362 * (The decimal separator will always appear with decimals.)
1364 * @return TRUE if the decimal separator always appear with decimals.
1365 * Example: Decimal ON: 12345 -> 12345.; OFF: 12345 -> 12345
1368 UBool
isDecimalSeparatorAlwaysShown(void) const;
1371 * Allows you to set the behavior of the decimal separator with integers.
1372 * (The decimal separator will always appear with decimals.)
1374 * @param newValue set TRUE if the decimal separator will always appear with decimals.
1375 * Example: Decimal ON: 12345 -> 12345.; OFF: 12345 -> 12345
1378 virtual void setDecimalSeparatorAlwaysShown(UBool newValue
);
1381 * Synthesizes a pattern string that represents the current state
1382 * of this Format object.
1384 * @param result Output param which will receive the pattern.
1385 * Previous contents are deleted.
1386 * @return A reference to 'result'.
1390 virtual UnicodeString
& toPattern(UnicodeString
& result
) const;
1393 * Synthesizes a localized pattern string that represents the current
1394 * state of this Format object.
1396 * @param result Output param which will receive the localized pattern.
1397 * Previous contents are deleted.
1398 * @return A reference to 'result'.
1402 virtual UnicodeString
& toLocalizedPattern(UnicodeString
& result
) const;
1405 * Apply the given pattern to this Format object. A pattern is a
1406 * short-hand specification for the various formatting properties.
1407 * These properties can also be changed individually through the
1408 * various setter methods.
1410 * There is no limit to integer digits are set
1411 * by this routine, since that is the typical end-user desire;
1412 * use setMaximumInteger if you want to set a real value.
1413 * For negative numbers, use a second pattern, separated by a semicolon
1415 * . Example "#,#00.0#" -> 1,234.56
1417 * This means a minimum of 2 integer digits, 1 fraction digit, and
1418 * a maximum of 2 fraction digits.
1420 * . Example: "#,#00.0#;(#,#00.0#)" for negatives in parantheses.
1422 * In negative patterns, the minimum and maximum counts are ignored;
1423 * these are presumed to be set in the positive pattern.
1425 * @param pattern The pattern to be applied.
1426 * @param parseError Struct to recieve information on position
1427 * of error if an error is encountered
1428 * @param status Output param set to success/failure code on
1429 * exit. If the pattern is invalid, this will be
1430 * set to a failure result.
1433 virtual void applyPattern(const UnicodeString
& pattern
,
1434 UParseError
& parseError
,
1435 UErrorCode
& status
);
1438 * @param pattern The pattern to be applied.
1439 * @param status Output param set to success/failure code on
1440 * exit. If the pattern is invalid, this will be
1441 * set to a failure result.
1444 virtual void applyPattern(const UnicodeString
& pattern
,
1445 UErrorCode
& status
);
1448 * Apply the given pattern to this Format object. The pattern
1449 * is assumed to be in a localized notation. A pattern is a
1450 * short-hand specification for the various formatting properties.
1451 * These properties can also be changed individually through the
1452 * various setter methods.
1454 * There is no limit to integer digits are set
1455 * by this routine, since that is the typical end-user desire;
1456 * use setMaximumInteger if you want to set a real value.
1457 * For negative numbers, use a second pattern, separated by a semicolon
1459 * . Example "#,#00.0#" -> 1,234.56
1461 * This means a minimum of 2 integer digits, 1 fraction digit, and
1462 * a maximum of 2 fraction digits.
1464 * Example: "#,#00.0#;(#,#00.0#)" for negatives in parantheses.
1466 * In negative patterns, the minimum and maximum counts are ignored;
1467 * these are presumed to be set in the positive pattern.
1469 * @param pattern The localized pattern to be applied.
1470 * @param parseError Struct to recieve information on position
1471 * of error if an error is encountered
1472 * @param status Output param set to success/failure code on
1473 * exit. If the pattern is invalid, this will be
1474 * set to a failure result.
1477 virtual void applyLocalizedPattern(const UnicodeString
& pattern
,
1478 UParseError
& parseError
,
1479 UErrorCode
& status
);
1482 * Apply the given pattern to this Format object.
1484 * @param pattern The localized pattern to be applied.
1485 * @param status Output param set to success/failure code on
1486 * exit. If the pattern is invalid, this will be
1487 * set to a failure result.
1490 virtual void applyLocalizedPattern(const UnicodeString
& pattern
,
1491 UErrorCode
& status
);
1495 * Sets the maximum number of digits allowed in the integer portion of a
1496 * number. This override limits the integer digit count to 309.
1498 * @param newValue the new value of the maximum number of digits
1499 * allowed in the integer portion of a number.
1500 * @see NumberFormat#setMaximumIntegerDigits
1503 virtual void setMaximumIntegerDigits(int32_t newValue
);
1506 * Sets the minimum number of digits allowed in the integer portion of a
1507 * number. This override limits the integer digit count to 309.
1509 * @param newValue the new value of the minimum number of digits
1510 * allowed in the integer portion of a number.
1511 * @see NumberFormat#setMinimumIntegerDigits
1514 virtual void setMinimumIntegerDigits(int32_t newValue
);
1517 * Sets the maximum number of digits allowed in the fraction portion of a
1518 * number. This override limits the fraction digit count to 340.
1520 * @param newValue the new value of the maximum number of digits
1521 * allowed in the fraction portion of a number.
1522 * @see NumberFormat#setMaximumFractionDigits
1525 virtual void setMaximumFractionDigits(int32_t newValue
);
1528 * Sets the minimum number of digits allowed in the fraction portion of a
1529 * number. This override limits the fraction digit count to 340.
1531 * @param newValue the new value of the minimum number of digits
1532 * allowed in the fraction portion of a number.
1533 * @see NumberFormat#setMinimumFractionDigits
1536 virtual void setMinimumFractionDigits(int32_t newValue
);
1539 * Returns the minimum number of significant digits that will be
1540 * displayed. This value has no effect unless areSignificantDigitsUsed()
1542 * @return the fewest significant digits that will be shown
1545 int32_t getMinimumSignificantDigits() const;
1548 * Returns the maximum number of significant digits that will be
1549 * displayed. This value has no effect unless areSignificantDigitsUsed()
1551 * @return the most significant digits that will be shown
1554 int32_t getMaximumSignificantDigits() const;
1557 * Sets the minimum number of significant digits that will be
1558 * displayed. If <code>min</code> is less than one then it is set
1559 * to one. If the maximum significant digits count is less than
1560 * <code>min</code>, then it is set to <code>min</code>. This
1561 * value has no effect unless areSignificantDigits() returns true.
1562 * @param min the fewest significant digits to be shown
1565 void setMinimumSignificantDigits(int32_t min
);
1568 * Sets the maximum number of significant digits that will be
1569 * displayed. If <code>max</code> is less than one then it is set
1570 * to one. If the minimum significant digits count is greater
1571 * than <code>max</code>, then it is set to <code>max</code>.
1572 * This value has no effect unless areSignificantDigits() returns
1574 * @param max the most significant digits to be shown
1577 void setMaximumSignificantDigits(int32_t max
);
1580 * Returns true if significant digits are in use, or false if
1581 * integer and fraction digit counts are in use.
1582 * @return true if significant digits are in use
1585 UBool
areSignificantDigitsUsed() const;
1588 * Sets whether significant digits are in use, or integer and
1589 * fraction digit counts are in use.
1590 * @param useSignificantDigits true to use significant digits, or
1591 * false to use integer and fraction digit counts
1594 void setSignificantDigitsUsed(UBool useSignificantDigits
);
1598 * Sets the currency used to display currency
1599 * amounts. This takes effect immediately, if this format is a
1600 * currency format. If this format is not a currency format, then
1601 * the currency is used if and when this object becomes a
1602 * currency format through the application of a new pattern.
1603 * @param theCurrency a 3-letter ISO code indicating new currency
1604 * to use. It need not be null-terminated. May be the empty
1605 * string or NULL to indicate no currency.
1606 * @param ec input-output error code
1609 virtual void setCurrency(const UChar
* theCurrency
, UErrorCode
& ec
);
1612 * Sets the currency used to display currency amounts. See
1613 * setCurrency(const UChar*, UErrorCode&).
1614 * @deprecated ICU 3.0. Use setCurrency(const UChar*, UErrorCode&).
1616 virtual void setCurrency(const UChar
* theCurrency
);
1619 * The resource tags we use to retrieve decimal format data from
1620 * locale resource bundles.
1621 * @deprecated ICU 3.4. This string has no public purpose. Please don't use it.
1623 static const char fgNumberPatterns
[];
1628 * Return the class ID for this class. This is useful only for
1629 * comparing to a return value from getDynamicClassID(). For example:
1631 * . Base* polymorphic_pointer = createPolymorphicObject();
1632 * . if (polymorphic_pointer->getDynamicClassID() ==
1633 * . Derived::getStaticClassID()) ...
1635 * @return The class ID for all objects of this class.
1638 static UClassID U_EXPORT2
getStaticClassID(void);
1641 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override.
1642 * This method is to implement a simple version of RTTI, since not all
1643 * C++ compilers support genuine RTTI. Polymorphic operator==() and
1644 * clone() methods call this method.
1646 * @return The class ID for this object. All objects of a
1647 * given class have the same class ID. Objects of
1648 * other classes have different class IDs.
1651 virtual UClassID
getDynamicClassID(void) const;
1654 DecimalFormat(); // default constructor not implemented
1656 int32_t precision(UBool isIntegral
) const;
1659 * Do real work of constructing a new DecimalFormat.
1661 void construct(UErrorCode
& status
,
1662 UParseError
& parseErr
,
1663 const UnicodeString
* pattern
= 0,
1664 DecimalFormatSymbols
* symbolsToAdopt
= 0
1668 * Does the real work of generating a pattern.
1670 * @param result Output param which will receive the pattern.
1671 * Previous contents are deleted.
1672 * @param localized TRUE return localized pattern.
1673 * @return A reference to 'result'.
1675 UnicodeString
& toPattern(UnicodeString
& result
, UBool localized
) const;
1678 * Does the real work of applying a pattern.
1679 * @param pattern The pattern to be applied.
1680 * @param localized If true, the pattern is localized; else false.
1681 * @param parseError Struct to recieve information on position
1682 * of error if an error is encountered
1683 * @param status Output param set to success/failure code on
1684 * exit. If the pattern is invalid, this will be
1685 * set to a failure result.
1687 void applyPattern(const UnicodeString
& pattern
,
1689 UParseError
& parseError
,
1690 UErrorCode
& status
);
1692 * Do the work of formatting a number, either a double or a long.
1694 * @param appendTo Output parameter to receive result.
1695 * Result is appended to existing contents.
1696 * @param fieldPosition On input: an alignment field, if desired.
1697 * On output: the offsets of the alignment field.
1698 * @param digits the digits to be formatted.
1699 * @param isInteger if TRUE format the digits as Integer.
1700 * @return Reference to 'appendTo' parameter.
1702 UnicodeString
& subformat(UnicodeString
& appendTo
,
1703 FieldPosition
& fieldPosition
,
1705 UBool isInteger
) const;
1707 void parse(const UnicodeString
& text
,
1708 Formattable
& result
,
1710 UBool parseCurrency
) const;
1714 fgStatusLength
// Leave last in list.
1717 UBool
subparse(const UnicodeString
& text
, ParsePosition
& parsePosition
,
1718 DigitList
& digits
, UBool
* status
,
1719 UChar
* currency
) const;
1721 int32_t skipPadding(const UnicodeString
& text
, int32_t position
) const;
1723 int32_t compareAffix(const UnicodeString
& input
,
1727 UChar
* currency
) const;
1729 static int32_t compareSimpleAffix(const UnicodeString
& affix
,
1730 const UnicodeString
& input
,
1733 static int32_t skipRuleWhiteSpace(const UnicodeString
& text
, int32_t pos
);
1735 static int32_t skipUWhiteSpace(const UnicodeString
& text
, int32_t pos
);
1737 int32_t compareComplexAffix(const UnicodeString
& affixPat
,
1738 const UnicodeString
& input
,
1740 UChar
* currency
) const;
1742 static int32_t match(const UnicodeString
& text
, int32_t pos
, UChar32 ch
);
1744 static int32_t match(const UnicodeString
& text
, int32_t pos
, const UnicodeString
& str
);
1747 * Get a decimal format symbol.
1748 * Returns a const reference to the symbol string.
1751 inline const UnicodeString
&getConstSymbol(DecimalFormatSymbols::ENumberFormatSymbol symbol
) const;
1753 int32_t appendAffix(UnicodeString
& buf
, double number
,
1754 UBool isNegative
, UBool isPrefix
) const;
1757 * Append an affix to the given UnicodeString, using quotes if
1758 * there are special characters. Single quotes themselves must be
1759 * escaped in either case.
1761 void appendAffixPattern(UnicodeString
& appendTo
, const UnicodeString
& affix
,
1762 UBool localized
) const;
1764 void appendAffixPattern(UnicodeString
& appendTo
,
1765 const UnicodeString
* affixPattern
,
1766 const UnicodeString
& expAffix
, UBool localized
) const;
1768 void expandAffix(const UnicodeString
& pattern
,
1769 UnicodeString
& affix
,
1771 UBool doFormat
) const;
1773 void expandAffixes();
1775 static double round(double a
, ERoundingMode mode
, UBool isNegative
);
1777 void addPadding(UnicodeString
& appendTo
,
1778 FieldPosition
& fieldPosition
,
1779 int32_t prefixLen
, int32_t suffixLen
) const;
1781 UBool
isGroupingPosition(int32_t pos
) const;
1783 void setCurrencyForSymbols();
1788 //static const int8_t fgMaxDigit; // The largest digit, in this case 9
1790 /*transient*/ //DigitList* fDigitList;
1792 UnicodeString fPositivePrefix
;
1793 UnicodeString fPositiveSuffix
;
1794 UnicodeString fNegativePrefix
;
1795 UnicodeString fNegativeSuffix
;
1796 UnicodeString
* fPosPrefixPattern
;
1797 UnicodeString
* fPosSuffixPattern
;
1798 UnicodeString
* fNegPrefixPattern
;
1799 UnicodeString
* fNegSuffixPattern
;
1802 * Formatter for ChoiceFormat-based currency names. If this field
1803 * is not null, then delegate to it to format currency symbols.
1806 ChoiceFormat
* fCurrencyChoice
;
1808 int32_t fMultiplier
;
1809 int32_t fGroupingSize
;
1810 int32_t fGroupingSize2
;
1811 UBool fDecimalSeparatorAlwaysShown
;
1812 /*transient*/ UBool fIsCurrencyFormat
;
1813 DecimalFormatSymbols
* fSymbols
;
1815 UBool fUseSignificantDigits
;
1816 int32_t fMinSignificantDigits
;
1817 int32_t fMaxSignificantDigits
;
1819 UBool fUseExponentialNotation
;
1820 int8_t fMinExponentDigits
;
1821 UBool fExponentSignAlwaysShown
;
1823 /* If fRoundingIncrement is NULL, there is no rounding. Otherwise, round to
1824 * fRoundingIncrement.getDouble(). Since this operation may be expensive,
1825 * we cache the result in fRoundingDouble. All methods that update
1826 * fRoundingIncrement also update fRoundingDouble. */
1827 DigitList
* fRoundingIncrement
;
1828 /*transient*/ double fRoundingDouble
;
1829 ERoundingMode fRoundingMode
;
1832 int32_t fFormatWidth
;
1833 EPadPosition fPadPosition
;
1838 * Returns the currency in effect for this formatter. Subclasses
1839 * should override this method as needed. Unlike getCurrency(),
1840 * this method should never return "".
1841 * @result output parameter for null-terminated result, which must
1842 * have a capacity of at least 4
1845 virtual void getEffectiveCurrency(UChar
* result
, UErrorCode
& ec
) const;
1847 /** number of integer digits
1850 static const int32_t kDoubleIntegerDigits
;
1851 /** number of fraction digits
1854 static const int32_t kDoubleFractionDigits
;
1857 * When someone turns on scientific mode, we assume that more than this
1858 * number of digits is due to flipping from some other mode that didn't
1859 * restrict the maximum, and so we force 1 integer digit. We don't bother
1860 * to track and see if someone is using exponential notation with more than
1861 * this number, it wouldn't make sense anyway, and this is just to make sure
1862 * that someone turning on scientific mode with default settings doesn't
1863 * end up with lots of zeroes.
1866 static const int32_t kMaxScientificIntegerDigits
;
1869 inline UnicodeString
&
1870 DecimalFormat::format(const Formattable
& obj
,
1871 UnicodeString
& appendTo
,
1872 UErrorCode
& status
) const {
1873 // Don't use Format:: - use immediate base class only,
1874 // in case immediate base modifies behavior later.
1875 return NumberFormat::format(obj
, appendTo
, status
);
1878 inline UnicodeString
&
1879 DecimalFormat::format(double number
,
1880 UnicodeString
& appendTo
) const {
1881 FieldPosition
pos(0);
1882 return format(number
, appendTo
, pos
);
1885 inline UnicodeString
&
1886 DecimalFormat::format(int32_t number
,
1887 UnicodeString
& appendTo
) const {
1888 FieldPosition
pos(0);
1889 return format((int64_t)number
, appendTo
, pos
);
1892 inline const UnicodeString
&
1893 DecimalFormat::getConstSymbol(DecimalFormatSymbols::ENumberFormatSymbol symbol
) const {
1894 return fSymbols
->getConstSymbol(symbol
);
1899 #endif /* #if !UCONFIG_NO_FORMATTING */