2 ********************************************************************************
3 * Copyright (C) 1997-2013, International Business Machines Corporation and others.
5 ********************************************************************************
9 * Modification History:
11 * Date Name Description
12 * 02/19/97 aliu Converted from java.
13 * 03/18/97 clhuang Updated per C++ implementation.
14 * 04/17/97 aliu Changed DigitCount to int per code review.
15 * 07/20/98 stephen JDK 1.2 sync up. Added scientific support.
16 * Changed naming conventions to match C++ guidelines
17 * Derecated Java style constants (eg, INTEGER_FIELD)
18 ********************************************************************************
25 #include "unicode/utypes.h"
29 * \brief C++ API: Abstract base class for all number formats.
32 #if !UCONFIG_NO_FORMATTING
34 #include "unicode/unistr.h"
35 #include "unicode/format.h"
36 #include "unicode/unum.h" // UNumberFormatStyle
37 #include "unicode/locid.h"
38 #include "unicode/stringpiece.h"
39 #include "unicode/curramt.h"
41 class NumberFormatTest
;
45 #if !UCONFIG_NO_SERVICE
46 class NumberFormatFactory
;
47 class StringEnumeration
;
52 * Abstract base class for all number formats. Provides interface for
53 * formatting and parsing a number. Also provides methods for
54 * determining which locales have number formats, and what their names
57 * NumberFormat helps you to format and parse numbers for any locale.
58 * Your code can be completely independent of the locale conventions
59 * for decimal points, thousands-separators, or even the particular
60 * decimal digits used, or whether the number format is even decimal.
62 * To format a number for the current Locale, use one of the static
66 * double myNumber = 7.0;
67 * UnicodeString myString;
68 * UErrorCode success = U_ZERO_ERROR;
69 * NumberFormat* nf = NumberFormat::createInstance(success)
70 * nf->format(myNumber, myString);
71 * cout << " Example 1: " << myString << endl;
74 * Note that there are additional factory methods within subclasses of
77 * If you are formatting multiple numbers, it is more efficient to get
78 * the format and use it multiple times so that the system doesn't
79 * have to fetch the information about the local language and country
80 * conventions multiple times.
83 * UnicodeString myString;
84 * UErrorCode success = U_ZERO_ERROR;
85 * nf = NumberFormat::createInstance( success );
86 * int32_t a[] = { 123, 3333, -1234567 };
87 * const int32_t a_len = sizeof(a) / sizeof(a[0]);
89 * for (int32_t i = 0; i < a_len; i++) {
90 * nf->format(a[i], myString);
93 * cout << " Example 2: " << myString << endl;
96 * To format a number for a different Locale, specify it in the
97 * call to createInstance().
100 * nf = NumberFormat::createInstance( Locale::FRENCH, success );
103 * You can use a NumberFormat to parse also.
106 * UErrorCode success;
107 * Formattable result(-999); // initialized with error code
108 * nf->parse(myString, result, success);
111 * Use createInstance to get the normal number format for that country.
112 * There are other static factory methods available. Use getCurrency
113 * to get the currency number format for that country. Use getPercent
114 * to get a format for displaying percentages. With this format, a
115 * fraction from 0.53 is displayed as 53%.
117 * Starting from ICU 4.2, you can use createInstance() by passing in a 'style'
118 * as parameter to get the correct instance.
120 * use createInstance(...kNumberStyle...) to get the normal number format,
121 * createInstance(...kPercentStyle...) to get a format for displaying
123 * createInstance(...kScientificStyle...) to get a format for displaying
125 * createInstance(...kCurrencyStyle...) to get the currency number format,
126 * in which the currency is represented by its symbol, for example, "$3.00".
127 * createInstance(...kIsoCurrencyStyle...) to get the currency number format,
128 * in which the currency is represented by its ISO code, for example "USD3.00".
129 * createInstance(...kPluralCurrencyStyle...) to get the currency number format,
130 * in which the currency is represented by its full name in plural format,
131 * for example, "3.00 US dollars" or "1.00 US dollar".
133 * You can also control the display of numbers with such methods as
134 * getMinimumFractionDigits. If you want even more control over the
135 * format or parsing, or want to give your users more control, you can
136 * try casting the NumberFormat you get from the factory methods to a
137 * DecimalNumberFormat. This will work for the vast majority of
138 * countries; just remember to put it in a try block in case you
139 * encounter an unusual one.
141 * You can also use forms of the parse and format methods with
142 * ParsePosition and FieldPosition to allow you to:
144 * <li>(a) progressively parse through pieces of a string.
145 * <li>(b) align the decimal point and other areas.
147 * For example, you can align numbers in two ways.
149 * If you are using a monospaced font with spacing for alignment, you
150 * can pass the FieldPosition in your format call, with field =
151 * INTEGER_FIELD. On output, getEndIndex will be set to the offset
152 * between the last character of the integer and the decimal. Add
153 * (desiredSpaceCount - getEndIndex) spaces at the front of the
156 * If you are using proportional fonts, instead of padding with
157 * spaces, measure the width of the string in pixels from the start to
158 * getEndIndex. Then move the pen by (desiredPixelWidth -
159 * widthToAlignmentPoint) before drawing the text. It also works
160 * where there is no decimal, but possibly additional characters at
161 * the end, e.g. with parentheses in negative numbers: "(12)" for -12.
163 * <em>User subclasses are not supported.</em> While clients may write
164 * subclasses, such code will not necessarily work and will not be
165 * guaranteed to work stably from release to release.
169 class U_I18N_API NumberFormat
: public Format
{
172 * Alignment Field constants used to construct a FieldPosition object.
173 * Signifies that the position of the integer part or fraction part of
174 * a formatted number should be returned.
176 * Note: as of ICU 4.4, the values in this enum have been extended to
177 * support identification of all number format fields, not just those
178 * pertaining to alignment.
180 * These constants are provided for backwards compatibility only.
181 * Please use the C style constants defined in the header file unum.h.
186 enum EAlignmentFields
{
187 /** @stable ICU 2.0 */
188 kIntegerField
= UNUM_INTEGER_FIELD
,
189 /** @stable ICU 2.0 */
190 kFractionField
= UNUM_FRACTION_FIELD
,
191 /** @stable ICU 2.0 */
192 kDecimalSeparatorField
= UNUM_DECIMAL_SEPARATOR_FIELD
,
193 /** @stable ICU 2.0 */
194 kExponentSymbolField
= UNUM_EXPONENT_SYMBOL_FIELD
,
195 /** @stable ICU 2.0 */
196 kExponentSignField
= UNUM_EXPONENT_SIGN_FIELD
,
197 /** @stable ICU 2.0 */
198 kExponentField
= UNUM_EXPONENT_FIELD
,
199 /** @stable ICU 2.0 */
200 kGroupingSeparatorField
= UNUM_GROUPING_SEPARATOR_FIELD
,
201 /** @stable ICU 2.0 */
202 kCurrencyField
= UNUM_CURRENCY_FIELD
,
203 /** @stable ICU 2.0 */
204 kPercentField
= UNUM_PERCENT_FIELD
,
205 /** @stable ICU 2.0 */
206 kPermillField
= UNUM_PERMILL_FIELD
,
207 /** @stable ICU 2.0 */
208 kSignField
= UNUM_SIGN_FIELD
,
211 * These constants are provided for backwards compatibility only.
212 * Please use the constants defined in the header file unum.h.
214 /** @stable ICU 2.0 */
215 INTEGER_FIELD
= UNUM_INTEGER_FIELD
,
216 /** @stable ICU 2.0 */
217 FRACTION_FIELD
= UNUM_FRACTION_FIELD
224 virtual ~NumberFormat();
227 * Return true if the given Format objects are semantically equal.
228 * Objects of different subclasses are considered unequal.
229 * @return true if the given Format objects are semantically equal.
232 virtual UBool
operator==(const Format
& other
) const;
235 using Format::format
;
238 * Format an object to produce a string. This method handles
239 * Formattable objects with numeric types. If the Formattable
240 * object type is not a numeric type, then it returns a failing
243 * @param obj The object to format.
244 * @param appendTo Output parameter to receive result.
245 * Result is appended to existing contents.
246 * @param pos On input: an alignment field, if desired.
247 * On output: the offsets of the alignment field.
248 * @param status Output param filled with success/failure status.
249 * @return Reference to 'appendTo' parameter.
252 virtual UnicodeString
& format(const Formattable
& obj
,
253 UnicodeString
& appendTo
,
255 UErrorCode
& status
) const;
258 * Format an object to produce a string. This method handles
259 * Formattable objects with numeric types. If the Formattable
260 * object type is not a numeric type, then it returns a failing
263 * @param obj The object to format.
264 * @param appendTo Output parameter to receive result.
265 * Result is appended to existing contents.
266 * @param posIter On return, can be used to iterate over positions
267 * of fields generated by this format call. Can be
269 * @param status Output param filled with success/failure status.
270 * @return Reference to 'appendTo' parameter.
273 virtual UnicodeString
& format(const Formattable
& obj
,
274 UnicodeString
& appendTo
,
275 FieldPositionIterator
* posIter
,
276 UErrorCode
& status
) const;
279 * Parse a string to produce an object. This methods handles
280 * parsing of numeric strings into Formattable objects with numeric
283 * Before calling, set parse_pos.index to the offset you want to
284 * start parsing at in the source. After calling, parse_pos.index
285 * indicates the position after the successfully parsed text. If
286 * an error occurs, parse_pos.index is unchanged.
288 * When parsing, leading whitespace is discarded (with successful
289 * parse), while trailing whitespace is left as is.
291 * See Format::parseObject() for more.
293 * @param source The string to be parsed into an object.
294 * @param result Formattable to be set to the parse result.
295 * If parse fails, return contents are undefined.
296 * @param parse_pos The position to start parsing at. Upon return
297 * this param is set to the position after the
298 * last character successfully parsed. If the
299 * source is not parsed successfully, this param
300 * will remain unchanged.
301 * @return A newly created Formattable* object, or NULL
302 * on failure. The caller owns this and should
303 * delete it when done.
306 virtual void parseObject(const UnicodeString
& source
,
308 ParsePosition
& parse_pos
) const;
311 * Format a double number. These methods call the NumberFormat
312 * pure virtual format() methods with the default FieldPosition.
314 * @param number The value to be formatted.
315 * @param appendTo Output parameter to receive result.
316 * Result is appended to existing contents.
317 * @return Reference to 'appendTo' parameter.
320 UnicodeString
& format( double number
,
321 UnicodeString
& appendTo
) const;
324 * Format a long number. These methods call the NumberFormat
325 * pure virtual format() methods with the default FieldPosition.
327 * @param number The value to be formatted.
328 * @param appendTo Output parameter to receive result.
329 * Result is appended to existing contents.
330 * @return Reference to 'appendTo' parameter.
333 UnicodeString
& format( int32_t number
,
334 UnicodeString
& appendTo
) const;
337 * Format an int64 number. These methods call the NumberFormat
338 * pure virtual format() methods with the default FieldPosition.
340 * @param number The value to be formatted.
341 * @param appendTo Output parameter to receive result.
342 * Result is appended to existing contents.
343 * @return Reference to 'appendTo' parameter.
346 UnicodeString
& format( int64_t number
,
347 UnicodeString
& appendTo
) const;
350 * Format a double number. Concrete subclasses must implement
351 * these pure virtual methods.
353 * @param number The value to be formatted.
354 * @param appendTo Output parameter to receive result.
355 * Result is appended to existing contents.
356 * @param pos On input: an alignment field, if desired.
357 * On output: the offsets of the alignment field.
358 * @return Reference to 'appendTo' parameter.
361 virtual UnicodeString
& format(double number
,
362 UnicodeString
& appendTo
,
363 FieldPosition
& pos
) const = 0;
365 * Format a double number. By default, the parent function simply
366 * calls the base class and does not return an error status.
367 * Therefore, the status may be ignored in some subclasses.
369 * @param number The value to be formatted.
370 * @param appendTo Output parameter to receive result.
371 * Result is appended to existing contents.
372 * @param pos On input: an alignment field, if desired.
373 * On output: the offsets of the alignment field.
374 * @param status error status
375 * @return Reference to 'appendTo' parameter.
378 virtual UnicodeString
& format(double number
,
379 UnicodeString
& appendTo
,
381 UErrorCode
&status
) const;
383 * Format a double number. Subclasses must implement
386 * @param number The value to be formatted.
387 * @param appendTo Output parameter to receive result.
388 * Result is appended to existing contents.
389 * @param posIter On return, can be used to iterate over positions
390 * of fields generated by this format call.
392 * @param status Output param filled with success/failure status.
393 * @return Reference to 'appendTo' parameter.
396 virtual UnicodeString
& format(double number
,
397 UnicodeString
& appendTo
,
398 FieldPositionIterator
* posIter
,
399 UErrorCode
& status
) const;
401 * Format a long number. Concrete subclasses must implement
402 * these pure virtual methods.
404 * @param number The value to be formatted.
405 * @param appendTo Output parameter to receive result.
406 * Result is appended to existing contents.
407 * @param pos On input: an alignment field, if desired.
408 * On output: the offsets of the alignment field.
409 * @return Reference to 'appendTo' parameter.
412 virtual UnicodeString
& format(int32_t number
,
413 UnicodeString
& appendTo
,
414 FieldPosition
& pos
) const = 0;
417 * Format a long number. Concrete subclasses may override
418 * this function to provide status return.
420 * @param number The value to be formatted.
421 * @param appendTo Output parameter to receive result.
422 * Result is appended to existing contents.
423 * @param pos On input: an alignment field, if desired.
424 * On output: the offsets of the alignment field.
425 * @param status the output status.
426 * @return Reference to 'appendTo' parameter.
429 virtual UnicodeString
& format(int32_t number
,
430 UnicodeString
& appendTo
,
432 UErrorCode
&status
) const;
435 * Format an int32 number. Subclasses must implement
438 * @param number The value to be formatted.
439 * @param appendTo Output parameter to receive result.
440 * Result is appended to existing contents.
441 * @param posIter On return, can be used to iterate over positions
442 * of fields generated by this format call.
444 * @param status Output param filled with success/failure status.
445 * @return Reference to 'appendTo' parameter.
448 virtual UnicodeString
& format(int32_t number
,
449 UnicodeString
& appendTo
,
450 FieldPositionIterator
* posIter
,
451 UErrorCode
& status
) const;
453 * Format an int64 number. (Not abstract to retain compatibility
454 * with earlier releases, however subclasses should override this
455 * method as it just delegates to format(int32_t number...);
457 * @param number The value to be formatted.
458 * @param appendTo Output parameter to receive result.
459 * Result is appended to existing contents.
460 * @param pos On input: an alignment field, if desired.
461 * On output: the offsets of the alignment field.
462 * @return Reference to 'appendTo' parameter.
465 virtual UnicodeString
& format(int64_t number
,
466 UnicodeString
& appendTo
,
467 FieldPosition
& pos
) const;
470 * Format an int64 number. (Not abstract to retain compatibility
471 * with earlier releases, however subclasses should override this
472 * method as it just delegates to format(int32_t number...);
474 * @param number The value to be formatted.
475 * @param appendTo Output parameter to receive result.
476 * Result is appended to existing contents.
477 * @param pos On input: an alignment field, if desired.
478 * On output: the offsets of the alignment field.
479 * @return Reference to 'appendTo' parameter.
482 virtual UnicodeString
& format(int64_t number
,
483 UnicodeString
& appendTo
,
485 UErrorCode
& status
) const;
487 * Format an int64 number. Subclasses must implement
490 * @param number The value to be formatted.
491 * @param appendTo Output parameter to receive result.
492 * Result is appended to existing contents.
493 * @param posIter On return, can be used to iterate over positions
494 * of fields generated by this format call.
496 * @param status Output param filled with success/failure status.
497 * @return Reference to 'appendTo' parameter.
500 virtual UnicodeString
& format(int64_t number
,
501 UnicodeString
& appendTo
,
502 FieldPositionIterator
* posIter
,
503 UErrorCode
& status
) const;
506 * Format a decimal number. Subclasses must implement
507 * this method. The syntax of the unformatted number is a "numeric string"
508 * as defined in the Decimal Arithmetic Specification, available at
509 * http://speleotrove.com/decimal
511 * @param number The unformatted number, as a string, to be formatted.
512 * @param appendTo Output parameter to receive result.
513 * Result is appended to existing contents.
514 * @param posIter On return, can be used to iterate over positions
515 * of fields generated by this format call.
517 * @param status Output param filled with success/failure status.
518 * @return Reference to 'appendTo' parameter.
521 virtual UnicodeString
& format(const StringPiece
&number
,
522 UnicodeString
& appendTo
,
523 FieldPositionIterator
* posIter
,
524 UErrorCode
& status
) const;
527 * Format a decimal number.
528 * The number is a DigitList wrapper onto a floating point decimal number.
529 * The default implementation in NumberFormat converts the decimal number
530 * to a double and formats that. Subclasses of NumberFormat that want
531 * to specifically handle big decimal numbers must override this method.
532 * class DecimalFormat does so.
534 * @param number The number, a DigitList format Decimal Floating Point.
535 * @param appendTo Output parameter to receive result.
536 * Result is appended to existing contents.
537 * @param posIter On return, can be used to iterate over positions
538 * of fields generated by this format call.
539 * @param status Output param filled with success/failure status.
540 * @return Reference to 'appendTo' parameter.
543 virtual UnicodeString
& format(const DigitList
&number
,
544 UnicodeString
& appendTo
,
545 FieldPositionIterator
* posIter
,
546 UErrorCode
& status
) const;
549 * Format a decimal number.
550 * The number is a DigitList wrapper onto a floating point decimal number.
551 * The default implementation in NumberFormat converts the decimal number
552 * to a double and formats that. Subclasses of NumberFormat that want
553 * to specifically handle big decimal numbers must override this method.
554 * class DecimalFormat does so.
556 * @param number The number, a DigitList format Decimal Floating Point.
557 * @param appendTo Output parameter to receive result.
558 * Result is appended to existing contents.
559 * @param pos On input: an alignment field, if desired.
560 * On output: the offsets of the alignment field.
561 * @param status Output param filled with success/failure status.
562 * @return Reference to 'appendTo' parameter.
565 virtual UnicodeString
& format(const DigitList
&number
,
566 UnicodeString
& appendTo
,
568 UErrorCode
& status
) const;
573 * Redeclared Format method.
574 * @param obj The object to be formatted.
575 * @param appendTo Output parameter to receive result.
576 * Result is appended to existing contents.
577 * @param status Output parameter set to a failure error code
578 * when a failure occurs.
579 * @return Reference to 'appendTo' parameter.
582 UnicodeString
& format(const Formattable
& obj
,
583 UnicodeString
& appendTo
,
584 UErrorCode
& status
) const;
587 * Return a long if possible (e.g. within range LONG_MAX,
588 * LONG_MAX], and with no decimals), otherwise a double. If
589 * IntegerOnly is set, will stop at a decimal point (or equivalent;
590 * e.g. for rational numbers "1 2/3", will stop after the 1).
592 * If no object can be parsed, index is unchanged, and NULL is
595 * This is a pure virtual which concrete subclasses must implement.
597 * @param text The text to be parsed.
598 * @param result Formattable to be set to the parse result.
599 * If parse fails, return contents are undefined.
600 * @param parsePosition The position to start parsing at on input.
601 * On output, moved to after the last successfully
602 * parse character. On parse failure, does not change.
605 virtual void parse(const UnicodeString
& text
,
607 ParsePosition
& parsePosition
) const = 0;
610 * Parse a string as a numeric value, and return a Formattable
611 * numeric object. This method parses integers only if IntegerOnly
614 * @param text The text to be parsed.
615 * @param result Formattable to be set to the parse result.
616 * If parse fails, return contents are undefined.
617 * @param status Output parameter set to a failure error code
618 * when a failure occurs.
619 * @see NumberFormat::isParseIntegerOnly
622 virtual void parse(const UnicodeString
& text
,
624 UErrorCode
& status
) const;
627 * Parses text from the given string as a currency amount. Unlike
628 * the parse() method, this method will attempt to parse a generic
629 * currency name, searching for a match of this object's locale's
630 * currency display names, or for a 3-letter ISO currency code.
631 * This method will fail if this format is not a currency format,
632 * that is, if it does not contain the currency pattern symbol
633 * (U+00A4) in its prefix or suffix.
635 * @param text the string to parse
636 * @param pos input-output position; on input, the position within text
637 * to match; must have 0 <= pos.getIndex() < text.length();
638 * on output, the position after the last matched character.
639 * If the parse fails, the position in unchanged upon output.
640 * @return if parse succeeds, a pointer to a newly-created CurrencyAmount
641 * object (owned by the caller) containing information about
642 * the parsed currency; if parse fails, this is NULL.
645 virtual CurrencyAmount
* parseCurrency(const UnicodeString
& text
,
646 ParsePosition
& pos
) const;
649 * Return true if this format will parse numbers as integers
650 * only. For example in the English locale, with ParseIntegerOnly
651 * true, the string "1234." would be parsed as the integer value
652 * 1234 and parsing would stop at the "." character. Of course,
653 * the exact format accepted by the parse operation is locale
654 * dependant and determined by sub-classes of NumberFormat.
655 * @return true if this format will parse numbers as integers
659 UBool
isParseIntegerOnly(void) const;
662 * Sets whether or not numbers should be parsed as integers only.
663 * @param value set True, this format will parse numbers as integers
665 * @see isParseIntegerOnly
668 virtual void setParseIntegerOnly(UBool value
);
671 * Sets whether lenient parsing should be enabled (it is off by default).
673 * @param enable <code>TRUE</code> if lenient parsing should be used,
674 * <code>FALSE</code> otherwise.
677 virtual void setLenient(UBool enable
);
680 * Returns whether lenient parsing is enabled (it is off by default).
682 * @return <code>TRUE</code> if lenient parsing is enabled,
683 * <code>FALSE</code> otherwise.
687 virtual UBool
isLenient(void) const;
690 * Returns the default number format for the current default
691 * locale. The default format is one of the styles provided by
692 * the other factory methods: getNumberInstance,
693 * getCurrencyInstance or getPercentInstance. Exactly which one
694 * is locale dependant.
697 static NumberFormat
* U_EXPORT2
createInstance(UErrorCode
&);
700 * Returns the default number format for the specified locale.
701 * The default format is one of the styles provided by the other
702 * factory methods: getNumberInstance, getCurrencyInstance or
703 * getPercentInstance. Exactly which one is locale dependant.
704 * @param inLocale the given locale.
707 static NumberFormat
* U_EXPORT2
createInstance(const Locale
& inLocale
,
711 * Creates the specified decimal format style of the desired locale.
712 * @param desiredLocale the given locale.
713 * @param style the given style.
714 * @param errorCode Output param filled with success/failure status.
715 * @return A new NumberFormat instance.
718 static NumberFormat
* U_EXPORT2
createInstance(const Locale
& desiredLocale
,
719 UNumberFormatStyle style
,
720 UErrorCode
& errorCode
);
723 * Returns a currency format for the current default locale.
726 static NumberFormat
* U_EXPORT2
createCurrencyInstance(UErrorCode
&);
729 * Returns a currency format for the specified locale.
730 * @param inLocale the given locale.
733 static NumberFormat
* U_EXPORT2
createCurrencyInstance(const Locale
& inLocale
,
737 * Returns a percentage format for the current default locale.
740 static NumberFormat
* U_EXPORT2
createPercentInstance(UErrorCode
&);
743 * Returns a percentage format for the specified locale.
744 * @param inLocale the given locale.
747 static NumberFormat
* U_EXPORT2
createPercentInstance(const Locale
& inLocale
,
751 * Returns a scientific format for the current default locale.
754 static NumberFormat
* U_EXPORT2
createScientificInstance(UErrorCode
&);
757 * Returns a scientific format for the specified locale.
758 * @param inLocale the given locale.
761 static NumberFormat
* U_EXPORT2
createScientificInstance(const Locale
& inLocale
,
765 * Get the set of Locales for which NumberFormats are installed.
766 * @param count Output param to receive the size of the locales
769 static const Locale
* U_EXPORT2
getAvailableLocales(int32_t& count
);
771 #if !UCONFIG_NO_SERVICE
773 * Register a new NumberFormatFactory. The factory will be adopted.
774 * @param toAdopt the NumberFormatFactory instance to be adopted
775 * @param status the in/out status code, no special meanings are assigned
776 * @return a registry key that can be used to unregister this factory
779 static URegistryKey U_EXPORT2
registerFactory(NumberFormatFactory
* toAdopt
, UErrorCode
& status
);
782 * Unregister a previously-registered NumberFormatFactory using the key returned from the
783 * register call. Key becomes invalid after a successful call and should not be used again.
784 * The NumberFormatFactory corresponding to the key will be deleted.
785 * @param key the registry key returned by a previous call to registerFactory
786 * @param status the in/out status code, no special meanings are assigned
787 * @return TRUE if the factory for the key was successfully unregistered
790 static UBool U_EXPORT2
unregister(URegistryKey key
, UErrorCode
& status
);
793 * Return a StringEnumeration over the locales available at the time of the call,
794 * including registered locales.
795 * @return a StringEnumeration over the locales available at the time of the call
798 static StringEnumeration
* U_EXPORT2
getAvailableLocales(void);
799 #endif /* UCONFIG_NO_SERVICE */
802 * Returns true if grouping is used in this format. For example,
803 * in the English locale, with grouping on, the number 1234567
804 * might be formatted as "1,234,567". The grouping separator as
805 * well as the size of each group is locale dependant and is
806 * determined by sub-classes of NumberFormat.
807 * @see setGroupingUsed
810 UBool
isGroupingUsed(void) const;
813 * Set whether or not grouping will be used in this format.
814 * @param newValue True, grouping will be used in this format.
815 * @see getGroupingUsed
818 virtual void setGroupingUsed(UBool newValue
);
821 * Returns the maximum number of digits allowed in the integer portion of a
823 * @return the maximum number of digits allowed in the integer portion of a
825 * @see setMaximumIntegerDigits
828 int32_t getMaximumIntegerDigits(void) const;
831 * Sets the maximum number of digits allowed in the integer portion of a
832 * number. maximumIntegerDigits must be >= minimumIntegerDigits. If the
833 * new value for maximumIntegerDigits is less than the current value
834 * of minimumIntegerDigits, then minimumIntegerDigits will also be set to
837 * @param newValue the new value for the maximum number of digits
838 * allowed in the integer portion of a number.
839 * @see getMaximumIntegerDigits
842 virtual void setMaximumIntegerDigits(int32_t newValue
);
845 * Returns the minimum number of digits allowed in the integer portion of a
847 * @return the minimum number of digits allowed in the integer portion of a
849 * @see setMinimumIntegerDigits
852 int32_t getMinimumIntegerDigits(void) const;
855 * Sets the minimum number of digits allowed in the integer portion of a
856 * number. minimumIntegerDigits must be <= maximumIntegerDigits. If the
857 * new value for minimumIntegerDigits exceeds the current value
858 * of maximumIntegerDigits, then maximumIntegerDigits will also be set to
860 * @param newValue the new value to be set.
861 * @see getMinimumIntegerDigits
864 virtual void setMinimumIntegerDigits(int32_t newValue
);
867 * Returns the maximum number of digits allowed in the fraction portion of a
869 * @return the maximum number of digits allowed in the fraction portion of a
871 * @see setMaximumFractionDigits
874 int32_t getMaximumFractionDigits(void) const;
877 * Sets the maximum number of digits allowed in the fraction portion of a
878 * number. maximumFractionDigits must be >= minimumFractionDigits. If the
879 * new value for maximumFractionDigits is less than the current value
880 * of minimumFractionDigits, then minimumFractionDigits will also be set to
882 * @param newValue the new value to be set.
883 * @see getMaximumFractionDigits
886 virtual void setMaximumFractionDigits(int32_t newValue
);
889 * Returns the minimum number of digits allowed in the fraction portion of a
891 * @return the minimum number of digits allowed in the fraction portion of a
893 * @see setMinimumFractionDigits
896 int32_t getMinimumFractionDigits(void) const;
899 * Sets the minimum number of digits allowed in the fraction portion of a
900 * number. minimumFractionDigits must be <= maximumFractionDigits. If the
901 * new value for minimumFractionDigits exceeds the current value
902 * of maximumFractionDigits, then maximumIntegerDigits will also be set to
904 * @param newValue the new value to be set.
905 * @see getMinimumFractionDigits
908 virtual void setMinimumFractionDigits(int32_t newValue
);
911 * Sets the currency used to display currency
912 * amounts. This takes effect immediately, if this format is a
913 * currency format. If this format is not a currency format, then
914 * the currency is used if and when this object becomes a
916 * @param theCurrency a 3-letter ISO code indicating new currency
917 * to use. It need not be null-terminated. May be the empty
918 * string or NULL to indicate no currency.
919 * @param ec input-output error code
922 virtual void setCurrency(const UChar
* theCurrency
, UErrorCode
& ec
);
925 * Gets the currency used to display currency
926 * amounts. This may be an empty string for some subclasses.
927 * @return a 3-letter null-terminated ISO code indicating
928 * the currency in use, or a pointer to the empty string.
931 const UChar
* getCurrency() const;
936 * Return the class ID for this class. This is useful for
937 * comparing to a return value from getDynamicClassID(). Note that,
938 * because NumberFormat is an abstract base class, no fully constructed object
939 * will have the class ID returned by NumberFormat::getStaticClassID().
940 * @return The class ID for all objects of this class.
943 static UClassID U_EXPORT2
getStaticClassID(void);
946 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override.
947 * This method is to implement a simple version of RTTI, since not all
948 * C++ compilers support genuine RTTI. Polymorphic operator==() and
949 * clone() methods call this method.
951 * @return The class ID for this object. All objects of a
952 * given class have the same class ID. Objects of
953 * other classes have different class IDs.
956 virtual UClassID
getDynamicClassID(void) const = 0;
961 * Default constructor for subclass use only.
970 NumberFormat(const NumberFormat
&);
973 * Assignment operator.
976 NumberFormat
& operator=(const NumberFormat
&);
979 * Returns the currency in effect for this formatter. Subclasses
980 * should override this method as needed. Unlike getCurrency(),
981 * this method should never return "".
982 * @result output parameter for null-terminated result, which must
983 * have a capacity of at least 4
986 virtual void getEffectiveCurrency(UChar
* result
, UErrorCode
& ec
) const;
988 #ifndef U_HIDE_INTERNAL_API
990 * Creates the specified number format style of the desired locale.
991 * If mustBeDecimalFormat is TRUE, then the returned pointer is
992 * either a DecimalFormat or it is NULL.
995 static NumberFormat
* makeInstance(const Locale
& desiredLocale
,
996 UNumberFormatStyle style
,
997 UBool mustBeDecimalFormat
,
998 UErrorCode
& errorCode
);
999 #endif /* U_HIDE_INTERNAL_API */
1003 static UBool
isStyleSupported(UNumberFormatStyle style
);
1006 * Creates the specified decimal format style of the desired locale.
1007 * @param desiredLocale the given locale.
1008 * @param style the given style.
1009 * @param errorCode Output param filled with success/failure status.
1010 * @return A new NumberFormat instance.
1012 static NumberFormat
* makeInstance(const Locale
& desiredLocale
,
1013 UNumberFormatStyle style
,
1014 UErrorCode
& errorCode
);
1016 UBool fGroupingUsed
;
1017 int32_t fMaxIntegerDigits
;
1018 int32_t fMinIntegerDigits
;
1019 int32_t fMaxFractionDigits
;
1020 int32_t fMinFractionDigits
;
1021 UBool fParseIntegerOnly
;
1022 UBool fLenient
; // TRUE => lenient parse is enabled
1024 // ISO currency code
1027 friend class ICUNumberFormatFactory
; // access to makeInstance
1028 friend class ICUNumberFormatService
;
1029 friend class ::NumberFormatTest
; // access to isStyleSupported()
1032 #if !UCONFIG_NO_SERVICE
1034 * A NumberFormatFactory is used to register new number formats. The factory
1035 * should be able to create any of the predefined formats for each locale it
1036 * supports. When registered, the locales it supports extend or override the
1037 * locale already supported by ICU.
1041 class U_I18N_API NumberFormatFactory
: public UObject
{
1048 virtual ~NumberFormatFactory();
1051 * Return true if this factory will be visible. Default is true.
1052 * If not visible, the locales supported by this factory will not
1053 * be listed by getAvailableLocales.
1056 virtual UBool
visible(void) const = 0;
1059 * Return the locale names directly supported by this factory. The number of names
1060 * is returned in count;
1063 virtual const UnicodeString
* getSupportedIDs(int32_t &count
, UErrorCode
& status
) const = 0;
1066 * Return a number format of the appropriate type. If the locale
1067 * is not supported, return null. If the locale is supported, but
1068 * the type is not provided by this service, return null. Otherwise
1069 * return an appropriate instance of NumberFormat.
1072 virtual NumberFormat
* createFormat(const Locale
& loc
, UNumberFormatStyle formatType
) = 0;
1076 * A NumberFormatFactory that supports a single locale. It can be visible or invisible.
1079 class U_I18N_API SimpleNumberFormatFactory
: public NumberFormatFactory
{
1082 * True if the locale supported by this factory is visible.
1085 const UBool _visible
;
1088 * The locale supported by this factory, as a UnicodeString.
1097 SimpleNumberFormatFactory(const Locale
& locale
, UBool visible
= TRUE
);
1102 virtual ~SimpleNumberFormatFactory();
1107 virtual UBool
visible(void) const;
1112 virtual const UnicodeString
* getSupportedIDs(int32_t &count
, UErrorCode
& status
) const;
1114 #endif /* #if !UCONFIG_NO_SERVICE */
1116 // -------------------------------------
1119 NumberFormat::isParseIntegerOnly() const
1121 return fParseIntegerOnly
;
1125 NumberFormat::isLenient() const
1130 inline UnicodeString
&
1131 NumberFormat::format(const Formattable
& obj
,
1132 UnicodeString
& appendTo
,
1133 UErrorCode
& status
) const {
1134 return Format::format(obj
, appendTo
, status
);
1139 #endif /* #if !UCONFIG_NO_FORMATTING */