michael@0: /* michael@0: ******************************************************************************** michael@0: * Copyright (C) 2012-2013, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ******************************************************************************** michael@0: * michael@0: * File COMPACTDECIMALFORMAT.H michael@0: ******************************************************************************** michael@0: */ michael@0: michael@0: #ifndef __COMPACT_DECIMAL_FORMAT_H__ michael@0: #define __COMPACT_DECIMAL_FORMAT_H__ michael@0: michael@0: #include "unicode/utypes.h" michael@0: /** michael@0: * \file michael@0: * \brief C++ API: Formats decimal numbers in compact form. michael@0: */ michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: #ifndef U_HIDE_DRAFT_API michael@0: michael@0: #include "unicode/decimfmt.h" michael@0: michael@0: struct UHashtable; michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: class PluralRules; michael@0: michael@0: /** michael@0: * The CompactDecimalFormat produces abbreviated numbers, suitable for display in michael@0: * environments will limited real estate. For example, 'Hits: 1.2B' instead of michael@0: * 'Hits: 1,200,000,000'. The format will be appropriate for the given language, michael@0: * such as "1,2 Mrd." for German. michael@0: *

michael@0: * For numbers under 1000 trillion (under 10^15, such as 123,456,789,012,345), michael@0: * the result will be short for supported languages. However, the result may michael@0: * sometimes exceed 7 characters, such as when there are combining marks or thin michael@0: * characters. In such cases, the visual width in fonts should still be short. michael@0: *

michael@0: * By default, there are 3 significant digits. After creation, if more than michael@0: * three significant digits are set (with setMaximumSignificantDigits), or if a michael@0: * fixed number of digits are set (with setMaximumIntegerDigits or michael@0: * setMaximumFractionDigits), then result may be wider. michael@0: *

michael@0: * At this time, parsing is not supported, and will produce a U_UNSUPPORTED_ERROR. michael@0: * Resetting the pattern prefixes or suffixes is not supported; the method calls michael@0: * are ignored. michael@0: *

michael@0: * @draft ICU 51 michael@0: */ michael@0: class U_I18N_API CompactDecimalFormat : public DecimalFormat { michael@0: public: michael@0: michael@0: /** michael@0: * Returns a compact decimal instance for specified locale. michael@0: * @param inLocale the given locale. michael@0: * @param style whether to use short or long style. michael@0: * @param status error code returned here. michael@0: * @draft ICU 51 michael@0: */ michael@0: static CompactDecimalFormat* U_EXPORT2 createInstance( michael@0: const Locale& inLocale, UNumberCompactStyle style, UErrorCode& status); michael@0: michael@0: /** michael@0: * Copy constructor. michael@0: * michael@0: * @param source the DecimalFormat object to be copied from. michael@0: * @draft ICU 51 michael@0: */ michael@0: CompactDecimalFormat(const CompactDecimalFormat& source); michael@0: michael@0: /** michael@0: * Destructor. michael@0: * @draft ICU 51 michael@0: */ michael@0: virtual ~CompactDecimalFormat(); michael@0: michael@0: /** michael@0: * Assignment operator. michael@0: * michael@0: * @param rhs the DecimalFormat object to be copied. michael@0: * @draft ICU 51 michael@0: */ michael@0: CompactDecimalFormat& operator=(const CompactDecimalFormat& rhs); michael@0: michael@0: /** michael@0: * Clone this Format object polymorphically. The caller owns the michael@0: * result and should delete it when done. michael@0: * michael@0: * @return a polymorphic copy of this CompactDecimalFormat. michael@0: * @draft ICU 51 michael@0: */ michael@0: virtual Format* clone() const; michael@0: michael@0: /** michael@0: * Return TRUE if the given Format objects are semantically equal. michael@0: * Objects of different subclasses are considered unequal. michael@0: * michael@0: * @param other the object to be compared with. michael@0: * @return TRUE if the given Format objects are semantically equal. michael@0: * @draft ICU 51 michael@0: */ michael@0: virtual UBool operator==(const Format& other) const; michael@0: michael@0: michael@0: using DecimalFormat::format; michael@0: michael@0: /** michael@0: * Format a double or long number using base-10 representation. michael@0: * michael@0: * @param number The value to be formatted. michael@0: * @param appendTo Output parameter to receive result. michael@0: * Result is appended to existing contents. michael@0: * @param pos On input: an alignment field, if desired. michael@0: * On output: the offsets of the alignment field. michael@0: * @return Reference to 'appendTo' parameter. michael@0: * @draft ICU 51 michael@0: */ michael@0: virtual UnicodeString& format(double number, michael@0: UnicodeString& appendTo, michael@0: FieldPosition& pos) const; michael@0: michael@0: /** michael@0: * Format a double or long number using base-10 representation. michael@0: * Currently sets status to U_UNSUPPORTED_ERROR. michael@0: * michael@0: * @param number The value to be formatted. michael@0: * @param appendTo Output parameter to receive result. michael@0: * Result is appended to existing contents. michael@0: * @param posIter On return, can be used to iterate over positions michael@0: * of fields generated by this format call. michael@0: * Can be NULL. michael@0: * @param status Output param filled with success/failure status. michael@0: * @return Reference to 'appendTo' parameter. michael@0: * @internal michael@0: */ michael@0: virtual UnicodeString& format(double number, michael@0: UnicodeString& appendTo, michael@0: FieldPositionIterator* posIter, michael@0: UErrorCode& status) const; michael@0: michael@0: /** michael@0: * Format an int64 number using base-10 representation. michael@0: * michael@0: * @param number The value to be formatted. michael@0: * @param appendTo Output parameter to receive result. michael@0: * Result is appended to existing contents. michael@0: * @param pos On input: an alignment field, if desired. michael@0: * On output: the offsets of the alignment field. michael@0: * @return Reference to 'appendTo' parameter. michael@0: * @draft ICU 51 michael@0: */ michael@0: virtual UnicodeString& format(int64_t number, michael@0: UnicodeString& appendTo, michael@0: FieldPosition& pos) const; michael@0: michael@0: /** michael@0: * Format an int64 number using base-10 representation. michael@0: * Currently sets status to U_UNSUPPORTED_ERROR michael@0: * michael@0: * @param number The value to be formatted. michael@0: * @param appendTo Output parameter to receive result. michael@0: * Result is appended to existing contents. michael@0: * @param posIter On return, can be used to iterate over positions michael@0: * of fields generated by this format call. michael@0: * Can be NULL. michael@0: * @param status Output param filled with success/failure status. michael@0: * @return Reference to 'appendTo' parameter. michael@0: * @internal michael@0: */ michael@0: virtual UnicodeString& format(int64_t number, michael@0: UnicodeString& appendTo, michael@0: FieldPositionIterator* posIter, michael@0: UErrorCode& status) const; michael@0: michael@0: /** michael@0: * Format a decimal number. Currently sets status to U_UNSUPPORTED_ERROR michael@0: * The syntax of the unformatted number is a "numeric string" michael@0: * as defined in the Decimal Arithmetic Specification, available at michael@0: * http://speleotrove.com/decimal michael@0: * michael@0: * @param number The unformatted number, as a string. michael@0: * @param appendTo Output parameter to receive result. michael@0: * Result is appended to existing contents. michael@0: * @param posIter On return, can be used to iterate over positions michael@0: * of fields generated by this format call. michael@0: * Can be NULL. michael@0: * @param status Output param filled with success/failure status. michael@0: * @return Reference to 'appendTo' parameter. michael@0: * @internal michael@0: */ michael@0: virtual UnicodeString& format(const StringPiece &number, michael@0: UnicodeString& appendTo, michael@0: FieldPositionIterator* posIter, michael@0: UErrorCode& status) const; michael@0: michael@0: /** michael@0: * Format a decimal number. Currently sets status to U_UNSUPPORTED_ERROR michael@0: * The number is a DigitList wrapper onto a floating point decimal number. michael@0: * The default implementation in NumberFormat converts the decimal number michael@0: * to a double and formats that. michael@0: * michael@0: * @param number The number, a DigitList format Decimal Floating Point. michael@0: * @param appendTo Output parameter to receive result. michael@0: * Result is appended to existing contents. michael@0: * @param posIter On return, can be used to iterate over positions michael@0: * of fields generated by this format call. michael@0: * @param status Output param filled with success/failure status. michael@0: * @return Reference to 'appendTo' parameter. michael@0: * @internal michael@0: */ michael@0: virtual UnicodeString& format(const DigitList &number, michael@0: UnicodeString& appendTo, michael@0: FieldPositionIterator* posIter, michael@0: UErrorCode& status) const; michael@0: michael@0: /** michael@0: * Format a decimal number. Currently sets status to U_UNSUPPORTED_ERROR. michael@0: * The number is a DigitList wrapper onto a floating point decimal number. michael@0: * The default implementation in NumberFormat converts the decimal number michael@0: * to a double and formats that. michael@0: * michael@0: * @param number The number, a DigitList format Decimal Floating Point. michael@0: * @param appendTo Output parameter to receive result. michael@0: * Result is appended to existing contents. michael@0: * @param pos On input: an alignment field, if desired. michael@0: * On output: the offsets of the alignment field. michael@0: * @param status Output param filled with success/failure status. michael@0: * @return Reference to 'appendTo' parameter. michael@0: * @internal michael@0: */ michael@0: virtual UnicodeString& format(const DigitList &number, michael@0: UnicodeString& appendTo, michael@0: FieldPosition& pos, michael@0: UErrorCode& status) const; michael@0: michael@0: /** michael@0: * CompactDecimalFormat does not support parsing. This implementation michael@0: * does nothing. michael@0: * @param text Unused. michael@0: * @param result Does not change. michael@0: * @param parsePosition Does not change. michael@0: * @see Formattable michael@0: * @draft ICU 51 michael@0: */ michael@0: virtual void parse(const UnicodeString& text, michael@0: Formattable& result, michael@0: ParsePosition& parsePosition) const; michael@0: michael@0: /** michael@0: * CompactDecimalFormat does not support parsing. This implementation michael@0: * sets status to U_UNSUPPORTED_ERROR michael@0: * michael@0: * @param text Unused. michael@0: * @param result Does not change. michael@0: * @param status Always set to U_UNSUPPORTED_ERROR. michael@0: * @draft ICU 51 michael@0: */ michael@0: virtual void parse(const UnicodeString& text, michael@0: Formattable& result, michael@0: UErrorCode& status) const; michael@0: michael@0: /* Cannot use #ifndef U_HIDE_INTERNAL_API for the following draft method since it is virtual */ michael@0: /** michael@0: * Parses text from the given string as a currency amount. Unlike michael@0: * the parse() method, this method will attempt to parse a generic michael@0: * currency name, searching for a match of this object's locale's michael@0: * currency display names, or for a 3-letter ISO currency code. michael@0: * This method will fail if this format is not a currency format, michael@0: * that is, if it does not contain the currency pattern symbol michael@0: * (U+00A4) in its prefix or suffix. This implementation always returns michael@0: * NULL. michael@0: * michael@0: * @param text the string to parse michael@0: * @param pos input-output position; on input, the position within text michael@0: * to match; must have 0 <= pos.getIndex() < text.length(); michael@0: * on output, the position after the last matched character. michael@0: * If the parse fails, the position in unchanged upon output. michael@0: * @return if parse succeeds, a pointer to a newly-created CurrencyAmount michael@0: * object (owned by the caller) containing information about michael@0: * the parsed currency; if parse fails, this is NULL. michael@0: * @internal michael@0: */ michael@0: virtual CurrencyAmount* parseCurrency(const UnicodeString& text, michael@0: ParsePosition& pos) const; michael@0: michael@0: /** michael@0: * Return the class ID for this class. This is useful only for michael@0: * comparing to a return value from getDynamicClassID(). For example: michael@0: *

michael@0:      * .      Base* polymorphic_pointer = createPolymorphicObject();
michael@0:      * .      if (polymorphic_pointer->getDynamicClassID() ==
michael@0:      * .          Derived::getStaticClassID()) ...
michael@0:      * 
michael@0: * @return The class ID for all objects of this class. michael@0: * @draft ICU 51 michael@0: */ michael@0: static UClassID U_EXPORT2 getStaticClassID(); michael@0: michael@0: /** michael@0: * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. michael@0: * This method is to implement a simple version of RTTI, since not all michael@0: * C++ compilers support genuine RTTI. Polymorphic operator==() and michael@0: * clone() methods call this method. michael@0: * michael@0: * @return The class ID for this object. All objects of a michael@0: * given class have the same class ID. Objects of michael@0: * other classes have different class IDs. michael@0: * @draft ICU 51 michael@0: */ michael@0: virtual UClassID getDynamicClassID() const; michael@0: michael@0: private: michael@0: michael@0: const UHashtable* _unitsByVariant; michael@0: const double* _divisors; michael@0: PluralRules* _pluralRules; michael@0: michael@0: // Default constructor not implemented. michael@0: CompactDecimalFormat(const DecimalFormat &, const UHashtable* unitsByVariant, const double* divisors, PluralRules* pluralRules); michael@0: michael@0: UBool eqHelper(const CompactDecimalFormat& that) const; michael@0: }; michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif /* U_HIDE_DRAFT_API */ michael@0: #endif /* #if !UCONFIG_NO_FORMATTING */ michael@0: michael@0: #endif // __COMPACT_DECIMAL_FORMAT_H__ michael@0: //eof