scientificnumberformatter.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. **********************************************************************
  5. * Copyright (c) 2014-2016, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. **********************************************************************
  8. */
  9. #ifndef SCINUMBERFORMATTER_H
  10. #define SCINUMBERFORMATTER_H
  11. #include "unicode/utypes.h"
  12. #if U_SHOW_CPLUSPLUS_API
  13. #if !UCONFIG_NO_FORMATTING
  14. #include "unicode/unistr.h"
  15. /**
  16. * \file
  17. * \brief C++ API: Formats in scientific notation.
  18. */
  19. U_NAMESPACE_BEGIN
  20. class FieldPositionIterator;
  21. class DecimalFormatSymbols;
  22. class DecimalFormat;
  23. class Formattable;
  24. /**
  25. * A formatter that formats numbers in user-friendly scientific notation.
  26. *
  27. * Sample code:
  28. * <pre>
  29. * UErrorCode status = U_ZERO_ERROR;
  30. * LocalPointer<ScientificNumberFormatter> fmt(
  31. * ScientificNumberFormatter::createMarkupInstance(
  32. * "en", "<sup>", "</sup>", status));
  33. * if (U_FAILURE(status)) {
  34. * return;
  35. * }
  36. * UnicodeString appendTo;
  37. * // appendTo = "1.23456x10<sup>-78</sup>"
  38. * fmt->format(1.23456e-78, appendTo, status);
  39. * </pre>
  40. *
  41. * @stable ICU 55
  42. */
  43. class U_I18N_API ScientificNumberFormatter : public UObject {
  44. public:
  45. /**
  46. * Creates a ScientificNumberFormatter instance that uses
  47. * superscript characters for exponents.
  48. * @param fmtToAdopt The DecimalFormat which must be configured for
  49. * scientific notation.
  50. * @param status error returned here.
  51. * @return The new ScientificNumberFormatter instance.
  52. *
  53. * @stable ICU 55
  54. */
  55. static ScientificNumberFormatter *createSuperscriptInstance(
  56. DecimalFormat *fmtToAdopt, UErrorCode &status);
  57. /**
  58. * Creates a ScientificNumberFormatter instance that uses
  59. * superscript characters for exponents for this locale.
  60. * @param locale The locale
  61. * @param status error returned here.
  62. * @return The ScientificNumberFormatter instance.
  63. *
  64. * @stable ICU 55
  65. */
  66. static ScientificNumberFormatter *createSuperscriptInstance(
  67. const Locale &locale, UErrorCode &status);
  68. /**
  69. * Creates a ScientificNumberFormatter instance that uses
  70. * markup for exponents.
  71. * @param fmtToAdopt The DecimalFormat which must be configured for
  72. * scientific notation.
  73. * @param beginMarkup the markup to start superscript.
  74. * @param endMarkup the markup to end superscript.
  75. * @param status error returned here.
  76. * @return The new ScientificNumberFormatter instance.
  77. *
  78. * @stable ICU 55
  79. */
  80. static ScientificNumberFormatter *createMarkupInstance(
  81. DecimalFormat *fmtToAdopt,
  82. const UnicodeString &beginMarkup,
  83. const UnicodeString &endMarkup,
  84. UErrorCode &status);
  85. /**
  86. * Creates a ScientificNumberFormatter instance that uses
  87. * markup for exponents for this locale.
  88. * @param locale The locale
  89. * @param beginMarkup the markup to start superscript.
  90. * @param endMarkup the markup to end superscript.
  91. * @param status error returned here.
  92. * @return The ScientificNumberFormatter instance.
  93. *
  94. * @stable ICU 55
  95. */
  96. static ScientificNumberFormatter *createMarkupInstance(
  97. const Locale &locale,
  98. const UnicodeString &beginMarkup,
  99. const UnicodeString &endMarkup,
  100. UErrorCode &status);
  101. /**
  102. * Returns a copy of this object. Caller must free returned copy.
  103. * @stable ICU 55
  104. */
  105. ScientificNumberFormatter *clone() const {
  106. return new ScientificNumberFormatter(*this);
  107. }
  108. /**
  109. * Destructor.
  110. * @stable ICU 55
  111. */
  112. virtual ~ScientificNumberFormatter();
  113. /**
  114. * Formats a number into user friendly scientific notation.
  115. *
  116. * @param number the number to format.
  117. * @param appendTo formatted string appended here.
  118. * @param status any error returned here.
  119. * @return appendTo
  120. *
  121. * @stable ICU 55
  122. */
  123. UnicodeString &format(
  124. const Formattable &number,
  125. UnicodeString &appendTo,
  126. UErrorCode &status) const;
  127. private:
  128. class U_I18N_API Style : public UObject {
  129. public:
  130. virtual Style *clone() const = 0;
  131. protected:
  132. virtual UnicodeString &format(
  133. const UnicodeString &original,
  134. FieldPositionIterator &fpi,
  135. const UnicodeString &preExponent,
  136. UnicodeString &appendTo,
  137. UErrorCode &status) const = 0;
  138. private:
  139. friend class ScientificNumberFormatter;
  140. };
  141. class U_I18N_API SuperscriptStyle : public Style {
  142. public:
  143. virtual SuperscriptStyle *clone() const override;
  144. protected:
  145. virtual UnicodeString &format(
  146. const UnicodeString &original,
  147. FieldPositionIterator &fpi,
  148. const UnicodeString &preExponent,
  149. UnicodeString &appendTo,
  150. UErrorCode &status) const override;
  151. };
  152. class U_I18N_API MarkupStyle : public Style {
  153. public:
  154. MarkupStyle(
  155. const UnicodeString &beginMarkup,
  156. const UnicodeString &endMarkup)
  157. : Style(),
  158. fBeginMarkup(beginMarkup),
  159. fEndMarkup(endMarkup) { }
  160. virtual MarkupStyle *clone() const override;
  161. protected:
  162. virtual UnicodeString &format(
  163. const UnicodeString &original,
  164. FieldPositionIterator &fpi,
  165. const UnicodeString &preExponent,
  166. UnicodeString &appendTo,
  167. UErrorCode &status) const override;
  168. private:
  169. UnicodeString fBeginMarkup;
  170. UnicodeString fEndMarkup;
  171. };
  172. ScientificNumberFormatter(
  173. DecimalFormat *fmtToAdopt,
  174. Style *styleToAdopt,
  175. UErrorCode &status);
  176. ScientificNumberFormatter(const ScientificNumberFormatter &other);
  177. ScientificNumberFormatter &operator=(const ScientificNumberFormatter &) = delete;
  178. static void getPreExponent(
  179. const DecimalFormatSymbols &dfs, UnicodeString &preExponent);
  180. static ScientificNumberFormatter *createInstance(
  181. DecimalFormat *fmtToAdopt,
  182. Style *styleToAdopt,
  183. UErrorCode &status);
  184. UnicodeString fPreExponent;
  185. DecimalFormat *fDecimalFormat;
  186. Style *fStyle;
  187. };
  188. U_NAMESPACE_END
  189. #endif /* !UCONFIG_NO_FORMATTING */
  190. #endif /* U_SHOW_CPLUSPLUS_API */
  191. #endif