ICU 55.1 55.1

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- Data Structures
- Data Structure Index
- Class Hierarchy
- Data Fields

All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Static Protected Attributes | Friends
icu::NumberFormat Class Reference

Abstract base class for all number formats. More...

`#include <numfmt.h>`

Inheritance diagram for icu::NumberFormat:

## Public Types
enum | EAlignmentFields { kIntegerField = UNUM_INTEGER_FIELD, kFractionField = UNUM_FRACTION_FIELD, kDecimalSeparatorField = UNUM_DECIMAL_SEPARATOR_FIELD, kExponentSymbolField = UNUM_EXPONENT_SYMBOL_FIELD, kExponentSignField = UNUM_EXPONENT_SIGN_FIELD, kExponentField = UNUM_EXPONENT_FIELD, kGroupingSeparatorField = UNUM_GROUPING_SEPARATOR_FIELD, kCurrencyField = UNUM_CURRENCY_FIELD, kPercentField = UNUM_PERCENT_FIELD, kPermillField = UNUM_PERMILL_FIELD, kSignField = UNUM_SIGN_FIELD, INTEGER_FIELD = UNUM_INTEGER_FIELD, FRACTION_FIELD = UNUM_FRACTION_FIELD }
| Alignment Field constants used to construct a FieldPosition object. More...

## Public Member Functions
virtual | ~NumberFormat ()
| Destructor.
virtual UBool | operator== (const Format &other) const
| Return true if the given Format objects are semantically equal.
virtual UnicodeString & | format (const Formattable &obj, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
| Format an object to produce a string.
virtual UnicodeString & | format (const Formattable &obj, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const
| Format an object to produce a string.
virtual void | parseObject (const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const
| Parse a string to produce an object.
UnicodeString & | format (double number, UnicodeString &appendTo) const
| Format a double number.
UnicodeString & | format (int32_t number, UnicodeString &appendTo) const
| Format a long number.
UnicodeString & | format (int64_t number, UnicodeString &appendTo) const
| Format an int64 number.
virtual UnicodeString & | format (double number, UnicodeString &appendTo, FieldPosition &pos) const =0
| Format a double number.
virtual UnicodeString & | format (double number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
| Format a double number.
virtual UnicodeString & | format (double number, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const
| Format a double number.
virtual UnicodeString & | format (int32_t number, UnicodeString &appendTo, FieldPosition &pos) const =0
| Format a long number.
virtual UnicodeString & | format (int32_t number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
| Format a long number.
virtual UnicodeString & | format (int32_t number, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const
| Format an int32 number.
virtual UnicodeString & | format (int64_t number, UnicodeString &appendTo, FieldPosition &pos) const
| Format an int64 number.
virtual UnicodeString & | format (int64_t number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
| Format an int64 number.
virtual UnicodeString & | format (int64_t number, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const
| Format an int64 number.
virtual UnicodeString & | format (const StringPiece &number, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const
| Format a decimal number.
virtual UnicodeString & | format (const DigitList &number, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const
| Format a decimal number.
virtual UnicodeString & | format (const DigitList &number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
| Format a decimal number.
virtual void | parse (const UnicodeString &text, Formattable &result, ParsePosition &parsePosition) const =0
| Return a long if possible (e.g.
virtual void | parse (const UnicodeString &text, Formattable &result, UErrorCode &status) const
| Parse a string as a numeric value, and return a Formattable numeric object.
virtual CurrencyAmount * | parseCurrency (const UnicodeString &text, ParsePosition &pos) const
| Parses text from the given string as a currency amount.
UBool | isParseIntegerOnly (void) const
| Return true if this format will parse numbers as integers only.
virtual void | setParseIntegerOnly (UBool value)
| Sets whether or not numbers should be parsed as integers only.
virtual void | setLenient (UBool enable)
| Sets whether lenient parsing should be enabled (it is off by default).
virtual UBool | isLenient (void) const
| Returns whether lenient parsing is enabled (it is off by default).
UBool | isGroupingUsed (void) const
| Returns true if grouping is used in this format.
virtual void | setGroupingUsed (UBool newValue)
| Set whether or not grouping will be used in this format.
int32_t | getMaximumIntegerDigits (void) const
| Returns the maximum number of digits allowed in the integer portion of a number.
virtual void | setMaximumIntegerDigits (int32_t newValue)
| Sets the maximum number of digits allowed in the integer portion of a number.
int32_t | getMinimumIntegerDigits (void) const
| Returns the minimum number of digits allowed in the integer portion of a number.
virtual void | setMinimumIntegerDigits (int32_t newValue)
| Sets the minimum number of digits allowed in the integer portion of a number.
int32_t | getMaximumFractionDigits (void) const
| Returns the maximum number of digits allowed in the fraction portion of a number.
virtual void | setMaximumFractionDigits (int32_t newValue)
| Sets the maximum number of digits allowed in the fraction portion of a number.
int32_t | getMinimumFractionDigits (void) const
| Returns the minimum number of digits allowed in the fraction portion of a number.
virtual void | setMinimumFractionDigits (int32_t newValue)
| Sets the minimum number of digits allowed in the fraction portion of a number.
virtual void | setCurrency (const UChar *theCurrency, UErrorCode &ec)
| Sets the currency used to display currency amounts.
const UChar * | getCurrency () const
| Gets the currency used to display currency amounts.
virtual void | setContext (UDisplayContext value, UErrorCode &status)
| Set a particular UDisplayContext value in the formatter, such as UDISPCTX_CAPITALIZATION_FOR_STANDALONE.
virtual UDisplayContext | getContext (UDisplayContextType type, UErrorCode &status) const
| Get the formatter's UDisplayContext value for the specified UDisplayContextType, such as UDISPCTX_TYPE_CAPITALIZATION.
virtual UClassID | getDynamicClassID (void) const =0
| Returns a unique class ID POLYMORPHICALLY.
Public Member Functions inherited from icu::Format
virtual | ~Format ()
| Destructor.
UBool | operator!= (const Format &other) const
| Return true if the given Format objects are not semantically equal.
virtual Format * | clone () const =0
| Clone this object polymorphically.
UnicodeString & | format (const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
| Formats an object to produce a string.
void | parseObject (const UnicodeString &source, Formattable &result, UErrorCode &status) const
| Parses a string to produce an object.
Locale | getLocale (ULocDataLocaleType type, UErrorCode &status) const
| Get the locale for this format object.
const char * | getLocaleID (ULocDataLocaleType type, UErrorCode &status) const
| Get the locale for this format object.
Public Member Functions inherited from icu::UObject
virtual | ~UObject ()
| Destructor.

## Static Public Member Functions
static NumberFormat * | createInstance (UErrorCode &)
| Returns the default number format for the current default locale.
static NumberFormat * | createInstance (const Locale &inLocale, UErrorCode &)
| Returns the default number format for the specified locale.
static NumberFormat * | createInstance (const Locale &desiredLocale, UNumberFormatStyle style, UErrorCode &errorCode)
| Creates the specified decimal format style of the desired locale.
static NumberFormat * | internalCreateInstance (const Locale &desiredLocale, UNumberFormatStyle style, UErrorCode &errorCode)
| ICU use only.
static const SharedNumberFormat * | createSharedInstance (const Locale &inLocale, UNumberFormatStyle style, UErrorCode &status)
| ICU use only.
static NumberFormat * | createCurrencyInstance (UErrorCode &)
| Returns a currency format for the current default locale.
static NumberFormat * | createCurrencyInstance (const Locale &inLocale, UErrorCode &)
| Returns a currency format for the specified locale.
static NumberFormat * | createPercentInstance (UErrorCode &)
| Returns a percentage format for the current default locale.
static NumberFormat * | createPercentInstance (const Locale &inLocale, UErrorCode &)
| Returns a percentage format for the specified locale.
static NumberFormat * | createScientificInstance (UErrorCode &)
| Returns a scientific format for the current default locale.
static NumberFormat * | createScientificInstance (const Locale &inLocale, UErrorCode &)
| Returns a scientific format for the specified locale.
static const Locale * | getAvailableLocales (int32_t &count)
| Get the set of Locales for which NumberFormats are installed.
static URegistryKey | registerFactory (NumberFormatFactory *toAdopt, UErrorCode &status)
| Register a new NumberFormatFactory.
static UBool | unregister (URegistryKey key, UErrorCode &status)
| Unregister a previously-registered NumberFormatFactory using the key returned from the register call.
static StringEnumeration * | getAvailableLocales (void)
| Return a StringEnumeration over the locales available at the time of the call, including registered locales.
static UClassID | getStaticClassID (void)
| Return the class ID for this class.

## Protected Member Functions
| NumberFormat ()
| Default constructor for subclass use only.
| NumberFormat (const NumberFormat &)
| Copy constructor.
NumberFormat & | operator= (const NumberFormat &)
| Assignment operator.
virtual void | getEffectiveCurrency (UChar *result, UErrorCode &ec) const
| Returns the currency in effect for this formatter.
Protected Member Functions inherited from icu::Format
void | setLocaleIDs (const char *valid, const char *actual)
| Format ()
| Default constructor for subclass use only.
| Format (const Format &)
Format & | operator= (const Format &)

## Static Protected Member Functions
static NumberFormat * | makeInstance (const Locale &desiredLocale, UNumberFormatStyle style, UBool mustBeDecimalFormat, UErrorCode &errorCode)
| Creates the specified number format style of the desired locale.
Static Protected Member Functions inherited from icu::Format
static void | syntaxError (const UnicodeString &pattern, int32_t pos, UParseError &parseError)
| Simple function for initializing a UParseError from a UnicodeString.

## Static Protected Attributes
static const int32_t | gDefaultMaxIntegerDigits
static const int32_t | gDefaultMinIntegerDigits

## Friends
class | ICUNumberFormatFactory
class | ICUNumberFormatService
class | ::NumberFormatTest

## Detailed Description

Abstract base class for all number formats.

Provides interface for formatting and parsing a number. Also provides methods for determining which locales have number formats, and what their names are.

NumberFormat helps you to format and parse numbers for any locale. Your code can be completely independent of the locale conventions for decimal points, thousands-separators, or even the particular decimal digits used, or whether the number format is even decimal.

To format a number for the current Locale, use one of the static factory methods:

    double myNumber = 7.0;
    UnicodeString myString;
    UErrorCode success = U_ZERO_ERROR;
    NumberFormat* nf = NumberFormat::createInstance(success)
    nf->format(myNumber, myString);
    cout << " Example 1: " << myString << endl;

Note that there are additional factory methods within subclasses of NumberFormat.

If you are formatting multiple numbers, it is more efficient to get the format and use it multiple times so that the system doesn't have to fetch the information about the local language and country conventions multiple times.

    UnicodeString myString;
    UErrorCode success = U_ZERO_ERROR;
    nf = NumberFormat::createInstance( success );
    int32_t a[] = { 123, 3333, -1234567 };
    const int32_t a_len = sizeof(a) / sizeof(a[0]);
    myString.remove();
    for (int32_t i = 0; i < a_len; i++) {
        nf->format(a[i], myString);
        myString += " ; ";
    }
    cout << " Example 2: " << myString << endl;

To format a number for a different Locale, specify it in the call to createInstance().

    nf = NumberFormat::createInstance( Locale::FRENCH, success );

You can use a NumberFormat to parse also.

    UErrorCode success;
    Formattable result(-999);  // initialized with error code
    nf->parse(myString, result, success);

Use createInstance to get the normal number format for that country. There are other static factory methods available. Use getCurrency to get the currency number format for that country. Use getPercent to get a format for displaying percentages. With this format, a fraction from 0.53 is displayed as 53%.

Starting from ICU 4.2, you can use createInstance() by passing in a 'style' as parameter to get the correct instance. For example, use createInstance(...kNumberStyle...) to get the normal number format, createInstance(...kPercentStyle...) to get a format for displaying percentage, createInstance(...kScientificStyle...) to get a format for displaying scientific number, createInstance(...kCurrencyStyle...) to get the currency number format, in which the currency is represented by its symbol, for example, "$3.00". createInstance(...kIsoCurrencyStyle...) to get the currency number format, in which the currency is represented by its ISO code, for example "USD3.00". createInstance(...kPluralCurrencyStyle...) to get the currency number format, in which the currency is represented by its full name in plural format, for example, "3.00 US dollars" or "1.00 US dollar".

You can also control the display of numbers with such methods as getMinimumFractionDigits. If you want even more control over the format or parsing, or want to give your users more control, you can try casting the NumberFormat you get from the factory methods to a DecimalNumberFormat. This will work for the vast majority of countries; just remember to put it in a try block in case you encounter an unusual one.

You can also use forms of the parse and format methods with ParsePosition and FieldPosition to allow you to:

- (a) progressively parse through pieces of a string.
- (b) align the decimal point and other areas.

For example, you can align numbers in two ways.

If you are using a monospaced font with spacing for alignment, you can pass the FieldPosition in your format call, with field = INTEGER_FIELD. On output, getEndIndex will be set to the offset between the last character of the integer and the decimal. Add (desiredSpaceCount - getEndIndex) spaces at the front of the string.

If you are using proportional fonts, instead of padding with spaces, measure the width of the string in pixels from the start to getEndIndex. Then move the pen by (desiredPixelWidth - widthToAlignmentPoint) before drawing the text. It also works where there is no decimal, but possibly additional characters at the end, e.g. with parentheses in negative numbers: "(12)" for -12.

User subclasses are not supported. While clients may write subclasses, such code will not necessarily work and will not be guaranteed to work stably from release to release.

Stable:
  ICU 2.0

Definition at line 172 of file numfmt.h.

## Member Enumeration Documentation

enum icu::NumberFormat::EAlignmentFields

Alignment Field constants used to construct a FieldPosition object.

Signifies that the position of the integer part or fraction part of a formatted number should be returned.

Note: as of ICU 4.4, the values in this enum have been extended to support identification of all number format fields, not just those pertaining to alignment.

These constants are provided for backwards compatibility only. Please use the C style constants defined in the header file unum.h.

See Also
  FieldPosition

Stable:
  ICU 2.0

Enumerator:

  kIntegerField | Stable: ICU 2.0
  kFractionField | Stable: ICU 2.0
  kDecimalSeparatorField | Stable: ICU 2.0
  kExponentSymbolField | Stable: ICU 2.0
  kExponentSignField | Stable: ICU 2.0
  kExponentField | Stable: ICU 2.0
  kGroupingSeparatorField | Stable: ICU 2.0
  kCurrencyField | Stable: ICU 2.0
  kPercentField | Stable: ICU 2.0
  kPermillField | Stable: ICU 2.0
  kSignField | Stable: ICU 2.0
  INTEGER_FIELD | These constants are provided for backwards compatibility only. Please use the constants defined in the header file unum.h. Stable: ICU 2.0
  FRACTION_FIELD | Stable: ICU 2.0

Definition at line 189 of file numfmt.h.

## Constructor & Destructor Documentation

virtual icu::NumberFormat::~NumberFormat | ( |  | ) |
| virtual

Destructor.

Stable:
  ICU 2.0

icu::NumberFormat::NumberFormat | ( |  | ) |
| protected

Default constructor for subclass use only.

Stable:
  ICU 2.0

icu::NumberFormat::NumberFormat | ( | const NumberFormat & |  | ) |
| protected

Copy constructor.

Stable:
  ICU 2.0

## Member Function Documentation

static NumberFormat* icu::NumberFormat::createCurrencyInstance | ( | UErrorCode & |  | ) |
| static

Returns a currency format for the current default locale.

Stable:
  ICU 2.0

static NumberFormat* icu::NumberFormat::createCurrencyInstance | ( | const Locale & | inLocale,
|  | UErrorCode & |
| ) |  |
| static

Returns a currency format for the specified locale.

Parameters

  inLocale | the given locale.

Stable:
  ICU 2.0

static NumberFormat* icu::NumberFormat::createInstance | ( | UErrorCode & |  | ) |
| static

Returns the default number format for the current default locale.

The default format is one of the styles provided by the other factory methods: getNumberInstance, getCurrencyInstance or getPercentInstance. Exactly which one is locale dependant.

Stable:
  ICU 2.0

static NumberFormat* icu::NumberFormat::createInstance | ( | const Locale & | inLocale,
|  | UErrorCode & |
| ) |  |
| static

Returns the default number format for the specified locale.

The default format is one of the styles provided by the other factory methods: getNumberInstance, getCurrencyInstance or getPercentInstance. Exactly which one is locale dependant.

Parameters

  inLocale | the given locale.

Stable:
  ICU 2.0

static NumberFormat* icu::NumberFormat::createInstance | ( | const Locale & | desiredLocale,
|  | UNumberFormatStyle | style,
|  | UErrorCode & | errorCode
| ) |  |
| static

Creates the specified decimal format style of the desired locale.

Parameters

  desiredLocale | the given locale.
  style | the given style.
  errorCode | Output param filled with success/failure status.

Returns
  A new NumberFormat instance.

Stable:
  ICU 4.8

static NumberFormat* icu::NumberFormat::createPercentInstance | ( | UErrorCode & |  | ) |
| static

Returns a percentage format for the current default locale.

Stable:
  ICU 2.0

static NumberFormat* icu::NumberFormat::createPercentInstance | ( | const Locale & | inLocale,
|  | UErrorCode & |
| ) |  |
| static

Returns a percentage format for the specified locale.

Parameters

  inLocale | the given locale.

Stable:
  ICU 2.0

static NumberFormat* icu::NumberFormat::createScientificInstance | ( | UErrorCode & |  | ) |
| static

Returns a scientific format for the current default locale.

Stable:
  ICU 2.0

static NumberFormat* icu::NumberFormat::createScientificInstance | ( | const Locale & | inLocale,
|  | UErrorCode & |
| ) |  |
| static

Returns a scientific format for the specified locale.

Parameters

  inLocale | the given locale.

Stable:
  ICU 2.0

static const SharedNumberFormat* icu::NumberFormat::createSharedInstance | ( | const Locale & | inLocale,
|  | UNumberFormatStyle | style,
|  | UErrorCode & | status
| ) |  |
| static

ICU use only.

Returns handle to the shared, cached NumberFormat instance for given locale. On success, caller must call removeRef() on returned value once it is done with the shared instance.

Internal:
  Do not use. This API is for internal use only.

virtual UnicodeString& icu::NumberFormat::format | ( | const Formattable & | obj,
|  | UnicodeString & | appendTo,
|  | FieldPosition & | pos,
|  | UErrorCode & | status
| ) |  | const
| virtual

Format an object to produce a string.

This method handles Formattable objects with numeric types. If the Formattable object type is not a numeric type, then it returns a failing UErrorCode.

Parameters

  obj | The object to format.
  appendTo | Output parameter to receive result. Result is appended to existing contents.
  pos | On input: an alignment field, if desired. On output: the offsets of the alignment field.
  status | Output param filled with success/failure status.

Returns
  Reference to 'appendTo' parameter.

Stable:
  ICU 2.0

Implements icu::Format.

virtual UnicodeString& icu::NumberFormat::format | ( | const Formattable & | obj,
|  | UnicodeString & | appendTo,
|  | FieldPositionIterator * | posIter,
|  | UErrorCode & | status
| ) |  | const
| virtual

Format an object to produce a string.

This method handles Formattable objects with numeric types. If the Formattable object type is not a numeric type, then it returns a failing UErrorCode.

Parameters

  obj | The object to format.
  appendTo | Output parameter to receive result. Result is appended to existing contents.
  posIter | On return, can be used to iterate over positions of fields generated by this format call. Can be NULL.
  status | Output param filled with success/failure status.

Returns
  Reference to 'appendTo' parameter.

Stable:
  4.4

Reimplemented from icu::Format.

UnicodeString& icu::NumberFormat::format | ( | double | number,
|  | UnicodeString & | appendTo
| ) |  | const

Format a double number.

These methods call the NumberFormat pure virtual format() methods with the default FieldPosition.

Parameters

  number | The value to be formatted.
  appendTo | Output parameter to receive result. Result is appended to existing contents.

Returns
  Reference to 'appendTo' parameter.

Stable:
  ICU 2.0

UnicodeString& icu::NumberFormat::format | ( | int32_t | number,
|  | UnicodeString & | appendTo
| ) |  | const

Format a long number.

These methods call the NumberFormat pure virtual format() methods with the default FieldPosition.

Parameters

  number | The value to be formatted.
  appendTo | Output parameter to receive result. Result is appended to existing contents.

Returns
  Reference to 'appendTo' parameter.

Stable:
  ICU 2.0

UnicodeString& icu::NumberFormat::format | ( | int64_t | number,
|  | UnicodeString & | appendTo
| ) |  | const

Format an int64 number.

These methods call the NumberFormat pure virtual format() methods with the default FieldPosition.

Parameters

  number | The value to be formatted.
  appendTo | Output parameter to receive result. Result is appended to existing contents.

Returns
  Reference to 'appendTo' parameter.

Stable:
  ICU 2.8

virtual UnicodeString& icu::NumberFormat::format | ( | double | number,
|  | UnicodeString & | appendTo,
|  | FieldPosition & | pos
| ) |  | const
| pure virtual

Format a double number.

Concrete subclasses must implement these pure virtual methods.

Parameters

  number | The value to be formatted.
  appendTo | Output parameter to receive result. Result is appended to existing contents.
  pos | On input: an alignment field, if desired. On output: the offsets of the alignment field.

Returns
  Reference to 'appendTo' parameter.

Stable:
  ICU 2.0

Implemented in icu::DecimalFormat, icu::RuleBasedNumberFormat, icu::ChoiceFormat, and icu::CompactDecimalFormat.

virtual UnicodeString& icu::NumberFormat::format | ( | double | number,
|  | UnicodeString & | appendTo,
|  | FieldPosition & | pos,
|  | UErrorCode & | status
| ) |  | const
| virtual

Format a double number.

By default, the parent function simply calls the base class and does not return an error status. Therefore, the status may be ignored in some subclasses.

Parameters

  number | The value to be formatted.
  appendTo | Output parameter to receive result. Result is appended to existing contents.
  pos | On input: an alignment field, if desired. On output: the offsets of the alignment field.
  status | error status

Returns
  Reference to 'appendTo' parameter.

Internal:
  Do not use. This API is for internal use only.

Reimplemented in icu::DecimalFormat.

virtual UnicodeString& icu::NumberFormat::format | ( | double | number,
|  | UnicodeString & | appendTo,
|  | FieldPositionIterator * | posIter,
|  | UErrorCode & | status
| ) |  | const
| virtual

Format a double number.

Subclasses must implement this method.

Parameters

  number | The value to be formatted.
  appendTo | Output parameter to receive result. Result is appended to existing contents.
  posIter | On return, can be used to iterate over positions of fields generated by this format call. Can be NULL.
  status | Output param filled with success/failure status.

Returns
  Reference to 'appendTo' parameter.

Stable:
  4.4

Reimplemented in icu::DecimalFormat, and icu::CompactDecimalFormat.

virtual UnicodeString& icu::NumberFormat::format | ( | int32_t | number,
|  | UnicodeString & | appendTo,
|  | FieldPosition & | pos
| ) |  | const
| pure virtual

Format a long number.

Concrete subclasses must implement these pure virtual methods.

Parameters

  number | The value to be formatted.
  appendTo | Output parameter to receive result. Result is appended to existing contents.
  pos | On input: an alignment field, if desired. On output: the offsets of the alignment field.

Returns
  Reference to 'appendTo' parameter.

Stable:
  ICU 2.0

Implemented in icu::DecimalFormat, icu::RuleBasedNumberFormat, and icu::ChoiceFormat.

virtual UnicodeString& icu::NumberFormat::format | ( | int32_t | number,
|  | UnicodeString & | appendTo,
|  | FieldPosition & | pos,
|  | UErrorCode & | status
| ) |  | const
| virtual

Format a long number.

Concrete subclasses may override this function to provide status return.

Parameters

  number | The value to be formatted.
  appendTo | Output parameter to receive result. Result is appended to existing contents.
  pos | On input: an alignment field, if desired. On output: the offsets of the alignment field.
  status | the output status.

Returns
  Reference to 'appendTo' parameter.

Internal:
  Do not use. This API is for internal use only.

Reimplemented in icu::DecimalFormat.

virtual UnicodeString& icu::NumberFormat::format | ( | int32_t | number,
|  | UnicodeString & | appendTo,
|  | FieldPositionIterator * | posIter,
|  | UErrorCode & | status
| ) |  | const
| virtual

Format an int32 number.

Subclasses must implement this method.

Parameters

  number | The value to be formatted.
  appendTo | Output parameter to receive result. Result is appended to existing contents.
  posIter | On return, can be used to iterate over positions of fields generated by this format call. Can be NULL.
  status | Output param filled with success/failure status.

Returns
  Reference to 'appendTo' parameter.

Stable:
  4.4

Reimplemented in icu::DecimalFormat.

virtual UnicodeString& icu::NumberFormat::format | ( | int64_t | number,
|  | UnicodeString & | appendTo,
|  | FieldPosition & | pos
| ) |  | const
| virtual

Format an int64 number.

(Not abstract to retain compatibility with earlier releases, however subclasses should override this method as it just delegates to format(int32_t number...);

Parameters

  number | The value to be formatted.
  appendTo | Output parameter to receive result. Result is appended to existing contents.
  pos | On input: an alignment field, if desired. On output: the offsets of the alignment field.

Returns
  Reference to 'appendTo' parameter.

Stable:
  ICU 2.8

Reimplemented in icu::DecimalFormat, icu::RuleBasedNumberFormat, icu::ChoiceFormat, and icu::CompactDecimalFormat.

virtual UnicodeString& icu::NumberFormat::format | ( | int64_t | number,
|  | UnicodeString & | appendTo,
|  | FieldPosition & | pos,
|  | UErrorCode & | status
| ) |  | const
| virtual

Format an int64 number.

(Not abstract to retain compatibility with earlier releases, however subclasses should override this method as it just delegates to format(int32_t number...);

Parameters

  number | The value to be formatted.
  appendTo | Output parameter to receive result. Result is appended to existing contents.
  pos | On input: an alignment field, if desired. On output: the offsets of the alignment field.

Returns
  Reference to 'appendTo' parameter.

Internal:
  Do not use. This API is for internal use only.

Reimplemented in icu::DecimalFormat.

virtual UnicodeString& icu::NumberFormat::format | ( | int64_t | number,
|  | UnicodeString & | appendTo,
|  | FieldPositionIterator * | posIter,
|  | UErrorCode & | status
| ) |  | const
| virtual

Format an int64 number.

Subclasses must implement this method.

Parameters

  number | The value to be formatted.
  appendTo | Output parameter to receive result. Result is appended to existing contents.
  posIter | On return, can be used to iterate over positions of fields generated by this format call. Can be NULL.
  status | Output param filled with success/failure status.

Returns
  Reference to 'appendTo' parameter.

Stable:
  4.4

Reimplemented in icu::DecimalFormat, and icu::CompactDecimalFormat.

virtual UnicodeString& icu::NumberFormat::format | ( | const StringPiece & | number,
|  | UnicodeString & | appendTo,
|  | FieldPositionIterator * | posIter,
|  | UErrorCode & | status
| ) |  | const
| virtual

Format a decimal number.

Subclasses must implement this method. The syntax of the unformatted number is a "numeric string" as defined in the Decimal Arithmetic Specification, available at http://speleotrove.com/decimal

Parameters

  number | The unformatted number, as a string, to be formatted.
  appendTo | Output parameter to receive result. Result is appended to existing contents.
  posIter | On return, can be used to iterate over positions of fields generated by this format call. Can be NULL.
  status | Output param filled with success/failure status.

Returns
  Reference to 'appendTo' parameter.

Stable:
  4.4

Reimplemented in icu::DecimalFormat, and icu::CompactDecimalFormat.

virtual UnicodeString& icu::NumberFormat::format | ( | const DigitList & | number,
|  | UnicodeString & | appendTo,
|  | FieldPositionIterator * | posIter,
|  | UErrorCode & | status
| ) |  | const
| virtual

Format a decimal number.

The number is a DigitList wrapper onto a floating point decimal number. The default implementation in NumberFormat converts the decimal number to a double and formats that. Subclasses of NumberFormat that want to specifically handle big decimal numbers must override this method. class DecimalFormat does so.

Parameters

  number | The number, a DigitList format Decimal Floating Point.
  appendTo | Output parameter to receive result. Result is appended to existing contents.
  posIter | On return, can be used to iterate over positions of fields generated by this format call.
  status | Output param filled with success/failure status.

Returns
  Reference to 'appendTo' parameter.

Internal:
  Do not use. This API is for internal use only.

Reimplemented in icu::DecimalFormat, and icu::CompactDecimalFormat.

virtual UnicodeString& icu::NumberFormat::format | ( | const DigitList & | number,
|  | UnicodeString & | appendTo,
|  | FieldPosition & | pos,
|  | UErrorCode & | status
| ) |  | const
| virtual

Format a decimal number.

The number is a DigitList wrapper onto a floating point decimal number. The default implementation in NumberFormat converts the decimal number to a double and formats that. Subclasses of NumberFormat that want to specifically handle big decimal numbers must override this method. class DecimalFormat does so.

Parameters

  number | The number, a DigitList format Decimal Floating Point.
  appendTo | Output parameter to receive result. Result is appended to existing contents.
  pos | On input: an alignment field, if desired. On output: the offsets of the alignment field.
  status | Output param filled with success/failure status.

Returns
  Reference to 'appendTo' parameter.

Internal:
  Do not use. This API is for internal use only.

Reimplemented in icu::DecimalFormat, and icu::CompactDecimalFormat.

static const Locale* icu::NumberFormat::getAvailableLocales | ( | int32_t & | count | ) |
| static

Get the set of Locales for which NumberFormats are installed.

Parameters

  count | Output param to receive the size of the locales

Stable:
  ICU 2.0

static StringEnumeration* icu::NumberFormat::getAvailableLocales | ( | void |  | ) |
| static

Return a StringEnumeration over the locales available at the time of the call, including registered locales.

Returns
  a StringEnumeration over the locales available at the time of the call

Stable:
  ICU 2.6

virtual UDisplayContext icu::NumberFormat::getContext | ( | UDisplayContextType | type,
|  | UErrorCode & | status
| ) |  | const
| virtual

Get the formatter's UDisplayContext value for the specified UDisplayContextType, such as UDISPCTX_TYPE_CAPITALIZATION.

Parameters

  type | The UDisplayContextType whose value to return
  status | Input/output status. If at entry this indicates a failure status, the function will do nothing; otherwise this will be updated with any new status from the function.

Returns
  The UDisplayContextValue for the specified type.

Stable:
  ICU 53

const UChar* icu::NumberFormat::getCurrency | ( |  | ) | const

Gets the currency used to display currency amounts.

This may be an empty string for some subclasses.

Returns
  a 3-letter null-terminated ISO code indicating the currency in use, or a pointer to the empty string.

Stable:
  ICU 2.6

virtual UClassID icu::NumberFormat::getDynamicClassID | ( | void |  | ) | const
| pure virtual

Returns a unique class ID POLYMORPHICALLY.

Pure virtual override. This method is to implement a simple version of RTTI, since not all C++ compilers support genuine RTTI. Polymorphic operator==() and clone() methods call this method.

Returns
  The class ID for this object. All objects of a given class have the same class ID. Objects of other classes have different class IDs.

Stable:
  ICU 2.0

Reimplemented from icu::UObject.

Implemented in icu::DecimalFormat, icu::RuleBasedNumberFormat, icu::ChoiceFormat, and icu::CompactDecimalFormat.

virtual void icu::NumberFormat::getEffectiveCurrency | ( | UChar * | result,
|  | UErrorCode & | ec
| ) |  | const
| protected virtual

Returns the currency in effect for this formatter.

Subclasses should override this method as needed. Unlike getCurrency(), this method should never return "".

Returns
  output parameter for null-terminated result, which must have a capacity of at least 4

Internal:
  Do not use. This API is for internal use only.

Reimplemented in icu::DecimalFormat.

int32_t icu::NumberFormat::getMaximumFractionDigits | ( | void |  | ) | const

Returns the maximum number of digits allowed in the fraction portion of a number.

Returns
  the maximum number of digits allowed in the fraction portion of a number.

See Also
  setMaximumFractionDigits

Stable:
  ICU 2.0

int32_t icu::NumberFormat::getMaximumIntegerDigits | ( | void |  | ) | const

Returns the maximum number of digits allowed in the integer portion of a number.

Returns
  the maximum number of digits allowed in the integer portion of a number.

See Also
  setMaximumIntegerDigits

Stable:
  ICU 2.0

int32_t icu::NumberFormat::getMinimumFractionDigits | ( | void |  | ) | const

Returns the minimum number of digits allowed in the fraction portion of a number.

Returns
  the minimum number of digits allowed in the fraction portion of a number.

See Also
  setMinimumFractionDigits

Stable:
  ICU 2.0

int32_t icu::NumberFormat::getMinimumIntegerDigits | ( | void |  | ) | const

Returns the minimum number of digits allowed in the integer portion of a number.

Returns
  the minimum number of digits allowed in the integer portion of a number.

See Also
  setMinimumIntegerDigits

Stable:
  ICU 2.0

static UClassID icu::NumberFormat::getStaticClassID | ( | void |  | ) |
| static

Return the class ID for this class.

This is useful for comparing to a return value from getDynamicClassID(). Note that, because NumberFormat is an abstract base class, no fully constructed object will have the class ID returned by NumberFormat::getStaticClassID().

Returns
  The class ID for all objects of this class.

Stable:
  ICU 2.0

Reimplemented in icu::DecimalFormat, icu::RuleBasedNumberFormat, icu::ChoiceFormat, and icu::CompactDecimalFormat.

static NumberFormat* icu::NumberFormat::internalCreateInstance | ( | const Locale & | desiredLocale,
|  | UNumberFormatStyle | style,
|  | UErrorCode & | errorCode
| ) |  |
| static

ICU use only.

Creates NumberFormat instance without using the cache.

Internal:
  Do not use. This API is for internal use only.

UBool icu::NumberFormat::isGroupingUsed | ( | void |  | ) | const

Returns true if grouping is used in this format.

For example, in the English locale, with grouping on, the number 1234567 might be formatted as "1,234,567". The grouping separator as well as the size of each group is locale dependant and is determined by sub-classes of NumberFormat.

See Also
  setGroupingUsed

Stable:
  ICU 2.0

UBool icu::NumberFormat::isLenient | ( | void |  | ) | const
| inline virtual

Returns whether lenient parsing is enabled (it is off by default).

Returns
  `TRUE` if lenient parsing is enabled, `FALSE` otherwise.

See Also
  setLenient

Stable:
  ICU 4.8

Reimplemented in icu::RuleBasedNumberFormat.

Definition at line 1175 of file numfmt.h.

UBool icu::NumberFormat::isParseIntegerOnly | ( | void |  | ) | const
| inline

Return true if this format will parse numbers as integers only.

For example in the English locale, with ParseIntegerOnly true, the string "1234." would be parsed as the integer value 1234 and parsing would stop at the "." character. Of course, the exact format accepted by the parse operation is locale dependant and determined by sub-classes of NumberFormat.

Returns
  true if this format will parse numbers as integers only.

Stable:
  ICU 2.0

Definition at line 1169 of file numfmt.h.

static NumberFormat* icu::NumberFormat::makeInstance | ( | const Locale & | desiredLocale,
|  | UNumberFormatStyle | style,
|  | UBool | mustBeDecimalFormat,
|  | UErrorCode & | errorCode
| ) |  |
| static protected

Creates the specified number format style of the desired locale.

If mustBeDecimalFormat is TRUE, then the returned pointer is either a DecimalFormat or it is NULL.

Internal:
  Do not use. This API is for internal use only.

NumberFormat& icu::NumberFormat::operator= | ( | const NumberFormat & |  | ) |
| protected

Assignment operator.

Stable:
  ICU 2.0

virtual UBool icu::NumberFormat::operator== | ( | const Format & | other | ) | const
| virtual

Return true if the given Format objects are semantically equal.

Objects of different subclasses are considered unequal.

Returns
  true if the given Format objects are semantically equal.

Stable:
  ICU 2.0

Implements icu::Format.

Reimplemented in icu::DecimalFormat, icu::RuleBasedNumberFormat, icu::ChoiceFormat, and icu::CompactDecimalFormat.

virtual void icu::NumberFormat::parse | ( | const UnicodeString & | text,
|  | Formattable & | result,
|  | ParsePosition & | parsePosition
| ) |  | const
| pure virtual

Return a long if possible (e.g.

within range LONG_MAX, LONG_MAX], and with no decimals), otherwise a double. If IntegerOnly is set, will stop at a decimal point (or equivalent; e.g. for rational numbers "1 2/3", will stop after the 1).

If no object can be parsed, index is unchanged, and NULL is returned.

This is a pure virtual which concrete subclasses must implement.

Parameters

  text | The text to be parsed.
  result | Formattable to be set to the parse result. If parse fails, return contents are undefined.
  parsePosition | The position to start parsing at on input. On output, moved to after the last successfully parse character. On parse failure, does not change.

Stable:
  ICU 2.0

Implemented in icu::DecimalFormat, icu::RuleBasedNumberFormat, icu::ChoiceFormat, and icu::CompactDecimalFormat.

virtual void icu::NumberFormat::parse | ( | const UnicodeString & | text,
|  | Formattable & | result,
|  | UErrorCode & | status
| ) |  | const
| virtual

Parse a string as a numeric value, and return a Formattable numeric object.

This method parses integers only if IntegerOnly is set.

Parameters

  text | The text to be parsed.
  result | Formattable to be set to the parse result. If parse fails, return contents are undefined.
  status | Output parameter set to a failure error code when a failure occurs.

See Also
  NumberFormat::isParseIntegerOnly

Stable:
  ICU 2.0

Reimplemented in icu::CompactDecimalFormat.

virtual CurrencyAmount* icu::NumberFormat::parseCurrency | ( | const UnicodeString & | text,
|  | ParsePosition & | pos
| ) |  | const
| virtual

Parses text from the given string as a currency amount.

Unlike the parse() method, this method will attempt to parse a generic currency name, searching for a match of this object's locale's currency display names, or for a 3-letter ISO currency code. This method will fail if this format is not a currency format, that is, if it does not contain the currency pattern symbol (U+00A4) in its prefix or suffix.

Parameters

  text | the string to parse
  pos | input-output position; on input, the position within text to match; must have 0 <= pos.getIndex() < text.length(); on output, the position after the last matched character. If the parse fails, the position in unchanged upon output.

Returns
  if parse succeeds, a pointer to a newly-created CurrencyAmount object (owned by the caller) containing information about the parsed currency; if parse fails, this is NULL.

Stable:
  ICU 49

Reimplemented in icu::DecimalFormat, and icu::CompactDecimalFormat.

virtual void icu::NumberFormat::parseObject | ( | const UnicodeString & | source,
|  | Formattable & | result,
|  | ParsePosition & | parse_pos
| ) |  | const
| virtual

Parse a string to produce an object.

This methods handles parsing of numeric strings into Formattable objects with numeric types.

Before calling, set parse_pos.index to the offset you want to start parsing at in the source. After calling, parse_pos.index indicates the position after the successfully parsed text. If an error occurs, parse_pos.index is unchanged.

When parsing, leading whitespace is discarded (with successful parse), while trailing whitespace is left as is.

See Format::parseObject() for more.

Parameters

  source | The string to be parsed into an object.
  result | Formattable to be set to the parse result. If parse fails, return contents are undefined.
  parse_pos | The position to start parsing at. Upon return this param is set to the position after the last character successfully parsed. If the source is not parsed successfully, this param will remain unchanged.

Returns
  A newly created Formattable* object, or NULL on failure. The caller owns this and should delete it when done.

Stable:
  ICU 2.0

Implements icu::Format.

static URegistryKey icu::NumberFormat::registerFactory | ( | NumberFormatFactory * | toAdopt,
|  | UErrorCode & | status
| ) |  |
| static

Register a new NumberFormatFactory.

The factory will be adopted. Because ICU may choose to cache NumberFormat objects internally, this must be called at application startup, prior to any calls to NumberFormat::createInstance to avoid undefined behavior.

Parameters

  toAdopt | the NumberFormatFactory instance to be adopted
  status | the in/out status code, no special meanings are assigned

Returns
  a registry key that can be used to unregister this factory

Stable:
  ICU 2.6

virtual void icu::NumberFormat::setContext | ( | UDisplayContext | value,
|  | UErrorCode & | status
| ) |  |
| virtual

Set a particular UDisplayContext value in the formatter, such as UDISPCTX_CAPITALIZATION_FOR_STANDALONE.

Parameters

  value | The UDisplayContext value to set.
  status | Input/output status. If at entry this indicates a failure status, the function will do nothing; otherwise this will be updated with any new status from the function.

Stable:
  ICU 53

Reimplemented in icu::RuleBasedNumberFormat, and icu::DecimalFormat.

virtual void icu::NumberFormat::setCurrency | ( | const UChar * | theCurrency,
|  | UErrorCode & | ec
| ) |  |
| virtual

Sets the currency used to display currency amounts.

This takes effect immediately, if this format is a currency format. If this format is not a currency format, then the currency is used if and when this object becomes a currency format.

Parameters

  theCurrency | a 3-letter ISO code indicating new currency to use. It need not be null-terminated. May be the empty string or NULL to indicate no currency.
  ec | input-output error code

Stable:
  ICU 3.0

Reimplemented in icu::DecimalFormat.

virtual void icu::NumberFormat::setGroupingUsed | ( | UBool | newValue | ) |
| virtual

Set whether or not grouping will be used in this format.

Parameters

  newValue | True, grouping will be used in this format.

See Also
  getGroupingUsed

Stable:
  ICU 2.0

Reimplemented in icu::DecimalFormat.

virtual void icu::NumberFormat::setLenient | ( | UBool | enable | ) |
| virtual

Sets whether lenient parsing should be enabled (it is off by default).

Parameters

  enable | `TRUE` if lenient parsing should be used, `FALSE` otherwise.

Stable:
  ICU 4.8

Reimplemented in icu::RuleBasedNumberFormat.

virtual void icu::NumberFormat::setMaximumFractionDigits | ( | int32_t | newValue | ) |
| virtual

Sets the maximum number of digits allowed in the fraction portion of a number.

maximumFractionDigits must be >= minimumFractionDigits. If the new value for maximumFractionDigits is less than the current value of minimumFractionDigits, then minimumFractionDigits will also be set to the new value.

Parameters

  newValue | the new value to be set.

See Also
  getMaximumFractionDigits

Stable:
  ICU 2.0

Reimplemented in icu::DecimalFormat.

virtual void icu::NumberFormat::setMaximumIntegerDigits | ( | int32_t | newValue | ) |
| virtual

Sets the maximum number of digits allowed in the integer portion of a number.

maximumIntegerDigits must be >= minimumIntegerDigits. If the new value for maximumIntegerDigits is less than the current value of minimumIntegerDigits, then minimumIntegerDigits will also be set to the new value.

Parameters

  newValue | the new value for the maximum number of digits allowed in the integer portion of a number.

See Also
  getMaximumIntegerDigits

Stable:
  ICU 2.0

Reimplemented in icu::DecimalFormat.

virtual void icu::NumberFormat::setMinimumFractionDigits | ( | int32_t | newValue | ) |
| virtual

Sets the minimum number of digits allowed in the fraction portion of a number.

minimumFractionDigits must be <= maximumFractionDigits. If the new value for minimumFractionDigits exceeds the current value of maximumFractionDigits, then maximumIntegerDigits will also be set to the new value

Parameters

  newValue | the new value to be set.

See Also
  getMinimumFractionDigits

Stable:
  ICU 2.0

Reimplemented in icu::DecimalFormat.

virtual void icu::NumberFormat::setMinimumIntegerDigits | ( | int32_t | newValue | ) |
| virtual

Sets the minimum number of digits allowed in the integer portion of a number.

minimumIntegerDigits must be <= maximumIntegerDigits. If the new value for minimumIntegerDigits exceeds the current value of maximumIntegerDigits, then maximumIntegerDigits will also be set to the new value.

Parameters

  newValue | the new value to be set.

See Also
  getMinimumIntegerDigits

Stable:
  ICU 2.0

Reimplemented in icu::DecimalFormat.

virtual void icu::NumberFormat::setParseIntegerOnly | ( | UBool | value | ) |
| virtual

Sets whether or not numbers should be parsed as integers only.

Parameters

  value | set True, this format will parse numbers as integers only.

See Also
  isParseIntegerOnly

Stable:
  ICU 2.0

Reimplemented in icu::DecimalFormat.

static UBool icu::NumberFormat::unregister | ( | URegistryKey | key,
|  | UErrorCode & | status
| ) |  |
| static

Unregister a previously-registered NumberFormatFactory using the key returned from the register call.

Key becomes invalid after a successful call and should not be used again. The NumberFormatFactory corresponding to the key will be deleted. Because ICU may choose to cache NumberFormat objects internally, this should be called during application shutdown, after all calls to NumberFormat::createInstance to avoid undefined behavior.

Parameters

  key | the registry key returned by a previous call to registerFactory
  status | the in/out status code, no special meanings are assigned

Returns
  TRUE if the factory for the key was successfully unregistered

Stable:
  ICU 2.6

The documentation for this class was generated from the following file:

- numfmt.h