Logo Search packages:      
Sourcecode: icu version File versions

NumberFormat Class Reference

#include <numfmt.h>

Inheritance diagram for NumberFormat:

Format 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]);
     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%.

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.

Definition at line 125 of file numfmt.h.

Public Types

enum  EAlignmentFields { kIntegerField, kFractionField, INTEGER_FIELD = kIntegerField, FRACTION_FIELD = kFractionField }

Public Member Functions

virtual Formatclone () const =0
UnicodeStringformat (const Formattable &obj, UnicodeString &result, UErrorCode &status) const
virtual UnicodeStringformat (int32_t number, UnicodeString &toAppendTo, FieldPosition &pos) const =0
virtual UnicodeStringformat (double number, UnicodeString &toAppendTo, FieldPosition &pos) const =0
UnicodeStringformat (int32_t number, UnicodeString &output) const
UnicodeStringformat (double number, UnicodeString &output) const
virtual UnicodeStringformat (const Formattable &obj, UnicodeString &toAppendTo, FieldPosition &pos, UErrorCode &status) const
virtual UClassID getDynamicClassID (void) 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
void parseObject (const UnicodeString &source, Formattable &result, UErrorCode &status) const
virtual void parseObject (const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const
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 NumberFormatcreateCurrencyInstance (const Locale &inLocale, UErrorCode &)
static NumberFormatcreateCurrencyInstance (UErrorCode &)
static NumberFormatcreateInstance (const Locale &inLocale, UErrorCode &)
static NumberFormatcreateInstance (UErrorCode &)
static NumberFormatcreatePercentInstance (const Locale &inLocale, UErrorCode &)
static NumberFormatcreatePercentInstance (UErrorCode &)
static NumberFormatcreateScientificInstance (const Locale &inLocale, UErrorCode &)
static NumberFormatcreateScientificInstance (UErrorCode &)
static const Locale * getAvailableLocales (int32_t &count)
static UClassID getStaticClassID (void)

Protected Member Functions

 NumberFormat (const NumberFormat &)
 NumberFormat ()
NumberFormatoperator= (const NumberFormat &)
void syntaxError (const UnicodeString &pattern, int32_t pos, UParseError &parseError)

Static Protected Attributes

static const int32_t fgMaxIntegerDigits = DBL_MAX_10_EXP + 1
static const int32_t fgMinIntegerDigits = 127

Private Types

enum  EStyles {
  kNumberStyle, kCurrencyStyle, kPercentStyle, kScientificStyle,

Static Private Member Functions

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

Private Attributes

UBool fGroupingUsed
int32_t fMaxFractionDigits
int32_t fMaxIntegerDigits
int32_t fMinFractionDigits
int32_t fMinIntegerDigits
UBool fParseIntegerOnly

Static Private Attributes

static const char fgClassID = 0
static const UChar *const fgLastResortNumberPatterns []
static const int32_t fgNumberPatternsCount = 3

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

Generated by  Doxygen 1.6.0   Back to index