Home | History | Annotate | Download | only in unicode
      1 /*
      2 ********************************************************************************
      3 * Copyright (C) 1997-2009, International Business Machines Corporation and others.
      4 * All Rights Reserved.
      5 ********************************************************************************
      6 *
      7 * File NUMFMT.H
      8 *
      9 * Modification History:
     10 *
     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 ********************************************************************************
     19 */
     20 
     21 #ifndef NUMFMT_H
     22 #define NUMFMT_H
     23 
     24 
     25 #include "unicode/utypes.h"
     26 
     27 /**
     28  * \file
     29  * \brief C++ API: Abstract base class for all number formats.
     30  */
     31 
     32 #if !UCONFIG_NO_FORMATTING
     33 
     34 #include "unicode/unistr.h"
     35 #include "unicode/format.h"
     36 #include "unicode/unum.h" // UNumberFormatStyle
     37 #include "unicode/locid.h"
     38 
     39 U_NAMESPACE_BEGIN
     40 
     41 #if !UCONFIG_NO_SERVICE
     42 class NumberFormatFactory;
     43 class StringEnumeration;
     44 #endif
     45 
     46 /**
     47  *
     48  * Abstract base class for all number formats.  Provides interface for
     49  * formatting and parsing a number.  Also provides methods for
     50  * determining which locales have number formats, and what their names
     51  * are.
     52  * <P>
     53  * NumberFormat helps you to format and parse numbers for any locale.
     54  * Your code can be completely independent of the locale conventions
     55  * for decimal points, thousands-separators, or even the particular
     56  * decimal digits used, or whether the number format is even decimal.
     57  * <P>
     58  * To format a number for the current Locale, use one of the static
     59  * factory methods:
     60  * <pre>
     61  * \code
     62  *    double myNumber = 7.0;
     63  *    UnicodeString myString;
     64  *    UErrorCode success = U_ZERO_ERROR;
     65  *    NumberFormat* nf = NumberFormat::createInstance(success)
     66  *    nf->format(myNumber, myString);
     67  *    cout << " Example 1: " << myString << endl;
     68  * \endcode
     69  * </pre>
     70  * If you are formatting multiple numbers, it is more efficient to get
     71  * the format and use it multiple times so that the system doesn't
     72  * have to fetch the information about the local language and country
     73  * conventions multiple times.
     74  * <pre>
     75  * \code
     76  *     UnicodeString myString;
     77  *     UErrorCode success = U_ZERO_ERROR;
     78  *     nf = NumberFormat::createInstance( success );
     79  *     int32_t a[] = { 123, 3333, -1234567 };
     80  *     const int32_t a_len = sizeof(a) / sizeof(a[0]);
     81  *     myString.remove();
     82  *     for (int32_t i = 0; i < a_len; i++) {
     83  *         nf->format(a[i], myString);
     84  *         myString += " ; ";
     85  *     }
     86  *     cout << " Example 2: " << myString << endl;
     87  * \endcode
     88  * </pre>
     89  * To format a number for a different Locale, specify it in the
     90  * call to createInstance().
     91  * <pre>
     92  * \code
     93  *     nf = NumberFormat::createInstance( Locale::FRENCH, success );
     94  * \endcode
     95  * </pre>
     96  * You can use a NumberFormat to parse also.
     97  * <pre>
     98  * \code
     99  *    UErrorCode success;
    100  *    Formattable result(-999);  // initialized with error code
    101  *    nf->parse(myString, result, success);
    102  * \endcode
    103  * </pre>
    104  * Use createInstance to get the normal number format for that country.
    105  * There are other static factory methods available.  Use getCurrency
    106  * to get the currency number format for that country.  Use getPercent
    107  * to get a format for displaying percentages. With this format, a
    108  * fraction from 0.53 is displayed as 53%.
    109  * <P>
    110  * Starting from ICU 4.2, you can use createInstance() by passing in a 'style'
    111  * as parameter to get the correct instance.
    112  * For example,
    113  * use createInstance(...kNumberStyle...) to get the normal number format,
    114  * createInstance(...kPercentStyle...) to get a format for displaying
    115  * percentage,
    116  * createInstance(...kScientificStyle...) to get a format for displaying
    117  * scientific number,
    118  * createInstance(...kCurrencyStyle...) to get the currency number format,
    119  * in which the currency is represented by its symbol, for example, "$3.00".
    120  * createInstance(...kIsoCurrencyStyle...)  to get the currency number format,
    121  * in which the currency is represented by its ISO code, for example "USD3.00".
    122  * createInstance(...kPluralCurrencyStyle...) to get the currency number format,
    123  * in which the currency is represented by its full name in plural format,
    124  * for example, "3.00 US dollars" or "1.00 US dollar".
    125  * <P>
    126  * You can also control the display of numbers with such methods as
    127  * getMinimumFractionDigits.  If you want even more control over the
    128  * format or parsing, or want to give your users more control, you can
    129  * try casting the NumberFormat you get from the factory methods to a
    130  * DecimalNumberFormat. This will work for the vast majority of
    131  * countries; just remember to put it in a try block in case you
    132  * encounter an unusual one.
    133  * <P>
    134  * You can also use forms of the parse and format methods with
    135  * ParsePosition and FieldPosition to allow you to:
    136  * <ul type=round>
    137  *   <li>(a) progressively parse through pieces of a string.
    138  *   <li>(b) align the decimal point and other areas.
    139  * </ul>
    140  * For example, you can align numbers in two ways.
    141  * <P>
    142  * If you are using a monospaced font with spacing for alignment, you
    143  * can pass the FieldPosition in your format call, with field =
    144  * INTEGER_FIELD. On output, getEndIndex will be set to the offset
    145  * between the last character of the integer and the decimal. Add
    146  * (desiredSpaceCount - getEndIndex) spaces at the front of the
    147  * string.
    148  * <P>
    149  * If you are using proportional fonts, instead of padding with
    150  * spaces, measure the width of the string in pixels from the start to
    151  * getEndIndex.  Then move the pen by (desiredPixelWidth -
    152  * widthToAlignmentPoint) before drawing the text.  It also works
    153  * where there is no decimal, but possibly additional characters at
    154  * the end, e.g. with parentheses in negative numbers: "(12)" for -12.
    155  * <p>
    156  * <em>User subclasses are not supported.</em> While clients may write
    157  * subclasses, such code will not necessarily work and will not be
    158  * guaranteed to work stably from release to release.
    159  *
    160  * @stable ICU 2.0
    161  */
    162 class U_I18N_API NumberFormat : public Format {
    163 public:
    164 
    165     /**
    166      * Constants for various number format styles.
    167      * kNumberStyle specifies a normal number style of format.
    168      * kCurrencyStyle specifies a currency format using currency symbol name,
    169      * such as in "$1.00".
    170      * kPercentStyle specifies a style of format to display percent.
    171      * kScientificStyle specifies a style of format to display scientific number.
    172      * kISOCurrencyStyle specifies a currency format using ISO currency code,
    173      * such as in "USD1.00".
    174      * kPluralCurrencyStyle specifies a currency format using currency plural
    175      * names, such as in "1.00 US dollar" and "3.00 US dollars".
    176      * @draft ICU 4.2
    177      */
    178     enum EStyles {
    179         kNumberStyle,
    180         kCurrencyStyle,
    181         kPercentStyle,
    182         kScientificStyle,
    183         kIsoCurrencyStyle,
    184         kPluralCurrencyStyle,
    185         kStyleCount // ALWAYS LAST ENUM: number of styles
    186     };
    187 
    188     /**
    189      * Alignment Field constants used to construct a FieldPosition object.
    190      * Signifies that the position of the integer part or fraction part of
    191      * a formatted number should be returned.
    192      *
    193      * @see FieldPosition
    194      * @stable ICU 2.0
    195      */
    196     enum EAlignmentFields {
    197         kIntegerField,
    198         kFractionField,
    199 
    200 
    201     /**
    202      * These constants are provided for backwards compatibility only.
    203      * Please use the C++ style constants defined above.
    204      * @stable ICU 2.0
    205      */
    206         INTEGER_FIELD        = kIntegerField,
    207         FRACTION_FIELD        = kFractionField
    208     };
    209 
    210     /**
    211      * Destructor.
    212      * @stable ICU 2.0
    213      */
    214     virtual ~NumberFormat();
    215 
    216     /**
    217      * Return true if the given Format objects are semantically equal.
    218      * Objects of different subclasses are considered unequal.
    219      * @return    true if the given Format objects are semantically equal.
    220      * @stable ICU 2.0
    221      */
    222     virtual UBool operator==(const Format& other) const;
    223 
    224     /**
    225      * Format an object to produce a string.  This method handles
    226      * Formattable objects with numeric types. If the Formattable
    227      * object type is not a numeric type, then it returns a failing
    228      * UErrorCode.
    229      *
    230      * @param obj       The object to format.
    231      * @param appendTo  Output parameter to receive result.
    232      *                  Result is appended to existing contents.
    233      * @param pos       On input: an alignment field, if desired.
    234      *                  On output: the offsets of the alignment field.
    235      * @param status    Output param filled with success/failure status.
    236      * @return          Reference to 'appendTo' parameter.
    237      * @stable ICU 2.0
    238      */
    239     virtual UnicodeString& format(const Formattable& obj,
    240                                   UnicodeString& appendTo,
    241                                   FieldPosition& pos,
    242                                   UErrorCode& status) const;
    243 
    244     /**
    245      * Parse a string to produce an object.  This methods handles
    246      * parsing of numeric strings into Formattable objects with numeric
    247      * types.
    248      * <P>
    249      * Before calling, set parse_pos.index to the offset you want to
    250      * start parsing at in the source. After calling, parse_pos.index
    251      * indicates the position after the successfully parsed text.  If
    252      * an error occurs, parse_pos.index is unchanged.
    253      * <P>
    254      * When parsing, leading whitespace is discarded (with successful
    255      * parse), while trailing whitespace is left as is.
    256      * <P>
    257      * See Format::parseObject() for more.
    258      *
    259      * @param source    The string to be parsed into an object.
    260      * @param result    Formattable to be set to the parse result.
    261      *                  If parse fails, return contents are undefined.
    262      * @param parse_pos The position to start parsing at. Upon return
    263      *                  this param is set to the position after the
    264      *                  last character successfully parsed. If the
    265      *                  source is not parsed successfully, this param
    266      *                  will remain unchanged.
    267      * @return          A newly created Formattable* object, or NULL
    268      *                  on failure.  The caller owns this and should
    269      *                  delete it when done.
    270      * @stable ICU 2.0
    271      */
    272     virtual void parseObject(const UnicodeString& source,
    273                              Formattable& result,
    274                              ParsePosition& parse_pos) const;
    275 
    276     /**
    277      * Format a double number. These methods call the NumberFormat
    278      * pure virtual format() methods with the default FieldPosition.
    279      *
    280      * @param number    The value to be formatted.
    281      * @param appendTo  Output parameter to receive result.
    282      *                  Result is appended to existing contents.
    283      * @return          Reference to 'appendTo' parameter.
    284      * @stable ICU 2.0
    285      */
    286     UnicodeString& format(  double number,
    287                             UnicodeString& appendTo) const;
    288 
    289     /**
    290      * Format a long number. These methods call the NumberFormat
    291      * pure virtual format() methods with the default FieldPosition.
    292      *
    293      * @param number    The value to be formatted.
    294      * @param appendTo  Output parameter to receive result.
    295      *                  Result is appended to existing contents.
    296      * @return          Reference to 'appendTo' parameter.
    297      * @stable ICU 2.0
    298      */
    299     UnicodeString& format(  int32_t number,
    300                             UnicodeString& appendTo) const;
    301 
    302     /**
    303      * Format an int64 number. These methods call the NumberFormat
    304      * pure virtual format() methods with the default FieldPosition.
    305      *
    306      * @param number    The value to be formatted.
    307      * @param appendTo  Output parameter to receive result.
    308      *                  Result is appended to existing contents.
    309      * @return          Reference to 'appendTo' parameter.
    310      * @stable ICU 2.8
    311      */
    312     UnicodeString& format(  int64_t number,
    313                             UnicodeString& appendTo) const;
    314 
    315     /**
    316      * Format a double number. Concrete subclasses must implement
    317      * these pure virtual methods.
    318      *
    319      * @param number    The value to be formatted.
    320      * @param appendTo  Output parameter to receive result.
    321      *                  Result is appended to existing contents.
    322      * @param pos       On input: an alignment field, if desired.
    323      *                  On output: the offsets of the alignment field.
    324      * @return          Reference to 'appendTo' parameter.
    325      * @stable ICU 2.0
    326      */
    327     virtual UnicodeString& format(double number,
    328                                   UnicodeString& appendTo,
    329                                   FieldPosition& pos) const = 0;
    330     /**
    331      * Format a long number. Concrete subclasses must implement
    332      * these pure virtual methods.
    333      *
    334      * @param number    The value to be formatted.
    335      * @param appendTo  Output parameter to receive result.
    336      *                  Result is appended to existing contents.
    337      * @param pos       On input: an alignment field, if desired.
    338      *                  On output: the offsets of the alignment field.
    339      * @return          Reference to 'appendTo' parameter.
    340      * @stable ICU 2.0
    341     */
    342     virtual UnicodeString& format(int32_t number,
    343                                   UnicodeString& appendTo,
    344                                   FieldPosition& pos) const = 0;
    345 
    346     /**
    347      * Format an int64 number. (Not abstract to retain compatibility
    348      * with earlier releases, however subclasses should override this
    349      * method as it just delegates to format(int32_t number...);
    350      *
    351      * @param number    The value to be formatted.
    352      * @param appendTo  Output parameter to receive result.
    353      *                  Result is appended to existing contents.
    354      * @param pos       On input: an alignment field, if desired.
    355      *                  On output: the offsets of the alignment field.
    356      * @return          Reference to 'appendTo' parameter.
    357      * @stable ICU 2.8
    358     */
    359     virtual UnicodeString& format(int64_t number,
    360                                   UnicodeString& appendTo,
    361                                   FieldPosition& pos) const;
    362     /**
    363      * Redeclared Format method.
    364      * @param obj       The object to be formatted.
    365      * @param appendTo  Output parameter to receive result.
    366      *                  Result is appended to existing contents.
    367      * @param status    Output parameter set to a failure error code
    368      *                  when a failure occurs.
    369      * @return          Reference to 'appendTo' parameter.
    370      * @stable ICU 2.0
    371      */
    372     UnicodeString& format(const Formattable& obj,
    373                           UnicodeString& appendTo,
    374                           UErrorCode& status) const;
    375 
    376    /**
    377     * Return a long if possible (e.g. within range LONG_MAX,
    378     * LONG_MAX], and with no decimals), otherwise a double.  If
    379     * IntegerOnly is set, will stop at a decimal point (or equivalent;
    380     * e.g. for rational numbers "1 2/3", will stop after the 1).
    381     * <P>
    382     * If no object can be parsed, index is unchanged, and NULL is
    383     * returned.
    384     * <P>
    385     * This is a pure virtual which concrete subclasses must implement.
    386     *
    387     * @param text           The text to be parsed.
    388     * @param result         Formattable to be set to the parse result.
    389     *                       If parse fails, return contents are undefined.
    390     * @param parsePosition  The position to start parsing at on input.
    391     *                       On output, moved to after the last successfully
    392     *                       parse character. On parse failure, does not change.
    393     * @return               A Formattable object of numeric type.  The caller
    394     *                       owns this an must delete it.  NULL on failure.
    395     * @stable ICU 2.0
    396     */
    397     virtual void parse(const UnicodeString& text,
    398                        Formattable& result,
    399                        ParsePosition& parsePosition) const = 0;
    400 
    401     /**
    402      * Parse a string as a numeric value, and return a Formattable
    403      * numeric object. This method parses integers only if IntegerOnly
    404      * is set.
    405      *
    406      * @param text          The text to be parsed.
    407      * @param result        Formattable to be set to the parse result.
    408      *                      If parse fails, return contents are undefined.
    409      * @param status        Output parameter set to a failure error code
    410      *                      when a failure occurs.
    411      * @return              A Formattable object of numeric type.  The caller
    412      *                      owns this an must delete it.  NULL on failure.
    413      * @see                 NumberFormat::isParseIntegerOnly
    414      * @stable ICU 2.0
    415      */
    416     virtual void parse( const UnicodeString& text,
    417                         Formattable& result,
    418                         UErrorCode& status) const;
    419 
    420     /**
    421      * Parses text from the given string as a currency amount.  Unlike
    422      * the parse() method, this method will attempt to parse a generic
    423      * currency name, searching for a match of this object's locale's
    424      * currency display names, or for a 3-letter ISO currency code.
    425      * This method will fail if this format is not a currency format,
    426      * that is, if it does not contain the currency pattern symbol
    427      * (U+00A4) in its prefix or suffix.
    428      *
    429      * @param text the string to parse
    430      * @param result output parameter to receive result. This will have
    431      * its currency set to the parsed ISO currency code.
    432      * @param pos input-output position; on input, the position within
    433      * text to match; must have 0 <= pos.getIndex() < text.length();
    434      * on output, the position after the last matched character. If
    435      * the parse fails, the position in unchanged upon output.
    436      * @return a reference to result
    437      * @internal
    438      */
    439     virtual Formattable& parseCurrency(const UnicodeString& text,
    440                                        Formattable& result,
    441                                        ParsePosition& pos) const;
    442 
    443     /**
    444      * Return true if this format will parse numbers as integers
    445      * only.  For example in the English locale, with ParseIntegerOnly
    446      * true, the string "1234." would be parsed as the integer value
    447      * 1234 and parsing would stop at the "." character.  Of course,
    448      * the exact format accepted by the parse operation is locale
    449      * dependant and determined by sub-classes of NumberFormat.
    450      * @return    true if this format will parse numbers as integers
    451      *            only.
    452      * @stable ICU 2.0
    453      */
    454     UBool isParseIntegerOnly(void) const;
    455 
    456     /**
    457      * Sets whether or not numbers should be parsed as integers only.
    458      * @param value    set True, this format will parse numbers as integers
    459      *                 only.
    460      * @see isParseIntegerOnly
    461      * @stable ICU 2.0
    462      */
    463     virtual void setParseIntegerOnly(UBool value);
    464 
    465     /**
    466      * Returns the default number format for the current default
    467      * locale.  The default format is one of the styles provided by
    468      * the other factory methods: getNumberInstance,
    469      * getCurrencyInstance or getPercentInstance.  Exactly which one
    470      * is locale dependant.
    471      * @stable ICU 2.0
    472      */
    473     static NumberFormat* U_EXPORT2 createInstance(UErrorCode&);
    474 
    475     /**
    476      * Returns the default number format for the specified locale.
    477      * The default format is one of the styles provided by the other
    478      * factory methods: getNumberInstance, getCurrencyInstance or
    479      * getPercentInstance.  Exactly which one is locale dependant.
    480      * @param inLocale    the given locale.
    481      * @stable ICU 2.0
    482      */
    483     static NumberFormat* U_EXPORT2 createInstance(const Locale& inLocale,
    484                                         UErrorCode&);
    485 
    486     /**
    487      * Creates the specified decimal format style of the desired locale.
    488      * @param desiredLocale    the given locale.
    489      * @param choice           the given style.
    490      * @param success          Output param filled with success/failure status.
    491      * @return                 A new NumberFormat instance.
    492      * @draft ICU 4.2
    493      */
    494     static NumberFormat* U_EXPORT2 createInstance(const Locale& desiredLocale, EStyles choice, UErrorCode& success);
    495 
    496 
    497     /**
    498      * Returns a currency format for the current default locale.
    499      * @stable ICU 2.0
    500      */
    501     static NumberFormat* U_EXPORT2 createCurrencyInstance(UErrorCode&);
    502 
    503     /**
    504      * Returns a currency format for the specified locale.
    505      * @param inLocale    the given locale.
    506      * @stable ICU 2.0
    507      */
    508     static NumberFormat* U_EXPORT2 createCurrencyInstance(const Locale& inLocale,
    509                                                 UErrorCode&);
    510 
    511     /**
    512      * Returns a percentage format for the current default locale.
    513      * @stable ICU 2.0
    514      */
    515     static NumberFormat* U_EXPORT2 createPercentInstance(UErrorCode&);
    516 
    517     /**
    518      * Returns a percentage format for the specified locale.
    519      * @param inLocale    the given locale.
    520      * @stable ICU 2.0
    521      */
    522     static NumberFormat* U_EXPORT2 createPercentInstance(const Locale& inLocale,
    523                                                UErrorCode&);
    524 
    525     /**
    526      * Returns a scientific format for the current default locale.
    527      * @stable ICU 2.0
    528      */
    529     static NumberFormat* U_EXPORT2 createScientificInstance(UErrorCode&);
    530 
    531     /**
    532      * Returns a scientific format for the specified locale.
    533      * @param inLocale    the given locale.
    534      * @stable ICU 2.0
    535      */
    536     static NumberFormat* U_EXPORT2 createScientificInstance(const Locale& inLocale,
    537                                                 UErrorCode&);
    538 
    539     /**
    540      * Get the set of Locales for which NumberFormats are installed.
    541      * @param count    Output param to receive the size of the locales
    542      * @stable ICU 2.0
    543      */
    544     static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
    545 
    546 #if !UCONFIG_NO_SERVICE
    547     /**
    548      * Register a new NumberFormatFactory.  The factory will be adopted.
    549      * @param toAdopt the NumberFormatFactory instance to be adopted
    550      * @param status the in/out status code, no special meanings are assigned
    551      * @return a registry key that can be used to unregister this factory
    552      * @stable ICU 2.6
    553      */
    554     static URegistryKey U_EXPORT2 registerFactory(NumberFormatFactory* toAdopt, UErrorCode& status);
    555 
    556     /**
    557      * Unregister a previously-registered NumberFormatFactory using the key returned from the
    558      * register call.  Key becomes invalid after a successful call and should not be used again.
    559      * The NumberFormatFactory corresponding to the key will be deleted.
    560      * @param key the registry key returned by a previous call to registerFactory
    561      * @param status the in/out status code, no special meanings are assigned
    562      * @return TRUE if the factory for the key was successfully unregistered
    563      * @stable ICU 2.6
    564      */
    565     static UBool U_EXPORT2 unregister(URegistryKey key, UErrorCode& status);
    566 
    567     /**
    568      * Return a StringEnumeration over the locales available at the time of the call,
    569      * including registered locales.
    570      * @return a StringEnumeration over the locales available at the time of the call
    571      * @stable ICU 2.6
    572      */
    573     static StringEnumeration* U_EXPORT2 getAvailableLocales(void);
    574 #endif /* UCONFIG_NO_SERVICE */
    575 
    576     /**
    577      * Returns true if grouping is used in this format. For example,
    578      * in the English locale, with grouping on, the number 1234567
    579      * might be formatted as "1,234,567". The grouping separator as
    580      * well as the size of each group is locale dependant and is
    581      * determined by sub-classes of NumberFormat.
    582      * @see setGroupingUsed
    583      * @stable ICU 2.0
    584      */
    585     UBool isGroupingUsed(void) const;
    586 
    587     /**
    588      * Set whether or not grouping will be used in this format.
    589      * @param newValue    True, grouping will be used in this format.
    590      * @see getGroupingUsed
    591      * @stable ICU 2.0
    592      */
    593     virtual void setGroupingUsed(UBool newValue);
    594 
    595     /**
    596      * Returns the maximum number of digits allowed in the integer portion of a
    597      * number.
    598      * @return     the maximum number of digits allowed in the integer portion of a
    599      *             number.
    600      * @see setMaximumIntegerDigits
    601      * @stable ICU 2.0
    602      */
    603     int32_t getMaximumIntegerDigits(void) const;
    604 
    605     /**
    606      * Sets the maximum number of digits allowed in the integer portion of a
    607      * number. maximumIntegerDigits must be >= minimumIntegerDigits.  If the
    608      * new value for maximumIntegerDigits is less than the current value
    609      * of minimumIntegerDigits, then minimumIntegerDigits will also be set to
    610      * the new value.
    611      *
    612      * @param newValue    the new value for the maximum number of digits
    613      *                    allowed in the integer portion of a number.
    614      * @see getMaximumIntegerDigits
    615      * @stable ICU 2.0
    616      */
    617     virtual void setMaximumIntegerDigits(int32_t newValue);
    618 
    619     /**
    620      * Returns the minimum number of digits allowed in the integer portion of a
    621      * number.
    622      * @return    the minimum number of digits allowed in the integer portion of a
    623      *            number.
    624      * @see setMinimumIntegerDigits
    625      * @stable ICU 2.0
    626      */
    627     int32_t getMinimumIntegerDigits(void) const;
    628 
    629     /**
    630      * Sets the minimum number of digits allowed in the integer portion of a
    631      * number. minimumIntegerDigits must be &lt;= maximumIntegerDigits.  If the
    632      * new value for minimumIntegerDigits exceeds the current value
    633      * of maximumIntegerDigits, then maximumIntegerDigits will also be set to
    634      * the new value.
    635      * @param newValue    the new value to be set.
    636      * @see getMinimumIntegerDigits
    637      * @stable ICU 2.0
    638      */
    639     virtual void setMinimumIntegerDigits(int32_t newValue);
    640 
    641     /**
    642      * Returns the maximum number of digits allowed in the fraction portion of a
    643      * number.
    644      * @return    the maximum number of digits allowed in the fraction portion of a
    645      *            number.
    646      * @see setMaximumFractionDigits
    647      * @stable ICU 2.0
    648      */
    649     int32_t getMaximumFractionDigits(void) const;
    650 
    651     /**
    652      * Sets the maximum number of digits allowed in the fraction portion of a
    653      * number. maximumFractionDigits must be >= minimumFractionDigits.  If the
    654      * new value for maximumFractionDigits is less than the current value
    655      * of minimumFractionDigits, then minimumFractionDigits will also be set to
    656      * the new value.
    657      * @param newValue    the new value to be set.
    658      * @see getMaximumFractionDigits
    659      * @stable ICU 2.0
    660      */
    661     virtual void setMaximumFractionDigits(int32_t newValue);
    662 
    663     /**
    664      * Returns the minimum number of digits allowed in the fraction portion of a
    665      * number.
    666      * @return    the minimum number of digits allowed in the fraction portion of a
    667      *            number.
    668      * @see setMinimumFractionDigits
    669      * @stable ICU 2.0
    670      */
    671     int32_t getMinimumFractionDigits(void) const;
    672 
    673     /**
    674      * Sets the minimum number of digits allowed in the fraction portion of a
    675      * number. minimumFractionDigits must be &lt;= maximumFractionDigits.   If the
    676      * new value for minimumFractionDigits exceeds the current value
    677      * of maximumFractionDigits, then maximumIntegerDigits will also be set to
    678      * the new value
    679      * @param newValue    the new value to be set.
    680      * @see getMinimumFractionDigits
    681      * @stable ICU 2.0
    682      */
    683     virtual void setMinimumFractionDigits(int32_t newValue);
    684 
    685     /**
    686      * Sets the currency used to display currency
    687      * amounts.  This takes effect immediately, if this format is a
    688      * currency format.  If this format is not a currency format, then
    689      * the currency is used if and when this object becomes a
    690      * currency format.
    691      * @param theCurrency a 3-letter ISO code indicating new currency
    692      * to use.  It need not be null-terminated.  May be the empty
    693      * string or NULL to indicate no currency.
    694      * @param ec input-output error code
    695      * @stable ICU 3.0
    696      */
    697     virtual void setCurrency(const UChar* theCurrency, UErrorCode& ec);
    698 
    699     /**
    700      * Gets the currency used to display currency
    701      * amounts.  This may be an empty string for some subclasses.
    702      * @return a 3-letter null-terminated ISO code indicating
    703      * the currency in use, or a pointer to the empty string.
    704      * @stable ICU 2.6
    705      */
    706     const UChar* getCurrency() const;
    707 
    708 public:
    709 
    710     /**
    711      * Return the class ID for this class.  This is useful for
    712      * comparing to a return value from getDynamicClassID(). Note that,
    713      * because NumberFormat is an abstract base class, no fully constructed object
    714      * will have the class ID returned by NumberFormat::getStaticClassID().
    715      * @return The class ID for all objects of this class.
    716      * @stable ICU 2.0
    717      */
    718     static UClassID U_EXPORT2 getStaticClassID(void);
    719 
    720     /**
    721      * Returns a unique class ID POLYMORPHICALLY.  Pure virtual override.
    722      * This method is to implement a simple version of RTTI, since not all
    723      * C++ compilers support genuine RTTI.  Polymorphic operator==() and
    724      * clone() methods call this method.
    725      * <P>
    726      * @return The class ID for this object. All objects of a
    727      * given class have the same class ID.  Objects of
    728      * other classes have different class IDs.
    729      * @stable ICU 2.0
    730      */
    731     virtual UClassID getDynamicClassID(void) const = 0;
    732 
    733 protected:
    734 
    735     /**
    736      * Default constructor for subclass use only.
    737      * @stable ICU 2.0
    738      */
    739     NumberFormat();
    740 
    741     /**
    742      * Copy constructor.
    743      * @stable ICU 2.0
    744      */
    745     NumberFormat(const NumberFormat&);
    746 
    747     /**
    748      * Assignment operator.
    749      * @stable ICU 2.0
    750      */
    751     NumberFormat& operator=(const NumberFormat&);
    752 
    753     /**
    754      * Returns the currency in effect for this formatter.  Subclasses
    755      * should override this method as needed.  Unlike getCurrency(),
    756      * this method should never return "".
    757      * @result output parameter for null-terminated result, which must
    758      * have a capacity of at least 4
    759      * @internal
    760      */
    761     virtual void getEffectiveCurrency(UChar* result, UErrorCode& ec) const;
    762 
    763 private:
    764 
    765     /**
    766      * Creates the specified decimal format style of the desired locale.
    767      * @param desiredLocale    the given locale.
    768      * @param choice           the given style.
    769      * @param success          Output param filled with success/failure status.
    770      * @return                 A new NumberFormat instance.
    771      */
    772     static NumberFormat* makeInstance(const Locale& desiredLocale, EStyles choice, UErrorCode& success);
    773 
    774     UBool      fGroupingUsed;
    775     int32_t     fMaxIntegerDigits;
    776     int32_t     fMinIntegerDigits;
    777     int32_t     fMaxFractionDigits;
    778     int32_t     fMinFractionDigits;
    779     UBool      fParseIntegerOnly;
    780 
    781     // ISO currency code
    782     UChar      fCurrency[4];
    783 
    784     friend class ICUNumberFormatFactory; // access to makeInstance, EStyles
    785     friend class ICUNumberFormatService;
    786 };
    787 
    788 #if !UCONFIG_NO_SERVICE
    789 /**
    790  * A NumberFormatFactory is used to register new number formats.  The factory
    791  * should be able to create any of the predefined formats for each locale it
    792  * supports.  When registered, the locales it supports extend or override the
    793  * locale already supported by ICU.
    794  *
    795  * @stable ICU 2.6
    796  */
    797 class U_I18N_API NumberFormatFactory : public UObject {
    798 public:
    799 
    800     /**
    801      * Destructor
    802      * @stable ICU 3.0
    803      */
    804     virtual ~NumberFormatFactory();
    805 
    806     /**
    807      * Return true if this factory will be visible.  Default is true.
    808      * If not visible, the locales supported by this factory will not
    809      * be listed by getAvailableLocales.
    810      * @stable ICU 2.6
    811      */
    812     virtual UBool visible(void) const = 0;
    813 
    814     /**
    815      * Return the locale names directly supported by this factory.  The number of names
    816      * is returned in count;
    817      * @stable ICU 2.6
    818      */
    819     virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const = 0;
    820 
    821     /**
    822      * Return a number format of the appropriate type.  If the locale
    823      * is not supported, return null.  If the locale is supported, but
    824      * the type is not provided by this service, return null.  Otherwise
    825      * return an appropriate instance of NumberFormat.
    826      * @stable ICU 2.6
    827      */
    828     virtual NumberFormat* createFormat(const Locale& loc, UNumberFormatStyle formatType) = 0;
    829 };
    830 
    831 /**
    832  * A NumberFormatFactory that supports a single locale.  It can be visible or invisible.
    833  * @stable ICU 2.6
    834  */
    835 class U_I18N_API SimpleNumberFormatFactory : public NumberFormatFactory {
    836 protected:
    837     /**
    838      * True if the locale supported by this factory is visible.
    839      * @stable ICU 2.6
    840      */
    841     const UBool _visible;
    842 
    843     /**
    844      * The locale supported by this factory, as a UnicodeString.
    845      * @stable ICU 2.6
    846      */
    847     UnicodeString _id;
    848 
    849 public:
    850     /**
    851      * @stable ICU 2.6
    852      */
    853     SimpleNumberFormatFactory(const Locale& locale, UBool visible = TRUE);
    854 
    855     /**
    856      * @stable ICU 3.0
    857      */
    858     virtual ~SimpleNumberFormatFactory();
    859 
    860     /**
    861      * @stable ICU 2.6
    862      */
    863     virtual UBool visible(void) const;
    864 
    865     /**
    866      * @stable ICU 2.6
    867      */
    868     virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const;
    869 };
    870 #endif /* #if !UCONFIG_NO_SERVICE */
    871 
    872 // -------------------------------------
    873 
    874 inline UBool
    875 NumberFormat::isParseIntegerOnly() const
    876 {
    877     return fParseIntegerOnly;
    878 }
    879 
    880 inline UnicodeString&
    881 NumberFormat::format(const Formattable& obj,
    882                      UnicodeString& appendTo,
    883                      UErrorCode& status) const {
    884     return Format::format(obj, appendTo, status);
    885 }
    886 
    887 U_NAMESPACE_END
    888 
    889 #endif /* #if !UCONFIG_NO_FORMATTING */
    890 
    891 #endif // _NUMFMT
    892 //eof
    893