jpayne@69: // © 2016 and later: Unicode, Inc. and others. jpayne@69: // License & terms of use: http://www.unicode.org/copyright.html jpayne@69: /* jpayne@69: ******************************************************************************** jpayne@69: * Copyright (C) 1997-2016, International Business Machines Corporation and others. jpayne@69: * All Rights Reserved. jpayne@69: ******************************************************************************** jpayne@69: * jpayne@69: * File NUMFMT.H jpayne@69: * jpayne@69: * Modification History: jpayne@69: * jpayne@69: * Date Name Description jpayne@69: * 02/19/97 aliu Converted from java. jpayne@69: * 03/18/97 clhuang Updated per C++ implementation. jpayne@69: * 04/17/97 aliu Changed DigitCount to int per code review. jpayne@69: * 07/20/98 stephen JDK 1.2 sync up. Added scientific support. jpayne@69: * Changed naming conventions to match C++ guidelines jpayne@69: * Derecated Java style constants (eg, INTEGER_FIELD) jpayne@69: ******************************************************************************** jpayne@69: */ jpayne@69: jpayne@69: #ifndef NUMFMT_H jpayne@69: #define NUMFMT_H jpayne@69: jpayne@69: jpayne@69: #include "unicode/utypes.h" jpayne@69: jpayne@69: #if U_SHOW_CPLUSPLUS_API jpayne@69: jpayne@69: /** jpayne@69: * \file jpayne@69: * \brief C++ API: Compatibility APIs for number formatting. jpayne@69: */ jpayne@69: jpayne@69: #if !UCONFIG_NO_FORMATTING jpayne@69: jpayne@69: #include "unicode/unistr.h" jpayne@69: #include "unicode/format.h" jpayne@69: #include "unicode/unum.h" // UNumberFormatStyle jpayne@69: #include "unicode/locid.h" jpayne@69: #include "unicode/stringpiece.h" jpayne@69: #include "unicode/curramt.h" jpayne@69: #include "unicode/udisplaycontext.h" jpayne@69: jpayne@69: class NumberFormatTest; jpayne@69: jpayne@69: U_NAMESPACE_BEGIN jpayne@69: jpayne@69: class SharedNumberFormat; jpayne@69: jpayne@69: #if !UCONFIG_NO_SERVICE jpayne@69: class NumberFormatFactory; jpayne@69: class StringEnumeration; jpayne@69: #endif jpayne@69: jpayne@69: /** jpayne@69: *

IMPORTANT: New users are strongly encouraged to see if jpayne@69: * numberformatter.h fits their use case. Although not deprecated, this header jpayne@69: * is provided for backwards compatibility only. jpayne@69: * jpayne@69: * Abstract base class for all number formats. Provides interface for jpayne@69: * formatting and parsing a number. Also provides methods for jpayne@69: * determining which locales have number formats, and what their names jpayne@69: * are. jpayne@69: * jpayne@69: * \headerfile unicode/numfmt.h "unicode/numfmt.h" jpayne@69: *

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

jpayne@69: * To format a number for the current Locale, use one of the static jpayne@69: * factory methods: jpayne@69: * \code jpayne@69: * #include jpayne@69: * #include "unicode/numfmt.h" jpayne@69: * #include "unicode/unistr.h" jpayne@69: * #include "unicode/ustream.h" jpayne@69: * using namespace std; jpayne@69: * jpayne@69: * int main() { jpayne@69: * double myNumber = 7.0; jpayne@69: * UnicodeString myString; jpayne@69: * UErrorCode success = U_ZERO_ERROR; jpayne@69: * NumberFormat* nf = NumberFormat::createInstance(success); jpayne@69: * nf->format(myNumber, myString); jpayne@69: * cout << " Example 1: " << myString << endl; jpayne@69: * } jpayne@69: * \endcode jpayne@69: * Note that there are additional factory methods within subclasses of jpayne@69: * NumberFormat. jpayne@69: *

jpayne@69: * If you are formatting multiple numbers, it is more efficient to get jpayne@69: * the format and use it multiple times so that the system doesn't jpayne@69: * have to fetch the information about the local language and country jpayne@69: * conventions multiple times. jpayne@69: * \code jpayne@69: * UnicodeString myString; jpayne@69: * UErrorCode success = U_ZERO_ERROR; jpayne@69: * NumberFormat *nf = NumberFormat::createInstance( success ); jpayne@69: * for (int32_t number: {123, 3333, -1234567}) { jpayne@69: * nf->format(number, myString); jpayne@69: * myString += "; "; jpayne@69: * } jpayne@69: * cout << " Example 2: " << myString << endl; jpayne@69: * \endcode jpayne@69: * To format a number for a different Locale, specify it in the jpayne@69: * call to \c createInstance(). jpayne@69: * \code jpayne@69: * nf = NumberFormat::createInstance(Locale::getFrench(), success); jpayne@69: * \endcode jpayne@69: * You can use a \c NumberFormat to parse also. jpayne@69: * \code jpayne@69: * UErrorCode success; jpayne@69: * Formattable result(-999); // initialized with error code jpayne@69: * nf->parse(myString, result, success); jpayne@69: * \endcode jpayne@69: * Use \c createInstance() to get the normal number format for a \c Locale. jpayne@69: * There are other static factory methods available. Use \c createCurrencyInstance() jpayne@69: * to get the currency number format for that country. Use \c createPercentInstance() jpayne@69: * to get a format for displaying percentages. With this format, a jpayne@69: * fraction from 0.53 is displayed as 53%. jpayne@69: *

jpayne@69: * The type of number formatting can be specified by passing a 'style' parameter to \c createInstance(). jpayne@69: * For example, use\n jpayne@69: * \c createInstance(locale, UNUM_DECIMAL, errorCode) to get the normal number format,\n jpayne@69: * \c createInstance(locale, UNUM_PERCENT, errorCode) to get a format for displaying percentage,\n jpayne@69: * \c createInstance(locale, UNUM_SCIENTIFIC, errorCode) to get a format for displaying scientific number,\n jpayne@69: * \c createInstance(locale, UNUM_CURRENCY, errorCode) to get the currency number format, jpayne@69: * in which the currency is represented by its symbol, for example, "$3.00".\n jpayne@69: * \c createInstance(locale, UNUM_CURRENCY_ISO, errorCode) to get the currency number format, jpayne@69: * in which the currency is represented by its ISO code, for example "USD3.00".\n jpayne@69: * \c createInstance(locale, UNUM_CURRENCY_PLURAL, errorCode) to get the currency number format, jpayne@69: * in which the currency is represented by its full name in plural format, jpayne@69: * for example, "3.00 US dollars" or "1.00 US dollar". jpayne@69: *

jpayne@69: * You can also control the display of numbers with such methods as jpayne@69: * \c getMinimumFractionDigits(). If you want even more control over the jpayne@69: * format or parsing, or want to give your users more control, you can jpayne@69: * try dynamic_casting the \c NumberFormat you get from the factory methods to a jpayne@69: * \c DecimalFormat. This will work for the vast majority of jpayne@69: * countries; just remember to test for NULL in case you jpayne@69: * encounter an unusual one. jpayne@69: *

jpayne@69: * You can also use forms of the parse and format methods with jpayne@69: * \c ParsePosition and \c FieldPosition to allow you to: jpayne@69: *

jpayne@69: * For example, you can align numbers in two ways. jpayne@69: *

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

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

jpayne@69: * User subclasses are not supported. While clients may write jpayne@69: * subclasses, such code will not necessarily work and will not be jpayne@69: * guaranteed to work stably from release to release. jpayne@69: * jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: class U_I18N_API NumberFormat : public Format { jpayne@69: public: jpayne@69: /** jpayne@69: * Rounding mode. jpayne@69: * jpayne@69: *

jpayne@69: * For more detail on rounding modes, see: jpayne@69: * http://userguide.icu-project.org/formatparse/numbers/rounding-modes jpayne@69: * jpayne@69: * @stable ICU 2.4 jpayne@69: */ jpayne@69: enum ERoundingMode { jpayne@69: kRoundCeiling, /**< Round towards positive infinity */ jpayne@69: kRoundFloor, /**< Round towards negative infinity */ jpayne@69: kRoundDown, /**< Round towards zero */ jpayne@69: kRoundUp, /**< Round away from zero */ jpayne@69: kRoundHalfEven, /**< Round towards the nearest integer, or jpayne@69: towards the nearest even integer if equidistant */ jpayne@69: kRoundHalfDown, /**< Round towards the nearest integer, or jpayne@69: towards zero if equidistant */ jpayne@69: kRoundHalfUp, /**< Round towards the nearest integer, or jpayne@69: away from zero if equidistant */ jpayne@69: /** jpayne@69: * Return U_FORMAT_INEXACT_ERROR if number does not format exactly. jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: kRoundUnnecessary jpayne@69: }; jpayne@69: jpayne@69: /** jpayne@69: * Alignment Field constants used to construct a FieldPosition object. jpayne@69: * Signifies that the position of the integer part or fraction part of jpayne@69: * a formatted number should be returned. jpayne@69: * jpayne@69: * Note: as of ICU 4.4, the values in this enum have been extended to jpayne@69: * support identification of all number format fields, not just those jpayne@69: * pertaining to alignment. jpayne@69: * jpayne@69: * These constants are provided for backwards compatibility only. jpayne@69: * Please use the C style constants defined in the header file unum.h. jpayne@69: * jpayne@69: * @see FieldPosition jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: enum EAlignmentFields { jpayne@69: /** @stable ICU 2.0 */ jpayne@69: kIntegerField = UNUM_INTEGER_FIELD, jpayne@69: /** @stable ICU 2.0 */ jpayne@69: kFractionField = UNUM_FRACTION_FIELD, jpayne@69: /** @stable ICU 2.0 */ jpayne@69: kDecimalSeparatorField = UNUM_DECIMAL_SEPARATOR_FIELD, jpayne@69: /** @stable ICU 2.0 */ jpayne@69: kExponentSymbolField = UNUM_EXPONENT_SYMBOL_FIELD, jpayne@69: /** @stable ICU 2.0 */ jpayne@69: kExponentSignField = UNUM_EXPONENT_SIGN_FIELD, jpayne@69: /** @stable ICU 2.0 */ jpayne@69: kExponentField = UNUM_EXPONENT_FIELD, jpayne@69: /** @stable ICU 2.0 */ jpayne@69: kGroupingSeparatorField = UNUM_GROUPING_SEPARATOR_FIELD, jpayne@69: /** @stable ICU 2.0 */ jpayne@69: kCurrencyField = UNUM_CURRENCY_FIELD, jpayne@69: /** @stable ICU 2.0 */ jpayne@69: kPercentField = UNUM_PERCENT_FIELD, jpayne@69: /** @stable ICU 2.0 */ jpayne@69: kPermillField = UNUM_PERMILL_FIELD, jpayne@69: /** @stable ICU 2.0 */ jpayne@69: kSignField = UNUM_SIGN_FIELD, jpayne@69: /** @stable ICU 64 */ jpayne@69: kMeasureUnitField = UNUM_MEASURE_UNIT_FIELD, jpayne@69: /** @stable ICU 64 */ jpayne@69: kCompactField = UNUM_COMPACT_FIELD, jpayne@69: jpayne@69: /** jpayne@69: * These constants are provided for backwards compatibility only. jpayne@69: * Please use the constants defined in the header file unum.h. jpayne@69: */ jpayne@69: /** @stable ICU 2.0 */ jpayne@69: INTEGER_FIELD = UNUM_INTEGER_FIELD, jpayne@69: /** @stable ICU 2.0 */ jpayne@69: FRACTION_FIELD = UNUM_FRACTION_FIELD jpayne@69: }; jpayne@69: jpayne@69: /** jpayne@69: * Destructor. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual ~NumberFormat(); jpayne@69: jpayne@69: /** jpayne@69: * Clones this object polymorphically. jpayne@69: * The caller owns the result and should delete it when done. jpayne@69: * @return clone, or nullptr if an error occurred jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual NumberFormat* clone() const = 0; jpayne@69: jpayne@69: /** jpayne@69: * Return true if the given Format objects are semantically equal. jpayne@69: * Objects of different subclasses are considered unequal. jpayne@69: * @return true if the given Format objects are semantically equal. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual UBool operator==(const Format& other) const; jpayne@69: jpayne@69: jpayne@69: using Format::format; jpayne@69: jpayne@69: /** jpayne@69: * Format an object to produce a string. This method handles jpayne@69: * Formattable objects with numeric types. If the Formattable jpayne@69: * object type is not a numeric type, then it returns a failing jpayne@69: * UErrorCode. jpayne@69: * jpayne@69: * @param obj The object to format. jpayne@69: * @param appendTo Output parameter to receive result. jpayne@69: * Result is appended to existing contents. jpayne@69: * @param pos On input: an alignment field, if desired. jpayne@69: * On output: the offsets of the alignment field. jpayne@69: * @param status Output param filled with success/failure status. jpayne@69: * @return Reference to 'appendTo' parameter. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual UnicodeString& format(const Formattable& obj, jpayne@69: UnicodeString& appendTo, jpayne@69: FieldPosition& pos, jpayne@69: UErrorCode& status) const; jpayne@69: jpayne@69: /** jpayne@69: * Format an object to produce a string. This method handles jpayne@69: * Formattable objects with numeric types. If the Formattable jpayne@69: * object type is not a numeric type, then it returns a failing jpayne@69: * UErrorCode. jpayne@69: * jpayne@69: * @param obj The object to format. jpayne@69: * @param appendTo Output parameter to receive result. jpayne@69: * Result is appended to existing contents. jpayne@69: * @param posIter On return, can be used to iterate over positions jpayne@69: * of fields generated by this format call. Can be jpayne@69: * NULL. jpayne@69: * @param status Output param filled with success/failure status. jpayne@69: * @return Reference to 'appendTo' parameter. jpayne@69: * @stable ICU 4.4 jpayne@69: */ jpayne@69: virtual UnicodeString& format(const Formattable& obj, jpayne@69: UnicodeString& appendTo, jpayne@69: FieldPositionIterator* posIter, jpayne@69: UErrorCode& status) const; jpayne@69: jpayne@69: /** jpayne@69: * Parse a string to produce an object. This methods handles jpayne@69: * parsing of numeric strings into Formattable objects with numeric jpayne@69: * types. jpayne@69: *

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

jpayne@69: * When parsing, leading whitespace is discarded (with successful jpayne@69: * parse), while trailing whitespace is left as is. jpayne@69: *

jpayne@69: * See Format::parseObject() for more. jpayne@69: * jpayne@69: * @param source The string to be parsed into an object. jpayne@69: * @param result Formattable to be set to the parse result. jpayne@69: * If parse fails, return contents are undefined. jpayne@69: * @param parse_pos The position to start parsing at. Upon return jpayne@69: * this param is set to the position after the jpayne@69: * last character successfully parsed. If the jpayne@69: * source is not parsed successfully, this param jpayne@69: * will remain unchanged. jpayne@69: * @return A newly created Formattable* object, or NULL jpayne@69: * on failure. The caller owns this and should jpayne@69: * delete it when done. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual void parseObject(const UnicodeString& source, jpayne@69: Formattable& result, jpayne@69: ParsePosition& parse_pos) const; jpayne@69: jpayne@69: /** jpayne@69: * Format a double number. These methods call the NumberFormat jpayne@69: * pure virtual format() methods with the default FieldPosition. jpayne@69: * jpayne@69: * @param number The value to be formatted. jpayne@69: * @param appendTo Output parameter to receive result. jpayne@69: * Result is appended to existing contents. jpayne@69: * @return Reference to 'appendTo' parameter. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: UnicodeString& format( double number, jpayne@69: UnicodeString& appendTo) const; jpayne@69: jpayne@69: /** jpayne@69: * Format a long number. These methods call the NumberFormat jpayne@69: * pure virtual format() methods with the default FieldPosition. jpayne@69: * jpayne@69: * @param number The value to be formatted. jpayne@69: * @param appendTo Output parameter to receive result. jpayne@69: * Result is appended to existing contents. jpayne@69: * @return Reference to 'appendTo' parameter. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: UnicodeString& format( int32_t number, jpayne@69: UnicodeString& appendTo) const; jpayne@69: jpayne@69: /** jpayne@69: * Format an int64 number. These methods call the NumberFormat jpayne@69: * pure virtual format() methods with the default FieldPosition. jpayne@69: * jpayne@69: * @param number The value to be formatted. jpayne@69: * @param appendTo Output parameter to receive result. jpayne@69: * Result is appended to existing contents. jpayne@69: * @return Reference to 'appendTo' parameter. jpayne@69: * @stable ICU 2.8 jpayne@69: */ jpayne@69: UnicodeString& format( int64_t number, jpayne@69: UnicodeString& appendTo) const; jpayne@69: jpayne@69: /** jpayne@69: * Format a double number. Concrete subclasses must implement jpayne@69: * these pure virtual methods. jpayne@69: * jpayne@69: * @param number The value to be formatted. jpayne@69: * @param appendTo Output parameter to receive result. jpayne@69: * Result is appended to existing contents. jpayne@69: * @param pos On input: an alignment field, if desired. jpayne@69: * On output: the offsets of the alignment field. jpayne@69: * @return Reference to 'appendTo' parameter. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual UnicodeString& format(double number, jpayne@69: UnicodeString& appendTo, jpayne@69: FieldPosition& pos) const = 0; jpayne@69: /** jpayne@69: * Format a double number. By default, the parent function simply jpayne@69: * calls the base class and does not return an error status. jpayne@69: * Therefore, the status may be ignored in some subclasses. jpayne@69: * jpayne@69: * @param number The value to be formatted. jpayne@69: * @param appendTo Output parameter to receive result. jpayne@69: * Result is appended to existing contents. jpayne@69: * @param pos On input: an alignment field, if desired. jpayne@69: * On output: the offsets of the alignment field. jpayne@69: * @param status error status jpayne@69: * @return Reference to 'appendTo' parameter. jpayne@69: * @internal jpayne@69: */ jpayne@69: virtual UnicodeString& format(double number, jpayne@69: UnicodeString& appendTo, jpayne@69: FieldPosition& pos, jpayne@69: UErrorCode &status) const; jpayne@69: /** jpayne@69: * Format a double number. Subclasses must implement jpayne@69: * this method. jpayne@69: * jpayne@69: * @param number The value to be formatted. jpayne@69: * @param appendTo Output parameter to receive result. jpayne@69: * Result is appended to existing contents. jpayne@69: * @param posIter On return, can be used to iterate over positions jpayne@69: * of fields generated by this format call. jpayne@69: * Can be NULL. jpayne@69: * @param status Output param filled with success/failure status. jpayne@69: * @return Reference to 'appendTo' parameter. jpayne@69: * @stable ICU 4.4 jpayne@69: */ jpayne@69: virtual UnicodeString& format(double number, jpayne@69: UnicodeString& appendTo, jpayne@69: FieldPositionIterator* posIter, jpayne@69: UErrorCode& status) const; jpayne@69: /** jpayne@69: * Format a long number. Concrete subclasses must implement jpayne@69: * these pure virtual methods. jpayne@69: * jpayne@69: * @param number The value to be formatted. jpayne@69: * @param appendTo Output parameter to receive result. jpayne@69: * Result is appended to existing contents. jpayne@69: * @param pos On input: an alignment field, if desired. jpayne@69: * On output: the offsets of the alignment field. jpayne@69: * @return Reference to 'appendTo' parameter. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual UnicodeString& format(int32_t number, jpayne@69: UnicodeString& appendTo, jpayne@69: FieldPosition& pos) const = 0; jpayne@69: jpayne@69: /** jpayne@69: * Format a long number. Concrete subclasses may override jpayne@69: * this function to provide status return. jpayne@69: * jpayne@69: * @param number The value to be formatted. jpayne@69: * @param appendTo Output parameter to receive result. jpayne@69: * Result is appended to existing contents. jpayne@69: * @param pos On input: an alignment field, if desired. jpayne@69: * On output: the offsets of the alignment field. jpayne@69: * @param status the output status. jpayne@69: * @return Reference to 'appendTo' parameter. jpayne@69: * @internal jpayne@69: */ jpayne@69: virtual UnicodeString& format(int32_t number, jpayne@69: UnicodeString& appendTo, jpayne@69: FieldPosition& pos, jpayne@69: UErrorCode &status) const; jpayne@69: jpayne@69: /** jpayne@69: * Format an int32 number. Subclasses must implement jpayne@69: * this method. jpayne@69: * jpayne@69: * @param number The value to be formatted. jpayne@69: * @param appendTo Output parameter to receive result. jpayne@69: * Result is appended to existing contents. jpayne@69: * @param posIter On return, can be used to iterate over positions jpayne@69: * of fields generated by this format call. jpayne@69: * Can be NULL. jpayne@69: * @param status Output param filled with success/failure status. jpayne@69: * @return Reference to 'appendTo' parameter. jpayne@69: * @stable ICU 4.4 jpayne@69: */ jpayne@69: virtual UnicodeString& format(int32_t number, jpayne@69: UnicodeString& appendTo, jpayne@69: FieldPositionIterator* posIter, jpayne@69: UErrorCode& status) const; jpayne@69: /** jpayne@69: * Format an int64 number. (Not abstract to retain compatibility jpayne@69: * with earlier releases, however subclasses should override this jpayne@69: * method as it just delegates to format(int32_t number...); jpayne@69: * jpayne@69: * @param number The value to be formatted. jpayne@69: * @param appendTo Output parameter to receive result. jpayne@69: * Result is appended to existing contents. jpayne@69: * @param pos On input: an alignment field, if desired. jpayne@69: * On output: the offsets of the alignment field. jpayne@69: * @return Reference to 'appendTo' parameter. jpayne@69: * @stable ICU 2.8 jpayne@69: */ jpayne@69: virtual UnicodeString& format(int64_t number, jpayne@69: UnicodeString& appendTo, jpayne@69: FieldPosition& pos) const; jpayne@69: jpayne@69: /** jpayne@69: * Format an int64 number. (Not abstract to retain compatibility jpayne@69: * with earlier releases, however subclasses should override this jpayne@69: * method as it just delegates to format(int32_t number...); jpayne@69: * jpayne@69: * @param number The value to be formatted. jpayne@69: * @param appendTo Output parameter to receive result. jpayne@69: * Result is appended to existing contents. jpayne@69: * @param pos On input: an alignment field, if desired. jpayne@69: * On output: the offsets of the alignment field. jpayne@69: * @param status Output param filled with success/failure status. jpayne@69: * @return Reference to 'appendTo' parameter. jpayne@69: * @internal jpayne@69: */ jpayne@69: virtual UnicodeString& format(int64_t number, jpayne@69: UnicodeString& appendTo, jpayne@69: FieldPosition& pos, jpayne@69: UErrorCode& status) const; jpayne@69: /** jpayne@69: * Format an int64 number. Subclasses must implement jpayne@69: * this method. jpayne@69: * jpayne@69: * @param number The value to be formatted. jpayne@69: * @param appendTo Output parameter to receive result. jpayne@69: * Result is appended to existing contents. jpayne@69: * @param posIter On return, can be used to iterate over positions jpayne@69: * of fields generated by this format call. jpayne@69: * Can be NULL. jpayne@69: * @param status Output param filled with success/failure status. jpayne@69: * @return Reference to 'appendTo' parameter. jpayne@69: * @stable ICU 4.4 jpayne@69: */ jpayne@69: virtual UnicodeString& format(int64_t number, jpayne@69: UnicodeString& appendTo, jpayne@69: FieldPositionIterator* posIter, jpayne@69: UErrorCode& status) const; jpayne@69: jpayne@69: /** jpayne@69: * Format a decimal number. Subclasses must implement jpayne@69: * this method. The syntax of the unformatted number is a "numeric string" jpayne@69: * as defined in the Decimal Arithmetic Specification, available at jpayne@69: * http://speleotrove.com/decimal jpayne@69: * jpayne@69: * @param number The unformatted number, as a string, to be formatted. jpayne@69: * @param appendTo Output parameter to receive result. jpayne@69: * Result is appended to existing contents. jpayne@69: * @param posIter On return, can be used to iterate over positions jpayne@69: * of fields generated by this format call. jpayne@69: * Can be NULL. jpayne@69: * @param status Output param filled with success/failure status. jpayne@69: * @return Reference to 'appendTo' parameter. jpayne@69: * @stable ICU 4.4 jpayne@69: */ jpayne@69: virtual UnicodeString& format(StringPiece number, jpayne@69: UnicodeString& appendTo, jpayne@69: FieldPositionIterator* posIter, jpayne@69: UErrorCode& status) const; jpayne@69: jpayne@69: // Can't use #ifndef U_HIDE_INTERNAL_API because these are virtual methods jpayne@69: jpayne@69: /** jpayne@69: * Format a decimal number. jpayne@69: * The number is a DecimalQuantity wrapper onto a floating point decimal number. jpayne@69: * The default implementation in NumberFormat converts the decimal number jpayne@69: * to a double and formats that. Subclasses of NumberFormat that want jpayne@69: * to specifically handle big decimal numbers must override this method. jpayne@69: * class DecimalFormat does so. jpayne@69: * jpayne@69: * @param number The number, a DecimalQuantity format Decimal Floating Point. jpayne@69: * @param appendTo Output parameter to receive result. jpayne@69: * Result is appended to existing contents. jpayne@69: * @param posIter On return, can be used to iterate over positions jpayne@69: * of fields generated by this format call. jpayne@69: * @param status Output param filled with success/failure status. jpayne@69: * @return Reference to 'appendTo' parameter. jpayne@69: * @internal jpayne@69: */ jpayne@69: virtual UnicodeString& format(const number::impl::DecimalQuantity &number, jpayne@69: UnicodeString& appendTo, jpayne@69: FieldPositionIterator* posIter, jpayne@69: UErrorCode& status) const; jpayne@69: jpayne@69: /** jpayne@69: * Format a decimal number. jpayne@69: * The number is a DecimalQuantity wrapper onto a floating point decimal number. jpayne@69: * The default implementation in NumberFormat converts the decimal number jpayne@69: * to a double and formats that. Subclasses of NumberFormat that want jpayne@69: * to specifically handle big decimal numbers must override this method. jpayne@69: * class DecimalFormat does so. jpayne@69: * jpayne@69: * @param number The number, a DecimalQuantity format Decimal Floating Point. jpayne@69: * @param appendTo Output parameter to receive result. jpayne@69: * Result is appended to existing contents. jpayne@69: * @param pos On input: an alignment field, if desired. jpayne@69: * On output: the offsets of the alignment field. jpayne@69: * @param status Output param filled with success/failure status. jpayne@69: * @return Reference to 'appendTo' parameter. jpayne@69: * @internal jpayne@69: */ jpayne@69: virtual UnicodeString& format(const number::impl::DecimalQuantity &number, jpayne@69: UnicodeString& appendTo, jpayne@69: FieldPosition& pos, jpayne@69: UErrorCode& status) const; jpayne@69: jpayne@69: /** jpayne@69: * Return a long if possible (e.g. within range LONG_MAX, jpayne@69: * LONG_MAX], and with no decimals), otherwise a double. If jpayne@69: * IntegerOnly is set, will stop at a decimal point (or equivalent; jpayne@69: * e.g. for rational numbers "1 2/3", will stop after the 1). jpayne@69: *

jpayne@69: * If no object can be parsed, index is unchanged, and NULL is jpayne@69: * returned. jpayne@69: *

jpayne@69: * This is a pure virtual which concrete subclasses must implement. jpayne@69: * jpayne@69: * @param text The text to be parsed. jpayne@69: * @param result Formattable to be set to the parse result. jpayne@69: * If parse fails, return contents are undefined. jpayne@69: * @param parsePosition The position to start parsing at on input. jpayne@69: * On output, moved to after the last successfully jpayne@69: * parse character. On parse failure, does not change. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual void parse(const UnicodeString& text, jpayne@69: Formattable& result, jpayne@69: ParsePosition& parsePosition) const = 0; jpayne@69: jpayne@69: /** jpayne@69: * Parse a string as a numeric value, and return a Formattable jpayne@69: * numeric object. This method parses integers only if IntegerOnly jpayne@69: * is set. jpayne@69: * jpayne@69: * @param text The text to be parsed. jpayne@69: * @param result Formattable to be set to the parse result. jpayne@69: * If parse fails, return contents are undefined. jpayne@69: * @param status Output parameter set to a failure error code jpayne@69: * when a failure occurs. The error code when the jpayne@69: * string fails to parse is U_INVALID_FORMAT_ERROR, jpayne@69: * unless overridden by a subclass. jpayne@69: * @see NumberFormat::isParseIntegerOnly jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual void parse(const UnicodeString& text, jpayne@69: Formattable& result, jpayne@69: UErrorCode& status) const; jpayne@69: jpayne@69: /** jpayne@69: * Parses text from the given string as a currency amount. Unlike jpayne@69: * the parse() method, this method will attempt to parse a generic jpayne@69: * currency name, searching for a match of this object's locale's jpayne@69: * currency display names, or for a 3-letter ISO currency code. jpayne@69: * This method will fail if this format is not a currency format, jpayne@69: * that is, if it does not contain the currency pattern symbol jpayne@69: * (U+00A4) in its prefix or suffix. jpayne@69: * jpayne@69: * @param text the string to parse jpayne@69: * @param pos input-output position; on input, the position within text jpayne@69: * to match; must have 0 <= pos.getIndex() < text.length(); jpayne@69: * on output, the position after the last matched character. jpayne@69: * If the parse fails, the position in unchanged upon output. jpayne@69: * @return if parse succeeds, a pointer to a newly-created CurrencyAmount jpayne@69: * object (owned by the caller) containing information about jpayne@69: * the parsed currency; if parse fails, this is NULL. jpayne@69: * @stable ICU 49 jpayne@69: */ jpayne@69: virtual CurrencyAmount* parseCurrency(const UnicodeString& text, jpayne@69: ParsePosition& pos) const; jpayne@69: jpayne@69: /** jpayne@69: * Return true if this format will parse numbers as integers jpayne@69: * only. For example in the English locale, with ParseIntegerOnly jpayne@69: * true, the string "1234." would be parsed as the integer value jpayne@69: * 1234 and parsing would stop at the "." character. Of course, jpayne@69: * the exact format accepted by the parse operation is locale jpayne@69: * dependant and determined by sub-classes of NumberFormat. jpayne@69: * @return true if this format will parse numbers as integers jpayne@69: * only. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: UBool isParseIntegerOnly(void) const; jpayne@69: jpayne@69: /** jpayne@69: * Sets whether or not numbers should be parsed as integers only. jpayne@69: * @param value set True, this format will parse numbers as integers jpayne@69: * only. jpayne@69: * @see isParseIntegerOnly jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual void setParseIntegerOnly(UBool value); jpayne@69: jpayne@69: /** jpayne@69: * Sets whether lenient parsing should be enabled (it is off by default). jpayne@69: * jpayne@69: * @param enable \c TRUE if lenient parsing should be used, jpayne@69: * \c FALSE otherwise. jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: virtual void setLenient(UBool enable); jpayne@69: jpayne@69: /** jpayne@69: * Returns whether lenient parsing is enabled (it is off by default). jpayne@69: * jpayne@69: * @return \c TRUE if lenient parsing is enabled, jpayne@69: * \c FALSE otherwise. jpayne@69: * @see #setLenient jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: virtual UBool isLenient(void) const; jpayne@69: jpayne@69: /** jpayne@69: * Create a default style NumberFormat for the current default locale. jpayne@69: * The default formatting style is locale dependent. jpayne@69: *

jpayne@69: * NOTE: New users are strongly encouraged to use jpayne@69: * {@link icu::number::NumberFormatter} instead of NumberFormat. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: static NumberFormat* U_EXPORT2 createInstance(UErrorCode&); jpayne@69: jpayne@69: /** jpayne@69: * Create a default style NumberFormat for the specified locale. jpayne@69: * The default formatting style is locale dependent. jpayne@69: * @param inLocale the given locale. jpayne@69: *

jpayne@69: * NOTE: New users are strongly encouraged to use jpayne@69: * {@link icu::number::NumberFormatter} instead of NumberFormat. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: static NumberFormat* U_EXPORT2 createInstance(const Locale& inLocale, jpayne@69: UErrorCode&); jpayne@69: jpayne@69: /** jpayne@69: * Create a specific style NumberFormat for the specified locale. jpayne@69: *

jpayne@69: * NOTE: New users are strongly encouraged to use jpayne@69: * {@link icu::number::NumberFormatter} instead of NumberFormat. jpayne@69: * @param desiredLocale the given locale. jpayne@69: * @param style the given style. jpayne@69: * @param errorCode Output param filled with success/failure status. jpayne@69: * @return A new NumberFormat instance. jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: static NumberFormat* U_EXPORT2 createInstance(const Locale& desiredLocale, jpayne@69: UNumberFormatStyle style, jpayne@69: UErrorCode& errorCode); jpayne@69: jpayne@69: #ifndef U_HIDE_INTERNAL_API jpayne@69: jpayne@69: /** jpayne@69: * ICU use only. jpayne@69: * Creates NumberFormat instance without using the cache. jpayne@69: * @internal jpayne@69: */ jpayne@69: static NumberFormat* internalCreateInstance( jpayne@69: const Locale& desiredLocale, jpayne@69: UNumberFormatStyle style, jpayne@69: UErrorCode& errorCode); jpayne@69: jpayne@69: /** jpayne@69: * ICU use only. jpayne@69: * Returns handle to the shared, cached NumberFormat instance for given jpayne@69: * locale. On success, caller must call removeRef() on returned value jpayne@69: * once it is done with the shared instance. jpayne@69: * @internal jpayne@69: */ jpayne@69: static const SharedNumberFormat* U_EXPORT2 createSharedInstance( jpayne@69: const Locale& inLocale, UNumberFormatStyle style, UErrorCode& status); jpayne@69: jpayne@69: #endif /* U_HIDE_INTERNAL_API */ jpayne@69: jpayne@69: /** jpayne@69: * Returns a currency format for the current default locale. jpayne@69: *

jpayne@69: * NOTE: New users are strongly encouraged to use jpayne@69: * {@link icu::number::NumberFormatter} instead of NumberFormat. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: static NumberFormat* U_EXPORT2 createCurrencyInstance(UErrorCode&); jpayne@69: jpayne@69: /** jpayne@69: * Returns a currency format for the specified locale. jpayne@69: *

jpayne@69: * NOTE: New users are strongly encouraged to use jpayne@69: * {@link icu::number::NumberFormatter} instead of NumberFormat. jpayne@69: * @param inLocale the given locale. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: static NumberFormat* U_EXPORT2 createCurrencyInstance(const Locale& inLocale, jpayne@69: UErrorCode&); jpayne@69: jpayne@69: /** jpayne@69: * Returns a percentage format for the current default locale. jpayne@69: *

jpayne@69: * NOTE: New users are strongly encouraged to use jpayne@69: * {@link icu::number::NumberFormatter} instead of NumberFormat. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: static NumberFormat* U_EXPORT2 createPercentInstance(UErrorCode&); jpayne@69: jpayne@69: /** jpayne@69: * Returns a percentage format for the specified locale. jpayne@69: *

jpayne@69: * NOTE: New users are strongly encouraged to use jpayne@69: * {@link icu::number::NumberFormatter} instead of NumberFormat. jpayne@69: * @param inLocale the given locale. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: static NumberFormat* U_EXPORT2 createPercentInstance(const Locale& inLocale, jpayne@69: UErrorCode&); jpayne@69: jpayne@69: /** jpayne@69: * Returns a scientific format for the current default locale. jpayne@69: *

jpayne@69: * NOTE: New users are strongly encouraged to use jpayne@69: * {@link icu::number::NumberFormatter} instead of NumberFormat. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: static NumberFormat* U_EXPORT2 createScientificInstance(UErrorCode&); jpayne@69: jpayne@69: /** jpayne@69: * Returns a scientific format for the specified locale. jpayne@69: *

jpayne@69: * NOTE: New users are strongly encouraged to use jpayne@69: * {@link icu::number::NumberFormatter} instead of NumberFormat. jpayne@69: * @param inLocale the given locale. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: static NumberFormat* U_EXPORT2 createScientificInstance(const Locale& inLocale, jpayne@69: UErrorCode&); jpayne@69: jpayne@69: /** jpayne@69: * Get the set of Locales for which NumberFormats are installed. jpayne@69: * @param count Output param to receive the size of the locales jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count); jpayne@69: jpayne@69: #if !UCONFIG_NO_SERVICE jpayne@69: /** jpayne@69: * Register a new NumberFormatFactory. The factory will be adopted. jpayne@69: * Because ICU may choose to cache NumberFormat objects internally, jpayne@69: * this must be called at application startup, prior to any calls to jpayne@69: * NumberFormat::createInstance to avoid undefined behavior. jpayne@69: * @param toAdopt the NumberFormatFactory instance to be adopted jpayne@69: * @param status the in/out status code, no special meanings are assigned jpayne@69: * @return a registry key that can be used to unregister this factory jpayne@69: * @stable ICU 2.6 jpayne@69: */ jpayne@69: static URegistryKey U_EXPORT2 registerFactory(NumberFormatFactory* toAdopt, UErrorCode& status); jpayne@69: jpayne@69: /** jpayne@69: * Unregister a previously-registered NumberFormatFactory using the key returned from the jpayne@69: * register call. Key becomes invalid after a successful call and should not be used again. jpayne@69: * The NumberFormatFactory corresponding to the key will be deleted. jpayne@69: * Because ICU may choose to cache NumberFormat objects internally, jpayne@69: * this should be called during application shutdown, after all calls to jpayne@69: * NumberFormat::createInstance to avoid undefined behavior. jpayne@69: * @param key the registry key returned by a previous call to registerFactory jpayne@69: * @param status the in/out status code, no special meanings are assigned jpayne@69: * @return TRUE if the factory for the key was successfully unregistered jpayne@69: * @stable ICU 2.6 jpayne@69: */ jpayne@69: static UBool U_EXPORT2 unregister(URegistryKey key, UErrorCode& status); jpayne@69: jpayne@69: /** jpayne@69: * Return a StringEnumeration over the locales available at the time of the call, jpayne@69: * including registered locales. jpayne@69: * @return a StringEnumeration over the locales available at the time of the call jpayne@69: * @stable ICU 2.6 jpayne@69: */ jpayne@69: static StringEnumeration* U_EXPORT2 getAvailableLocales(void); jpayne@69: #endif /* UCONFIG_NO_SERVICE */ jpayne@69: jpayne@69: /** jpayne@69: * Returns true if grouping is used in this format. For example, jpayne@69: * in the English locale, with grouping on, the number 1234567 jpayne@69: * might be formatted as "1,234,567". The grouping separator as jpayne@69: * well as the size of each group is locale dependent and is jpayne@69: * determined by sub-classes of NumberFormat. jpayne@69: * @see setGroupingUsed jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: UBool isGroupingUsed(void) const; jpayne@69: jpayne@69: /** jpayne@69: * Set whether or not grouping will be used in this format. jpayne@69: * @param newValue True, grouping will be used in this format. jpayne@69: * @see getGroupingUsed jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual void setGroupingUsed(UBool newValue); jpayne@69: jpayne@69: /** jpayne@69: * Returns the maximum number of digits allowed in the integer portion of a jpayne@69: * number. jpayne@69: * @return the maximum number of digits allowed in the integer portion of a jpayne@69: * number. jpayne@69: * @see setMaximumIntegerDigits jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: int32_t getMaximumIntegerDigits(void) const; jpayne@69: jpayne@69: /** jpayne@69: * Sets the maximum number of digits allowed in the integer portion of a jpayne@69: * number. maximumIntegerDigits must be >= minimumIntegerDigits. If the jpayne@69: * new value for maximumIntegerDigits is less than the current value jpayne@69: * of minimumIntegerDigits, then minimumIntegerDigits will also be set to jpayne@69: * the new value. jpayne@69: * jpayne@69: * @param newValue the new value for the maximum number of digits jpayne@69: * allowed in the integer portion of a number. jpayne@69: * @see getMaximumIntegerDigits jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual void setMaximumIntegerDigits(int32_t newValue); jpayne@69: jpayne@69: /** jpayne@69: * Returns the minimum number of digits allowed in the integer portion of a jpayne@69: * number. jpayne@69: * @return the minimum number of digits allowed in the integer portion of a jpayne@69: * number. jpayne@69: * @see setMinimumIntegerDigits jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: int32_t getMinimumIntegerDigits(void) const; jpayne@69: jpayne@69: /** jpayne@69: * Sets the minimum number of digits allowed in the integer portion of a jpayne@69: * number. minimumIntegerDigits must be <= maximumIntegerDigits. If the jpayne@69: * new value for minimumIntegerDigits exceeds the current value jpayne@69: * of maximumIntegerDigits, then maximumIntegerDigits will also be set to jpayne@69: * the new value. jpayne@69: * @param newValue the new value to be set. jpayne@69: * @see getMinimumIntegerDigits jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual void setMinimumIntegerDigits(int32_t newValue); jpayne@69: jpayne@69: /** jpayne@69: * Returns the maximum number of digits allowed in the fraction portion of a jpayne@69: * number. jpayne@69: * @return the maximum number of digits allowed in the fraction portion of a jpayne@69: * number. jpayne@69: * @see setMaximumFractionDigits jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: int32_t getMaximumFractionDigits(void) const; jpayne@69: jpayne@69: /** jpayne@69: * Sets the maximum number of digits allowed in the fraction portion of a jpayne@69: * number. maximumFractionDigits must be >= minimumFractionDigits. If the jpayne@69: * new value for maximumFractionDigits is less than the current value jpayne@69: * of minimumFractionDigits, then minimumFractionDigits will also be set to jpayne@69: * the new value. jpayne@69: * @param newValue the new value to be set. jpayne@69: * @see getMaximumFractionDigits jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual void setMaximumFractionDigits(int32_t newValue); jpayne@69: jpayne@69: /** jpayne@69: * Returns the minimum number of digits allowed in the fraction portion of a jpayne@69: * number. jpayne@69: * @return the minimum number of digits allowed in the fraction portion of a jpayne@69: * number. jpayne@69: * @see setMinimumFractionDigits jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: int32_t getMinimumFractionDigits(void) const; jpayne@69: jpayne@69: /** jpayne@69: * Sets the minimum number of digits allowed in the fraction portion of a jpayne@69: * number. minimumFractionDigits must be <= maximumFractionDigits. If the jpayne@69: * new value for minimumFractionDigits exceeds the current value jpayne@69: * of maximumFractionDigits, then maximumIntegerDigits will also be set to jpayne@69: * the new value jpayne@69: * @param newValue the new value to be set. jpayne@69: * @see getMinimumFractionDigits jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual void setMinimumFractionDigits(int32_t newValue); jpayne@69: jpayne@69: /** jpayne@69: * Sets the currency used to display currency jpayne@69: * amounts. This takes effect immediately, if this format is a jpayne@69: * currency format. If this format is not a currency format, then jpayne@69: * the currency is used if and when this object becomes a jpayne@69: * currency format. jpayne@69: * @param theCurrency a 3-letter ISO code indicating new currency jpayne@69: * to use. It need not be null-terminated. May be the empty jpayne@69: * string or NULL to indicate no currency. jpayne@69: * @param ec input-output error code jpayne@69: * @stable ICU 3.0 jpayne@69: */ jpayne@69: virtual void setCurrency(const char16_t* theCurrency, UErrorCode& ec); jpayne@69: jpayne@69: /** jpayne@69: * Gets the currency used to display currency jpayne@69: * amounts. This may be an empty string for some subclasses. jpayne@69: * @return a 3-letter null-terminated ISO code indicating jpayne@69: * the currency in use, or a pointer to the empty string. jpayne@69: * @stable ICU 2.6 jpayne@69: */ jpayne@69: const char16_t* getCurrency() const; jpayne@69: jpayne@69: /** jpayne@69: * Set a particular UDisplayContext value in the formatter, such as jpayne@69: * UDISPCTX_CAPITALIZATION_FOR_STANDALONE. jpayne@69: * @param value The UDisplayContext value to set. jpayne@69: * @param status Input/output status. If at entry this indicates a failure jpayne@69: * status, the function will do nothing; otherwise this will be jpayne@69: * updated with any new status from the function. jpayne@69: * @stable ICU 53 jpayne@69: */ jpayne@69: virtual void setContext(UDisplayContext value, UErrorCode& status); jpayne@69: jpayne@69: /** jpayne@69: * Get the formatter's UDisplayContext value for the specified UDisplayContextType, jpayne@69: * such as UDISPCTX_TYPE_CAPITALIZATION. jpayne@69: * @param type The UDisplayContextType whose value to return jpayne@69: * @param status Input/output status. If at entry this indicates a failure jpayne@69: * status, the function will do nothing; otherwise this will be jpayne@69: * updated with any new status from the function. jpayne@69: * @return The UDisplayContextValue for the specified type. jpayne@69: * @stable ICU 53 jpayne@69: */ jpayne@69: virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const; jpayne@69: jpayne@69: /** jpayne@69: * Get the rounding mode. This will always return NumberFormat::ERoundingMode::kRoundUnnecessary jpayne@69: * if the subclass does not support rounding. jpayne@69: * @return A rounding mode jpayne@69: * @stable ICU 60 jpayne@69: */ jpayne@69: virtual ERoundingMode getRoundingMode(void) const; jpayne@69: jpayne@69: /** jpayne@69: * Set the rounding mode. If a subclass does not support rounding, this will do nothing. jpayne@69: * @param roundingMode A rounding mode jpayne@69: * @stable ICU 60 jpayne@69: */ jpayne@69: virtual void setRoundingMode(ERoundingMode roundingMode); jpayne@69: jpayne@69: public: jpayne@69: jpayne@69: /** jpayne@69: * Return the class ID for this class. This is useful for jpayne@69: * comparing to a return value from getDynamicClassID(). Note that, jpayne@69: * because NumberFormat is an abstract base class, no fully constructed object jpayne@69: * will have the class ID returned by NumberFormat::getStaticClassID(). jpayne@69: * @return The class ID for all objects of this class. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: static UClassID U_EXPORT2 getStaticClassID(void); jpayne@69: jpayne@69: /** jpayne@69: * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. jpayne@69: * This method is to implement a simple version of RTTI, since not all jpayne@69: * C++ compilers support genuine RTTI. Polymorphic operator==() and jpayne@69: * clone() methods call this method. jpayne@69: *

jpayne@69: * @return The class ID for this object. All objects of a jpayne@69: * given class have the same class ID. Objects of jpayne@69: * other classes have different class IDs. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual UClassID getDynamicClassID(void) const = 0; jpayne@69: jpayne@69: protected: jpayne@69: jpayne@69: /** jpayne@69: * Default constructor for subclass use only. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: NumberFormat(); jpayne@69: jpayne@69: /** jpayne@69: * Copy constructor. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: NumberFormat(const NumberFormat&); jpayne@69: jpayne@69: /** jpayne@69: * Assignment operator. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: NumberFormat& operator=(const NumberFormat&); jpayne@69: jpayne@69: /** jpayne@69: * Returns the currency in effect for this formatter. Subclasses jpayne@69: * should override this method as needed. Unlike getCurrency(), jpayne@69: * this method should never return "". jpayne@69: * @result output parameter for null-terminated result, which must jpayne@69: * have a capacity of at least 4 jpayne@69: * @internal jpayne@69: */ jpayne@69: virtual void getEffectiveCurrency(char16_t* result, UErrorCode& ec) const; jpayne@69: jpayne@69: #ifndef U_HIDE_INTERNAL_API jpayne@69: /** jpayne@69: * Creates the specified number format style of the desired locale. jpayne@69: * If mustBeDecimalFormat is TRUE, then the returned pointer is jpayne@69: * either a DecimalFormat or it is NULL. jpayne@69: * @internal jpayne@69: */ jpayne@69: static NumberFormat* makeInstance(const Locale& desiredLocale, jpayne@69: UNumberFormatStyle style, jpayne@69: UBool mustBeDecimalFormat, jpayne@69: UErrorCode& errorCode); jpayne@69: #endif /* U_HIDE_INTERNAL_API */ jpayne@69: jpayne@69: private: jpayne@69: jpayne@69: static UBool isStyleSupported(UNumberFormatStyle style); jpayne@69: jpayne@69: /** jpayne@69: * Creates the specified decimal format style of the desired locale. jpayne@69: * @param desiredLocale the given locale. jpayne@69: * @param style the given style. jpayne@69: * @param errorCode Output param filled with success/failure status. jpayne@69: * @return A new NumberFormat instance. jpayne@69: */ jpayne@69: static NumberFormat* makeInstance(const Locale& desiredLocale, jpayne@69: UNumberFormatStyle style, jpayne@69: UErrorCode& errorCode); jpayne@69: jpayne@69: UBool fGroupingUsed; jpayne@69: int32_t fMaxIntegerDigits; jpayne@69: int32_t fMinIntegerDigits; jpayne@69: int32_t fMaxFractionDigits; jpayne@69: int32_t fMinFractionDigits; jpayne@69: jpayne@69: protected: jpayne@69: /** \internal */ jpayne@69: static const int32_t gDefaultMaxIntegerDigits; jpayne@69: /** \internal */ jpayne@69: static const int32_t gDefaultMinIntegerDigits; jpayne@69: jpayne@69: private: jpayne@69: UBool fParseIntegerOnly; jpayne@69: UBool fLenient; // TRUE => lenient parse is enabled jpayne@69: jpayne@69: // ISO currency code jpayne@69: char16_t fCurrency[4]; jpayne@69: jpayne@69: UDisplayContext fCapitalizationContext; jpayne@69: jpayne@69: friend class ICUNumberFormatFactory; // access to makeInstance jpayne@69: friend class ICUNumberFormatService; jpayne@69: friend class ::NumberFormatTest; // access to isStyleSupported() jpayne@69: }; jpayne@69: jpayne@69: #if !UCONFIG_NO_SERVICE jpayne@69: /** jpayne@69: * A NumberFormatFactory is used to register new number formats. The factory jpayne@69: * should be able to create any of the predefined formats for each locale it jpayne@69: * supports. When registered, the locales it supports extend or override the jpayne@69: * locale already supported by ICU. jpayne@69: * jpayne@69: * @stable ICU 2.6 jpayne@69: */ jpayne@69: class U_I18N_API NumberFormatFactory : public UObject { jpayne@69: public: jpayne@69: jpayne@69: /** jpayne@69: * Destructor jpayne@69: * @stable ICU 3.0 jpayne@69: */ jpayne@69: virtual ~NumberFormatFactory(); jpayne@69: jpayne@69: /** jpayne@69: * Return true if this factory will be visible. Default is true. jpayne@69: * If not visible, the locales supported by this factory will not jpayne@69: * be listed by getAvailableLocales. jpayne@69: * @stable ICU 2.6 jpayne@69: */ jpayne@69: virtual UBool visible(void) const = 0; jpayne@69: jpayne@69: /** jpayne@69: * Return the locale names directly supported by this factory. The number of names jpayne@69: * is returned in count; jpayne@69: * @stable ICU 2.6 jpayne@69: */ jpayne@69: virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const = 0; jpayne@69: jpayne@69: /** jpayne@69: * Return a number format of the appropriate type. If the locale jpayne@69: * is not supported, return null. If the locale is supported, but jpayne@69: * the type is not provided by this service, return null. Otherwise jpayne@69: * return an appropriate instance of NumberFormat. jpayne@69: * @stable ICU 2.6 jpayne@69: */ jpayne@69: virtual NumberFormat* createFormat(const Locale& loc, UNumberFormatStyle formatType) = 0; jpayne@69: }; jpayne@69: jpayne@69: /** jpayne@69: * A NumberFormatFactory that supports a single locale. It can be visible or invisible. jpayne@69: * @stable ICU 2.6 jpayne@69: */ jpayne@69: class U_I18N_API SimpleNumberFormatFactory : public NumberFormatFactory { jpayne@69: protected: jpayne@69: /** jpayne@69: * True if the locale supported by this factory is visible. jpayne@69: * @stable ICU 2.6 jpayne@69: */ jpayne@69: const UBool _visible; jpayne@69: jpayne@69: /** jpayne@69: * The locale supported by this factory, as a UnicodeString. jpayne@69: * @stable ICU 2.6 jpayne@69: */ jpayne@69: UnicodeString _id; jpayne@69: jpayne@69: public: jpayne@69: /** jpayne@69: * @stable ICU 2.6 jpayne@69: */ jpayne@69: SimpleNumberFormatFactory(const Locale& locale, UBool visible = TRUE); jpayne@69: jpayne@69: /** jpayne@69: * @stable ICU 3.0 jpayne@69: */ jpayne@69: virtual ~SimpleNumberFormatFactory(); jpayne@69: jpayne@69: /** jpayne@69: * @stable ICU 2.6 jpayne@69: */ jpayne@69: virtual UBool visible(void) const; jpayne@69: jpayne@69: /** jpayne@69: * @stable ICU 2.6 jpayne@69: */ jpayne@69: virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const; jpayne@69: }; jpayne@69: #endif /* #if !UCONFIG_NO_SERVICE */ jpayne@69: jpayne@69: // ------------------------------------- jpayne@69: jpayne@69: inline UBool jpayne@69: NumberFormat::isParseIntegerOnly() const jpayne@69: { jpayne@69: return fParseIntegerOnly; jpayne@69: } jpayne@69: jpayne@69: inline UBool jpayne@69: NumberFormat::isLenient() const jpayne@69: { jpayne@69: return fLenient; jpayne@69: } jpayne@69: jpayne@69: U_NAMESPACE_END jpayne@69: jpayne@69: #endif /* #if !UCONFIG_NO_FORMATTING */ jpayne@69: jpayne@69: #endif /* U_SHOW_CPLUSPLUS_API */ jpayne@69: jpayne@69: #endif // _NUMFMT jpayne@69: //eof