1 /* 2 ******************************************************************************** 3 * Copyright (C) 2012-2015, International Business Machines 4 * Corporation and others. All Rights Reserved. 5 ******************************************************************************** 6 * 7 * File COMPACTDECIMALFORMAT.H 8 ******************************************************************************** 9 */ 10 11 #ifndef __COMPACT_DECIMAL_FORMAT_H__ 12 #define __COMPACT_DECIMAL_FORMAT_H__ 13 14 #include "unicode/utypes.h" 15 /** 16 * \file 17 * \brief C++ API: Formats decimal numbers in compact form. 18 */ 19 20 #if !UCONFIG_NO_FORMATTING 21 22 #include "unicode/decimfmt.h" 23 24 struct UHashtable; 25 26 U_NAMESPACE_BEGIN 27 28 class PluralRules; 29 30 /** 31 * The CompactDecimalFormat produces abbreviated numbers, suitable for display in 32 * environments will limited real estate. For example, 'Hits: 1.2B' instead of 33 * 'Hits: 1,200,000,000'. The format will be appropriate for the given language, 34 * such as "1,2 Mrd." for German. 35 * <p> 36 * For numbers under 1000 trillion (under 10^15, such as 123,456,789,012,345), 37 * the result will be short for supported languages. However, the result may 38 * sometimes exceed 7 characters, such as when there are combining marks or thin 39 * characters. In such cases, the visual width in fonts should still be short. 40 * <p> 41 * By default, there are 3 significant digits. After creation, if more than 42 * three significant digits are set (with setMaximumSignificantDigits), or if a 43 * fixed number of digits are set (with setMaximumIntegerDigits or 44 * setMaximumFractionDigits), then result may be wider. 45 * <p> 46 * At this time, parsing is not supported, and will produce a U_UNSUPPORTED_ERROR. 47 * Resetting the pattern prefixes or suffixes is not supported; the method calls 48 * are ignored. 49 * <p> 50 * @stable ICU 51 51 */ 52 class U_I18N_API CompactDecimalFormat : public DecimalFormat { 53 public: 54 55 /** 56 * Returns a compact decimal instance for specified locale. 57 * @param inLocale the given locale. 58 * @param style whether to use short or long style. 59 * @param status error code returned here. 60 * @stable ICU 51 61 */ 62 static CompactDecimalFormat* U_EXPORT2 createInstance( 63 const Locale& inLocale, UNumberCompactStyle style, UErrorCode& status); 64 65 /** 66 * Copy constructor. 67 * 68 * @param source the DecimalFormat object to be copied from. 69 * @stable ICU 51 70 */ 71 CompactDecimalFormat(const CompactDecimalFormat& source); 72 73 /** 74 * Destructor. 75 * @stable ICU 51 76 */ 77 virtual ~CompactDecimalFormat(); 78 79 /** 80 * Assignment operator. 81 * 82 * @param rhs the DecimalFormat object to be copied. 83 * @stable ICU 51 84 */ 85 CompactDecimalFormat& operator=(const CompactDecimalFormat& rhs); 86 87 /** 88 * Clone this Format object polymorphically. The caller owns the 89 * result and should delete it when done. 90 * 91 * @return a polymorphic copy of this CompactDecimalFormat. 92 * @stable ICU 51 93 */ 94 virtual Format* clone() const; 95 96 /** 97 * Return TRUE if the given Format objects are semantically equal. 98 * Objects of different subclasses are considered unequal. 99 * 100 * @param other the object to be compared with. 101 * @return TRUE if the given Format objects are semantically equal. 102 * @stable ICU 51 103 */ 104 virtual UBool operator==(const Format& other) const; 105 106 107 using DecimalFormat::format; 108 109 /** 110 * Format a double or long number using base-10 representation. 111 * 112 * @param number The value to be formatted. 113 * @param appendTo Output parameter to receive result. 114 * Result is appended to existing contents. 115 * @param pos On input: an alignment field, if desired. 116 * On output: the offsets of the alignment field. 117 * @return Reference to 'appendTo' parameter. 118 * @stable ICU 51 119 */ 120 virtual UnicodeString& format(double number, 121 UnicodeString& appendTo, 122 FieldPosition& pos) const; 123 124 /** 125 * Format a double or long number using base-10 representation. 126 * 127 * @param number The value to be formatted. 128 * @param appendTo Output parameter to receive result. 129 * Result is appended to existing contents. 130 * @param pos On input: an alignment field, if desired. 131 * On output: the offsets of the alignment field. 132 * @param status 133 * @return Reference to 'appendTo' parameter. 134 * @internal 135 */ 136 virtual UnicodeString& format(double number, 137 UnicodeString& appendTo, 138 FieldPosition& pos, 139 UErrorCode &status) const; 140 141 /** 142 * Format a double or long number using base-10 representation. 143 * Currently sets status to U_UNSUPPORTED_ERROR. 144 * 145 * @param number The value to be formatted. 146 * @param appendTo Output parameter to receive result. 147 * Result is appended to existing contents. 148 * @param posIter On return, can be used to iterate over positions 149 * of fields generated by this format call. 150 * Can be NULL. 151 * @param status Output param filled with success/failure status. 152 * @return Reference to 'appendTo' parameter. 153 * @internal 154 */ 155 virtual UnicodeString& format(double number, 156 UnicodeString& appendTo, 157 FieldPositionIterator* posIter, 158 UErrorCode& status) const; 159 160 /** 161 * Format a long number using base-10 representation. 162 * 163 * @param number The value to be formatted. 164 * @param appendTo Output parameter to receive result. 165 * Result is appended to existing contents. 166 * @param pos On input: an alignment field, if desired. 167 * On output: the offsets of the alignment field. 168 * @return Reference to 'appendTo' parameter. 169 * @draft ICU 56 170 */ 171 virtual UnicodeString& format(int32_t number, 172 UnicodeString& appendTo, 173 FieldPosition& pos) const; 174 175 /** 176 * Format a long number using base-10 representation. 177 * 178 * @param number The value to be formatted. 179 * @param appendTo Output parameter to receive result. 180 * Result is appended to existing contents. 181 * @param pos On input: an alignment field, if desired. 182 * On output: the offsets of the alignment field. 183 * @return Reference to 'appendTo' parameter. 184 * @internal 185 */ 186 virtual UnicodeString& format(int32_t number, 187 UnicodeString& appendTo, 188 FieldPosition& pos, 189 UErrorCode &status) const; 190 191 /** 192 * Format a long number using base-10 representation. 193 * Currently sets status to U_UNSUPPORTED_ERROR 194 * 195 * @param number The value to be formatted. 196 * @param appendTo Output parameter to receive result. 197 * Result is appended to existing contents. 198 * @param posIter On return, can be used to iterate over positions 199 * of fields generated by this format call. 200 * Can be NULL. 201 * @param status Output param filled with success/failure status. 202 * @return Reference to 'appendTo' parameter. 203 * @internal 204 */ 205 virtual UnicodeString& format(int32_t number, 206 UnicodeString& appendTo, 207 FieldPositionIterator* posIter, 208 UErrorCode& status) const; 209 210 /** 211 * Format an int64 number using base-10 representation. 212 * 213 * @param number The value to be formatted. 214 * @param appendTo Output parameter to receive result. 215 * Result is appended to existing contents. 216 * @param pos On input: an alignment field, if desired. 217 * On output: the offsets of the alignment field. 218 * @return Reference to 'appendTo' parameter. 219 * @stable ICU 51 220 */ 221 virtual UnicodeString& format(int64_t number, 222 UnicodeString& appendTo, 223 FieldPosition& pos) const; 224 225 /** 226 * Format an int64 number using base-10 representation. 227 * 228 * @param number The value to be formatted. 229 * @param appendTo Output parameter to receive result. 230 * Result is appended to existing contents. 231 * @param pos On input: an alignment field, if desired. 232 * On output: the offsets of the alignment field. 233 * @return Reference to 'appendTo' parameter. 234 * @internal 235 */ 236 virtual UnicodeString& format(int64_t number, 237 UnicodeString& appendTo, 238 FieldPosition& pos, 239 UErrorCode &status) const; 240 241 /** 242 * Format an int64 number using base-10 representation. 243 * Currently sets status to U_UNSUPPORTED_ERROR 244 * 245 * @param number The value to be formatted. 246 * @param appendTo Output parameter to receive result. 247 * Result is appended to existing contents. 248 * @param posIter On return, can be used to iterate over positions 249 * of fields generated by this format call. 250 * Can be NULL. 251 * @param status Output param filled with success/failure status. 252 * @return Reference to 'appendTo' parameter. 253 * @internal 254 */ 255 virtual UnicodeString& format(int64_t number, 256 UnicodeString& appendTo, 257 FieldPositionIterator* posIter, 258 UErrorCode& status) const; 259 260 /** 261 * Format a decimal number. Currently sets status to U_UNSUPPORTED_ERROR 262 * The syntax of the unformatted number is a "numeric string" 263 * as defined in the Decimal Arithmetic Specification, available at 264 * http://speleotrove.com/decimal 265 * 266 * @param number The unformatted number, as a string. 267 * @param appendTo Output parameter to receive result. 268 * Result is appended to existing contents. 269 * @param posIter On return, can be used to iterate over positions 270 * of fields generated by this format call. 271 * Can be NULL. 272 * @param status Output param filled with success/failure status. 273 * @return Reference to 'appendTo' parameter. 274 * @internal 275 */ 276 virtual UnicodeString& format(const StringPiece &number, 277 UnicodeString& appendTo, 278 FieldPositionIterator* posIter, 279 UErrorCode& status) const; 280 281 /** 282 * Format a decimal number. Currently sets status to U_UNSUPPORTED_ERROR 283 * The number is a DigitList wrapper onto a floating point decimal number. 284 * The default implementation in NumberFormat converts the decimal number 285 * to a double and formats that. 286 * 287 * @param number The number, a DigitList format Decimal Floating Point. 288 * @param appendTo Output parameter to receive result. 289 * Result is appended to existing contents. 290 * @param posIter On return, can be used to iterate over positions 291 * of fields generated by this format call. 292 * @param status Output param filled with success/failure status. 293 * @return Reference to 'appendTo' parameter. 294 * @internal 295 */ 296 virtual UnicodeString& format(const DigitList &number, 297 UnicodeString& appendTo, 298 FieldPositionIterator* posIter, 299 UErrorCode& status) const; 300 301 /** 302 * Format a decimal number. Currently sets status to U_UNSUPPORTED_ERROR. 303 * The number is a DigitList wrapper onto a floating point decimal number. 304 * The default implementation in NumberFormat converts the decimal number 305 * to a double and formats that. 306 * 307 * @param number The number, a DigitList format Decimal Floating Point. 308 * @param appendTo Output parameter to receive result. 309 * Result is appended to existing contents. 310 * @param pos On input: an alignment field, if desired. 311 * On output: the offsets of the alignment field. 312 * @param status Output param filled with success/failure status. 313 * @return Reference to 'appendTo' parameter. 314 * @internal 315 */ 316 virtual UnicodeString& format(const DigitList &number, 317 UnicodeString& appendTo, 318 FieldPosition& pos, 319 UErrorCode& status) const; 320 321 /** 322 * CompactDecimalFormat does not support parsing. This implementation 323 * does nothing. 324 * @param text Unused. 325 * @param result Does not change. 326 * @param parsePosition Does not change. 327 * @see Formattable 328 * @stable ICU 51 329 */ 330 virtual void parse(const UnicodeString& text, 331 Formattable& result, 332 ParsePosition& parsePosition) const; 333 334 /** 335 * CompactDecimalFormat does not support parsing. This implementation 336 * sets status to U_UNSUPPORTED_ERROR 337 * 338 * @param text Unused. 339 * @param result Does not change. 340 * @param status Always set to U_UNSUPPORTED_ERROR. 341 * @stable ICU 51 342 */ 343 virtual void parse(const UnicodeString& text, 344 Formattable& result, 345 UErrorCode& status) const; 346 347 /** 348 * Parses text from the given string as a currency amount. Unlike 349 * the parse() method, this method will attempt to parse a generic 350 * currency name, searching for a match of this object's locale's 351 * currency display names, or for a 3-letter ISO currency code. 352 * This method will fail if this format is not a currency format, 353 * that is, if it does not contain the currency pattern symbol 354 * (U+00A4) in its prefix or suffix. This implementation always returns 355 * NULL. 356 * 357 * @param text the string to parse 358 * @param pos input-output position; on input, the position within text 359 * to match; must have 0 <= pos.getIndex() < text.length(); 360 * on output, the position after the last matched character. 361 * If the parse fails, the position in unchanged upon output. 362 * @return if parse succeeds, a pointer to a newly-created CurrencyAmount 363 * object (owned by the caller) containing information about 364 * the parsed currency; if parse fails, this is NULL. 365 * @internal 366 */ 367 virtual CurrencyAmount* parseCurrency(const UnicodeString& text, 368 ParsePosition& pos) const; 369 370 /** 371 * Return the class ID for this class. This is useful only for 372 * comparing to a return value from getDynamicClassID(). For example: 373 * <pre> 374 * . Base* polymorphic_pointer = createPolymorphicObject(); 375 * . if (polymorphic_pointer->getDynamicClassID() == 376 * . Derived::getStaticClassID()) ... 377 * </pre> 378 * @return The class ID for all objects of this class. 379 * @stable ICU 51 380 */ 381 static UClassID U_EXPORT2 getStaticClassID(); 382 383 /** 384 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. 385 * This method is to implement a simple version of RTTI, since not all 386 * C++ compilers support genuine RTTI. Polymorphic operator==() and 387 * clone() methods call this method. 388 * 389 * @return The class ID for this object. All objects of a 390 * given class have the same class ID. Objects of 391 * other classes have different class IDs. 392 * @stable ICU 51 393 */ 394 virtual UClassID getDynamicClassID() const; 395 396 private: 397 398 const UHashtable* _unitsByVariant; 399 const double* _divisors; 400 PluralRules* _pluralRules; 401 402 // Default constructor not implemented. 403 CompactDecimalFormat(const DecimalFormat &, const UHashtable* unitsByVariant, const double* divisors, PluralRules* pluralRules); 404 405 UBool eqHelper(const CompactDecimalFormat& that) const; 406 }; 407 408 U_NAMESPACE_END 409 410 #endif /* #if !UCONFIG_NO_FORMATTING */ 411 412 #endif // __COMPACT_DECIMAL_FORMAT_H__ 413 //eof 414