compactdecimalformat.h 17 KB

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