1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/i18n/unicode/compactdecimalformat.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,333 @@ 1.4 +/* 1.5 +******************************************************************************** 1.6 +* Copyright (C) 2012-2013, International Business Machines 1.7 +* Corporation and others. All Rights Reserved. 1.8 +******************************************************************************** 1.9 +* 1.10 +* File COMPACTDECIMALFORMAT.H 1.11 +******************************************************************************** 1.12 +*/ 1.13 + 1.14 +#ifndef __COMPACT_DECIMAL_FORMAT_H__ 1.15 +#define __COMPACT_DECIMAL_FORMAT_H__ 1.16 + 1.17 +#include "unicode/utypes.h" 1.18 +/** 1.19 + * \file 1.20 + * \brief C++ API: Formats decimal numbers in compact form. 1.21 + */ 1.22 + 1.23 +#if !UCONFIG_NO_FORMATTING 1.24 +#ifndef U_HIDE_DRAFT_API 1.25 + 1.26 +#include "unicode/decimfmt.h" 1.27 + 1.28 +struct UHashtable; 1.29 + 1.30 +U_NAMESPACE_BEGIN 1.31 + 1.32 +class PluralRules; 1.33 + 1.34 +/** 1.35 + * The CompactDecimalFormat produces abbreviated numbers, suitable for display in 1.36 + * environments will limited real estate. For example, 'Hits: 1.2B' instead of 1.37 + * 'Hits: 1,200,000,000'. The format will be appropriate for the given language, 1.38 + * such as "1,2 Mrd." for German. 1.39 + * <p> 1.40 + * For numbers under 1000 trillion (under 10^15, such as 123,456,789,012,345), 1.41 + * the result will be short for supported languages. However, the result may 1.42 + * sometimes exceed 7 characters, such as when there are combining marks or thin 1.43 + * characters. In such cases, the visual width in fonts should still be short. 1.44 + * <p> 1.45 + * By default, there are 3 significant digits. After creation, if more than 1.46 + * three significant digits are set (with setMaximumSignificantDigits), or if a 1.47 + * fixed number of digits are set (with setMaximumIntegerDigits or 1.48 + * setMaximumFractionDigits), then result may be wider. 1.49 + * <p> 1.50 + * At this time, parsing is not supported, and will produce a U_UNSUPPORTED_ERROR. 1.51 + * Resetting the pattern prefixes or suffixes is not supported; the method calls 1.52 + * are ignored. 1.53 + * <p> 1.54 + * @draft ICU 51 1.55 + */ 1.56 +class U_I18N_API CompactDecimalFormat : public DecimalFormat { 1.57 +public: 1.58 + 1.59 + /** 1.60 + * Returns a compact decimal instance for specified locale. 1.61 + * @param inLocale the given locale. 1.62 + * @param style whether to use short or long style. 1.63 + * @param status error code returned here. 1.64 + * @draft ICU 51 1.65 + */ 1.66 + static CompactDecimalFormat* U_EXPORT2 createInstance( 1.67 + const Locale& inLocale, UNumberCompactStyle style, UErrorCode& status); 1.68 + 1.69 + /** 1.70 + * Copy constructor. 1.71 + * 1.72 + * @param source the DecimalFormat object to be copied from. 1.73 + * @draft ICU 51 1.74 + */ 1.75 + CompactDecimalFormat(const CompactDecimalFormat& source); 1.76 + 1.77 + /** 1.78 + * Destructor. 1.79 + * @draft ICU 51 1.80 + */ 1.81 + virtual ~CompactDecimalFormat(); 1.82 + 1.83 + /** 1.84 + * Assignment operator. 1.85 + * 1.86 + * @param rhs the DecimalFormat object to be copied. 1.87 + * @draft ICU 51 1.88 + */ 1.89 + CompactDecimalFormat& operator=(const CompactDecimalFormat& rhs); 1.90 + 1.91 + /** 1.92 + * Clone this Format object polymorphically. The caller owns the 1.93 + * result and should delete it when done. 1.94 + * 1.95 + * @return a polymorphic copy of this CompactDecimalFormat. 1.96 + * @draft ICU 51 1.97 + */ 1.98 + virtual Format* clone() const; 1.99 + 1.100 + /** 1.101 + * Return TRUE if the given Format objects are semantically equal. 1.102 + * Objects of different subclasses are considered unequal. 1.103 + * 1.104 + * @param other the object to be compared with. 1.105 + * @return TRUE if the given Format objects are semantically equal. 1.106 + * @draft ICU 51 1.107 + */ 1.108 + virtual UBool operator==(const Format& other) const; 1.109 + 1.110 + 1.111 + using DecimalFormat::format; 1.112 + 1.113 + /** 1.114 + * Format a double or long number using base-10 representation. 1.115 + * 1.116 + * @param number The value to be formatted. 1.117 + * @param appendTo Output parameter to receive result. 1.118 + * Result is appended to existing contents. 1.119 + * @param pos On input: an alignment field, if desired. 1.120 + * On output: the offsets of the alignment field. 1.121 + * @return Reference to 'appendTo' parameter. 1.122 + * @draft ICU 51 1.123 + */ 1.124 + virtual UnicodeString& format(double number, 1.125 + UnicodeString& appendTo, 1.126 + FieldPosition& pos) const; 1.127 + 1.128 + /** 1.129 + * Format a double or long number using base-10 representation. 1.130 + * Currently sets status to U_UNSUPPORTED_ERROR. 1.131 + * 1.132 + * @param number The value to be formatted. 1.133 + * @param appendTo Output parameter to receive result. 1.134 + * Result is appended to existing contents. 1.135 + * @param posIter On return, can be used to iterate over positions 1.136 + * of fields generated by this format call. 1.137 + * Can be NULL. 1.138 + * @param status Output param filled with success/failure status. 1.139 + * @return Reference to 'appendTo' parameter. 1.140 + * @internal 1.141 + */ 1.142 + virtual UnicodeString& format(double number, 1.143 + UnicodeString& appendTo, 1.144 + FieldPositionIterator* posIter, 1.145 + UErrorCode& status) const; 1.146 + 1.147 + /** 1.148 + * Format an int64 number using base-10 representation. 1.149 + * 1.150 + * @param number The value to be formatted. 1.151 + * @param appendTo Output parameter to receive result. 1.152 + * Result is appended to existing contents. 1.153 + * @param pos On input: an alignment field, if desired. 1.154 + * On output: the offsets of the alignment field. 1.155 + * @return Reference to 'appendTo' parameter. 1.156 + * @draft ICU 51 1.157 + */ 1.158 + virtual UnicodeString& format(int64_t number, 1.159 + UnicodeString& appendTo, 1.160 + FieldPosition& pos) const; 1.161 + 1.162 + /** 1.163 + * Format an int64 number using base-10 representation. 1.164 + * Currently sets status to U_UNSUPPORTED_ERROR 1.165 + * 1.166 + * @param number The value to be formatted. 1.167 + * @param appendTo Output parameter to receive result. 1.168 + * Result is appended to existing contents. 1.169 + * @param posIter On return, can be used to iterate over positions 1.170 + * of fields generated by this format call. 1.171 + * Can be NULL. 1.172 + * @param status Output param filled with success/failure status. 1.173 + * @return Reference to 'appendTo' parameter. 1.174 + * @internal 1.175 + */ 1.176 + virtual UnicodeString& format(int64_t number, 1.177 + UnicodeString& appendTo, 1.178 + FieldPositionIterator* posIter, 1.179 + UErrorCode& status) const; 1.180 + 1.181 + /** 1.182 + * Format a decimal number. Currently sets status to U_UNSUPPORTED_ERROR 1.183 + * The syntax of the unformatted number is a "numeric string" 1.184 + * as defined in the Decimal Arithmetic Specification, available at 1.185 + * http://speleotrove.com/decimal 1.186 + * 1.187 + * @param number The unformatted number, as a string. 1.188 + * @param appendTo Output parameter to receive result. 1.189 + * Result is appended to existing contents. 1.190 + * @param posIter On return, can be used to iterate over positions 1.191 + * of fields generated by this format call. 1.192 + * Can be NULL. 1.193 + * @param status Output param filled with success/failure status. 1.194 + * @return Reference to 'appendTo' parameter. 1.195 + * @internal 1.196 + */ 1.197 + virtual UnicodeString& format(const StringPiece &number, 1.198 + UnicodeString& appendTo, 1.199 + FieldPositionIterator* posIter, 1.200 + UErrorCode& status) const; 1.201 + 1.202 + /** 1.203 + * Format a decimal number. Currently sets status to U_UNSUPPORTED_ERROR 1.204 + * The number is a DigitList wrapper onto a floating point decimal number. 1.205 + * The default implementation in NumberFormat converts the decimal number 1.206 + * to a double and formats that. 1.207 + * 1.208 + * @param number The number, a DigitList format Decimal Floating Point. 1.209 + * @param appendTo Output parameter to receive result. 1.210 + * Result is appended to existing contents. 1.211 + * @param posIter On return, can be used to iterate over positions 1.212 + * of fields generated by this format call. 1.213 + * @param status Output param filled with success/failure status. 1.214 + * @return Reference to 'appendTo' parameter. 1.215 + * @internal 1.216 + */ 1.217 + virtual UnicodeString& format(const DigitList &number, 1.218 + UnicodeString& appendTo, 1.219 + FieldPositionIterator* posIter, 1.220 + UErrorCode& status) const; 1.221 + 1.222 + /** 1.223 + * Format a decimal number. Currently sets status to U_UNSUPPORTED_ERROR. 1.224 + * The number is a DigitList wrapper onto a floating point decimal number. 1.225 + * The default implementation in NumberFormat converts the decimal number 1.226 + * to a double and formats that. 1.227 + * 1.228 + * @param number The number, a DigitList format Decimal Floating Point. 1.229 + * @param appendTo Output parameter to receive result. 1.230 + * Result is appended to existing contents. 1.231 + * @param pos On input: an alignment field, if desired. 1.232 + * On output: the offsets of the alignment field. 1.233 + * @param status Output param filled with success/failure status. 1.234 + * @return Reference to 'appendTo' parameter. 1.235 + * @internal 1.236 + */ 1.237 + virtual UnicodeString& format(const DigitList &number, 1.238 + UnicodeString& appendTo, 1.239 + FieldPosition& pos, 1.240 + UErrorCode& status) const; 1.241 + 1.242 + /** 1.243 + * CompactDecimalFormat does not support parsing. This implementation 1.244 + * does nothing. 1.245 + * @param text Unused. 1.246 + * @param result Does not change. 1.247 + * @param parsePosition Does not change. 1.248 + * @see Formattable 1.249 + * @draft ICU 51 1.250 + */ 1.251 + virtual void parse(const UnicodeString& text, 1.252 + Formattable& result, 1.253 + ParsePosition& parsePosition) const; 1.254 + 1.255 + /** 1.256 + * CompactDecimalFormat does not support parsing. This implementation 1.257 + * sets status to U_UNSUPPORTED_ERROR 1.258 + * 1.259 + * @param text Unused. 1.260 + * @param result Does not change. 1.261 + * @param status Always set to U_UNSUPPORTED_ERROR. 1.262 + * @draft ICU 51 1.263 + */ 1.264 + virtual void parse(const UnicodeString& text, 1.265 + Formattable& result, 1.266 + UErrorCode& status) const; 1.267 + 1.268 +/* Cannot use #ifndef U_HIDE_INTERNAL_API for the following draft method since it is virtual */ 1.269 + /** 1.270 + * Parses text from the given string as a currency amount. Unlike 1.271 + * the parse() method, this method will attempt to parse a generic 1.272 + * currency name, searching for a match of this object's locale's 1.273 + * currency display names, or for a 3-letter ISO currency code. 1.274 + * This method will fail if this format is not a currency format, 1.275 + * that is, if it does not contain the currency pattern symbol 1.276 + * (U+00A4) in its prefix or suffix. This implementation always returns 1.277 + * NULL. 1.278 + * 1.279 + * @param text the string to parse 1.280 + * @param pos input-output position; on input, the position within text 1.281 + * to match; must have 0 <= pos.getIndex() < text.length(); 1.282 + * on output, the position after the last matched character. 1.283 + * If the parse fails, the position in unchanged upon output. 1.284 + * @return if parse succeeds, a pointer to a newly-created CurrencyAmount 1.285 + * object (owned by the caller) containing information about 1.286 + * the parsed currency; if parse fails, this is NULL. 1.287 + * @internal 1.288 + */ 1.289 + virtual CurrencyAmount* parseCurrency(const UnicodeString& text, 1.290 + ParsePosition& pos) const; 1.291 + 1.292 + /** 1.293 + * Return the class ID for this class. This is useful only for 1.294 + * comparing to a return value from getDynamicClassID(). For example: 1.295 + * <pre> 1.296 + * . Base* polymorphic_pointer = createPolymorphicObject(); 1.297 + * . if (polymorphic_pointer->getDynamicClassID() == 1.298 + * . Derived::getStaticClassID()) ... 1.299 + * </pre> 1.300 + * @return The class ID for all objects of this class. 1.301 + * @draft ICU 51 1.302 + */ 1.303 + static UClassID U_EXPORT2 getStaticClassID(); 1.304 + 1.305 + /** 1.306 + * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. 1.307 + * This method is to implement a simple version of RTTI, since not all 1.308 + * C++ compilers support genuine RTTI. Polymorphic operator==() and 1.309 + * clone() methods call this method. 1.310 + * 1.311 + * @return The class ID for this object. All objects of a 1.312 + * given class have the same class ID. Objects of 1.313 + * other classes have different class IDs. 1.314 + * @draft ICU 51 1.315 + */ 1.316 + virtual UClassID getDynamicClassID() const; 1.317 + 1.318 +private: 1.319 + 1.320 + const UHashtable* _unitsByVariant; 1.321 + const double* _divisors; 1.322 + PluralRules* _pluralRules; 1.323 + 1.324 + // Default constructor not implemented. 1.325 + CompactDecimalFormat(const DecimalFormat &, const UHashtable* unitsByVariant, const double* divisors, PluralRules* pluralRules); 1.326 + 1.327 + UBool eqHelper(const CompactDecimalFormat& that) const; 1.328 +}; 1.329 + 1.330 +U_NAMESPACE_END 1.331 + 1.332 +#endif /* U_HIDE_DRAFT_API */ 1.333 +#endif /* #if !UCONFIG_NO_FORMATTING */ 1.334 + 1.335 +#endif // __COMPACT_DECIMAL_FORMAT_H__ 1.336 +//eof