scientificnumberformatter.h 6.5 KB

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