scientificnumberformatter.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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) 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 !UCONFIG_NO_FORMATTING
  13. #include "unicode/unistr.h"
  14. /**
  15. * \file
  16. * \brief C++ API: Formats in scientific notation.
  17. */
  18. U_NAMESPACE_BEGIN
  19. class FieldPositionIterator;
  20. class DecimalFormatStaticSets;
  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. const DecimalFormatStaticSets &decimalFormatSets,
  137. UnicodeString &appendTo,
  138. UErrorCode &status) const = 0;
  139. private:
  140. friend class ScientificNumberFormatter;
  141. };
  142. class U_I18N_API SuperscriptStyle : public Style {
  143. public:
  144. virtual Style *clone() const;
  145. protected:
  146. virtual UnicodeString &format(
  147. const UnicodeString &original,
  148. FieldPositionIterator &fpi,
  149. const UnicodeString &preExponent,
  150. const DecimalFormatStaticSets &decimalFormatSets,
  151. UnicodeString &appendTo,
  152. UErrorCode &status) const;
  153. };
  154. class U_I18N_API MarkupStyle : public Style {
  155. public:
  156. MarkupStyle(
  157. const UnicodeString &beginMarkup,
  158. const UnicodeString &endMarkup)
  159. : Style(),
  160. fBeginMarkup(beginMarkup),
  161. fEndMarkup(endMarkup) { }
  162. virtual Style *clone() const;
  163. protected:
  164. virtual UnicodeString &format(
  165. const UnicodeString &original,
  166. FieldPositionIterator &fpi,
  167. const UnicodeString &preExponent,
  168. const DecimalFormatStaticSets &decimalFormatSets,
  169. UnicodeString &appendTo,
  170. UErrorCode &status) const;
  171. private:
  172. UnicodeString fBeginMarkup;
  173. UnicodeString fEndMarkup;
  174. };
  175. ScientificNumberFormatter(
  176. DecimalFormat *fmtToAdopt,
  177. Style *styleToAdopt,
  178. UErrorCode &status);
  179. ScientificNumberFormatter(const ScientificNumberFormatter &other);
  180. ScientificNumberFormatter &operator=(const ScientificNumberFormatter &);
  181. static void getPreExponent(
  182. const DecimalFormatSymbols &dfs, UnicodeString &preExponent);
  183. static ScientificNumberFormatter *createInstance(
  184. DecimalFormat *fmtToAdopt,
  185. Style *styleToAdopt,
  186. UErrorCode &status);
  187. UnicodeString fPreExponent;
  188. DecimalFormat *fDecimalFormat;
  189. Style *fStyle;
  190. const DecimalFormatStaticSets *fStaticSets;
  191. };
  192. U_NAMESPACE_END
  193. #endif /* !UCONFIG_NO_FORMATTING */
  194. #endif