compactdecimalformat.h 13 KB

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