compactdecimalformat.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // © 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. #if U_SHOW_CPLUSPLUS_API
  16. /**
  17. * \file
  18. * \brief C++ API: Compatibility APIs for compact decimal number formatting.
  19. */
  20. #if !UCONFIG_NO_FORMATTING
  21. #include "unicode/decimfmt.h"
  22. struct UHashtable;
  23. U_NAMESPACE_BEGIN
  24. class PluralRules;
  25. /**
  26. * **IMPORTANT:** New users are strongly encouraged to see if
  27. * numberformatter.h fits their use case. Although not deprecated, this header
  28. * is provided for backwards compatibility only.
  29. *
  30. * -----------------------------------------------------------------------------
  31. *
  32. * The CompactDecimalFormat produces abbreviated numbers, suitable for display in
  33. * environments will limited real estate. For example, 'Hits: 1.2B' instead of
  34. * 'Hits: 1,200,000,000'. The format will be appropriate for the given language,
  35. * such as "1,2 Mrd." for German.
  36. *
  37. * For numbers under 1000 trillion (under 10^15, such as 123,456,789,012,345),
  38. * the result will be short for supported languages. However, the result may
  39. * sometimes exceed 7 characters, such as when there are combining marks or thin
  40. * characters. In such cases, the visual width in fonts should still be short.
  41. *
  42. * By default, there are 3 significant digits. After creation, if more than
  43. * three significant digits are set (with setMaximumSignificantDigits), or if a
  44. * fixed number of digits are set (with setMaximumIntegerDigits or
  45. * setMaximumFractionDigits), then result may be wider.
  46. *
  47. * At this time, parsing is not supported, and will produce a U_UNSUPPORTED_ERROR.
  48. * Resetting the pattern prefixes or suffixes is not supported; the method calls
  49. * are ignored.
  50. *
  51. * @stable ICU 51
  52. */
  53. class U_I18N_API CompactDecimalFormat : public DecimalFormat {
  54. public:
  55. /**
  56. * Returns a compact decimal instance for specified locale.
  57. *
  58. * **NOTE:** New users are strongly encouraged to use
  59. * `number::NumberFormatter` instead of NumberFormat.
  60. * @param inLocale the given locale.
  61. * @param style whether to use short or long style.
  62. * @param status error code returned here.
  63. * @stable ICU 51
  64. */
  65. static CompactDecimalFormat* U_EXPORT2 createInstance(
  66. const Locale& inLocale, UNumberCompactStyle style, UErrorCode& status);
  67. /**
  68. * Copy constructor.
  69. *
  70. * @param source the DecimalFormat object to be copied from.
  71. * @stable ICU 51
  72. */
  73. CompactDecimalFormat(const CompactDecimalFormat& source);
  74. /**
  75. * Destructor.
  76. * @stable ICU 51
  77. */
  78. ~CompactDecimalFormat() override;
  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. * Clone this Format object polymorphically. The caller owns the
  88. * result and should delete it when done.
  89. *
  90. * @return a polymorphic copy of this CompactDecimalFormat.
  91. * @stable ICU 51
  92. */
  93. CompactDecimalFormat* clone() const override;
  94. using DecimalFormat::format;
  95. /**
  96. * CompactDecimalFormat does not support parsing. This implementation
  97. * does nothing.
  98. * @param text Unused.
  99. * @param result Does not change.
  100. * @param parsePosition Does not change.
  101. * @see Formattable
  102. * @stable ICU 51
  103. */
  104. void parse(const UnicodeString& text, Formattable& result,
  105. ParsePosition& parsePosition) const override;
  106. /**
  107. * CompactDecimalFormat does not support parsing. This implementation
  108. * sets status to U_UNSUPPORTED_ERROR
  109. *
  110. * @param text Unused.
  111. * @param result Does not change.
  112. * @param status Always set to U_UNSUPPORTED_ERROR.
  113. * @stable ICU 51
  114. */
  115. void parse(const UnicodeString& text, Formattable& result, UErrorCode& status) const override;
  116. #ifndef U_HIDE_INTERNAL_API
  117. /**
  118. * Parses text from the given string as a currency amount. Unlike
  119. * the parse() method, this method will attempt to parse a generic
  120. * currency name, searching for a match of this object's locale's
  121. * currency display names, or for a 3-letter ISO currency code.
  122. * This method will fail if this format is not a currency format,
  123. * that is, if it does not contain the currency pattern symbol
  124. * (U+00A4) in its prefix or suffix. This implementation always returns
  125. * nullptr.
  126. *
  127. * @param text the string to parse
  128. * @param pos input-output position; on input, the position within text
  129. * to match; must have 0 <= pos.getIndex() < text.length();
  130. * on output, the position after the last matched character.
  131. * If the parse fails, the position in unchanged upon output.
  132. * @return if parse succeeds, a pointer to a newly-created CurrencyAmount
  133. * object (owned by the caller) containing information about
  134. * the parsed currency; if parse fails, this is nullptr.
  135. * @internal
  136. */
  137. CurrencyAmount* parseCurrency(const UnicodeString& text, ParsePosition& pos) const override;
  138. #endif /* U_HIDE_INTERNAL_API */
  139. /**
  140. * Return the class ID for this class. This is useful only for
  141. * comparing to a return value from getDynamicClassID(). For example:
  142. * <pre>
  143. * . Base* polymorphic_pointer = createPolymorphicObject();
  144. * . if (polymorphic_pointer->getDynamicClassID() ==
  145. * . Derived::getStaticClassID()) ...
  146. * </pre>
  147. * @return The class ID for all objects of this class.
  148. * @stable ICU 51
  149. */
  150. static UClassID U_EXPORT2 getStaticClassID();
  151. /**
  152. * Returns a unique class ID POLYMORPHICALLY. Pure virtual override.
  153. * This method is to implement a simple version of RTTI, since not all
  154. * C++ compilers support genuine RTTI. Polymorphic operator==() and
  155. * clone() methods call this method.
  156. *
  157. * @return The class ID for this object. All objects of a
  158. * given class have the same class ID. Objects of
  159. * other classes have different class IDs.
  160. * @stable ICU 51
  161. */
  162. UClassID getDynamicClassID() const override;
  163. private:
  164. CompactDecimalFormat(const Locale& inLocale, UNumberCompactStyle style, UErrorCode& status);
  165. };
  166. U_NAMESPACE_END
  167. #endif /* #if !UCONFIG_NO_FORMATTING */
  168. #endif /* U_SHOW_CPLUSPLUS_API */
  169. #endif // __COMPACT_DECIMAL_FORMAT_H__
  170. //eof