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