Logo Search packages:      
Sourcecode: icu version File versions  Download package

NumberFormat Class Reference

#include <numfmt.h>

Inheritance diagram for NumberFormat:

Format UObject UMemory ChoiceFormat DecimalFormat RuleBasedNumberFormat

List of all members.


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;
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:

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.

ICU 2.0

Definition at line 163 of file numfmt.h.


Public Types

enum  EAlignmentFields {
  kIntegerField, kFractionField, kDecimalSeparatorField, kExponentSymbolField,
  kExponentSignField, kExponentField, kGroupingSeparatorField, kCurrencyField,
  kPercentField, kPermillField, kSignField, INTEGER_FIELD = kIntegerField,
  FRACTION_FIELD = kFractionField
}
enum  EStyles {
  kNumberStyle, kCurrencyStyle, kPercentStyle, kScientificStyle,
  kIsoCurrencyStyle, kPluralCurrencyStyle, kStyleCount
}

Public Member Functions

virtual Formatclone () const =0
UnicodeStringformat (const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
virtual UnicodeStringformat (const StringPiece &number, UnicodeString &appendTo, FieldPositionIterator &posIter, UErrorCode &status) const
virtual UnicodeStringformat (int64_t number, UnicodeString &appendTo, FieldPositionIterator &posIter, UErrorCode &status) const
virtual UnicodeStringformat (int64_t number, UnicodeString &appendTo, FieldPosition &pos) const
virtual UnicodeStringformat (int32_t number, UnicodeString &appendTo, FieldPositionIterator &posIter, UErrorCode &status) const
virtual UnicodeStringformat (int32_t number, UnicodeString &appendTo, FieldPosition &pos) const =0
virtual UnicodeStringformat (double number, UnicodeString &appendTo, FieldPositionIterator &posIter, UErrorCode &status) const
virtual UnicodeStringformat (double number, UnicodeString &appendTo, FieldPosition &pos) const =0
UnicodeStringformat (int64_t number, UnicodeString &appendTo) const
UnicodeStringformat (int32_t number, UnicodeString &appendTo) const
UnicodeStringformat (double number, UnicodeString &appendTo) const
virtual UnicodeStringformat (const Formattable &obj, UnicodeString &appendTo, FieldPositionIterator &posIter, UErrorCode &status) const
virtual UnicodeStringformat (const Formattable &obj, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
const UChar * getCurrency () const
virtual UClassID getDynamicClassID (void) const =0
Locale getLocale (ULocDataLocaleType type, UErrorCode &status) const
const char * getLocaleID (ULocDataLocaleType type, UErrorCode &status) const
int32_t getMaximumFractionDigits (void) const
int32_t getMaximumIntegerDigits (void) const
int32_t getMinimumFractionDigits (void) const
int32_t getMinimumIntegerDigits (void) const
UBool isGroupingUsed (void) const
UBool isParseIntegerOnly (void) const
UBool operator!= (const Format &other) const
virtual UBool operator== (const Format &other) const
virtual void parse (const UnicodeString &text, Formattable &result, UErrorCode &status) const
virtual void parse (const UnicodeString &text, Formattable &result, ParsePosition &parsePosition) const =0
virtual FormattableparseCurrency (const UnicodeString &text, Formattable &result, ParsePosition &pos) const
void parseObject (const UnicodeString &source, Formattable &result, UErrorCode &status) const
virtual void parseObject (const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const
virtual void setCurrency (const UChar *theCurrency, UErrorCode &ec)
virtual void setGroupingUsed (UBool newValue)
virtual void setMaximumFractionDigits (int32_t newValue)
virtual void setMaximumIntegerDigits (int32_t newValue)
virtual void setMinimumFractionDigits (int32_t newValue)
virtual void setMinimumIntegerDigits (int32_t newValue)
virtual void setParseIntegerOnly (UBool value)
virtual ~NumberFormat ()

Static Public Member Functions

static NumberFormat *U_EXPORT2 createCurrencyInstance (const Locale &inLocale, UErrorCode &)
static NumberFormat *U_EXPORT2 createCurrencyInstance (UErrorCode &)
static NumberFormat *U_EXPORT2 createInstance (const Locale &desiredLocale, EStyles choice, UErrorCode &success)
static NumberFormat *U_EXPORT2 createInstance (const Locale &inLocale, UErrorCode &)
static NumberFormat *U_EXPORT2 createInstance (UErrorCode &)
static NumberFormat *U_EXPORT2 createPercentInstance (const Locale &inLocale, UErrorCode &)
static NumberFormat *U_EXPORT2 createPercentInstance (UErrorCode &)
static NumberFormat *U_EXPORT2 createScientificInstance (const Locale &inLocale, UErrorCode &)
static NumberFormat *U_EXPORT2 createScientificInstance (UErrorCode &)
static StringEnumeration *U_EXPORT2 getAvailableLocales (void)
static const Locale *U_EXPORT2 getAvailableLocales (int32_t &count)
static UClassID U_EXPORT2 getStaticClassID (void)
static void U_EXPORT2 operator delete (void *, void *) U_NO_THROW
static void U_EXPORT2 operator delete (void *p) U_NO_THROW
static void U_EXPORT2 operator delete[] (void *p) U_NO_THROW
static void *U_EXPORT2 operator new (size_t, void *ptr) U_NO_THROW
static void *U_EXPORT2 operator new (size_t size) U_NO_THROW
static void *U_EXPORT2 operator new[] (size_t size) U_NO_THROW
static URegistryKey U_EXPORT2 registerFactory (NumberFormatFactory *toAdopt, UErrorCode &status)
static UBool U_EXPORT2 unregister (URegistryKey key, UErrorCode &status)

Protected Member Functions

virtual void getEffectiveCurrency (UChar *result, UErrorCode &ec) const
 NumberFormat (const NumberFormat &)
 NumberFormat ()
NumberFormatoperator= (const NumberFormat &)
void setLocaleIDs (const char *valid, const char *actual)

Static Protected Member Functions

static void syntaxError (const UnicodeString &pattern, int32_t pos, UParseError &parseError)

Static Private Member Functions

static NumberFormatmakeInstance (const Locale &desiredLocale, EStyles choice, UErrorCode &success)

Private Attributes

UChar fCurrency [4]
UBool fGroupingUsed
int32_t fMaxFractionDigits
int32_t fMaxIntegerDigits
int32_t fMinFractionDigits
int32_t fMinIntegerDigits
UBool fParseIntegerOnly

Friends

class ICUNumberFormatFactory
class ICUNumberFormatService

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

Generated by  Doxygen 1.6.0   Back to index