Debian Codegraph @

intl/icu/source/i18n/unicode/decimfmt.h

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