1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (c) 2014-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
9 #ifndef SCINUMBERFORMATTER_H
10 #define SCINUMBERFORMATTER_H
12 #include "unicode/utypes.h"
14 #if !UCONFIG_NO_FORMATTING
17 #include "unicode/unistr.h"
21 * \brief C++ API: Formats in scientific notation.
24 #if U_SHOW_CPLUSPLUS_API
27 class FieldPositionIterator
;
28 class DecimalFormatStaticSets
;
29 class DecimalFormatSymbols
;
34 * A formatter that formats numbers in user-friendly scientific notation.
38 * UErrorCode status = U_ZERO_ERROR;
39 * LocalPointer<ScientificNumberFormatter> fmt(
40 * ScientificNumberFormatter::createMarkupInstance(
41 * "en", "<sup>", "</sup>", status));
42 * if (U_FAILURE(status)) {
45 * UnicodeString appendTo;
46 * // appendTo = "1.23456x10<sup>-78</sup>"
47 * fmt->format(1.23456e-78, appendTo, status);
52 class U_I18N_API ScientificNumberFormatter
: public UObject
{
56 * Creates a ScientificNumberFormatter instance that uses
57 * superscript characters for exponents.
58 * @param fmtToAdopt The DecimalFormat which must be configured for
59 * scientific notation.
60 * @param status error returned here.
61 * @return The new ScientificNumberFormatter instance.
65 static ScientificNumberFormatter
*createSuperscriptInstance(
66 DecimalFormat
*fmtToAdopt
, UErrorCode
&status
);
69 * Creates a ScientificNumberFormatter instance that uses
70 * superscript characters for exponents for this locale.
71 * @param locale The locale
72 * @param status error returned here.
73 * @return The ScientificNumberFormatter instance.
77 static ScientificNumberFormatter
*createSuperscriptInstance(
78 const Locale
&locale
, UErrorCode
&status
);
82 * Creates a ScientificNumberFormatter instance that uses
83 * markup for exponents.
84 * @param fmtToAdopt The DecimalFormat which must be configured for
85 * scientific notation.
86 * @param beginMarkup the markup to start superscript.
87 * @param endMarkup the markup to end superscript.
88 * @param status error returned here.
89 * @return The new ScientificNumberFormatter instance.
93 static ScientificNumberFormatter
*createMarkupInstance(
94 DecimalFormat
*fmtToAdopt
,
95 const UnicodeString
&beginMarkup
,
96 const UnicodeString
&endMarkup
,
100 * Creates a ScientificNumberFormatter instance that uses
101 * markup for exponents for this locale.
102 * @param locale The locale
103 * @param beginMarkup the markup to start superscript.
104 * @param endMarkup the markup to end superscript.
105 * @param status error returned here.
106 * @return The ScientificNumberFormatter instance.
110 static ScientificNumberFormatter
*createMarkupInstance(
111 const Locale
&locale
,
112 const UnicodeString
&beginMarkup
,
113 const UnicodeString
&endMarkup
,
118 * Returns a copy of this object. Caller must free returned copy.
121 ScientificNumberFormatter
*clone() const {
122 return new ScientificNumberFormatter(*this);
129 virtual ~ScientificNumberFormatter();
132 * Formats a number into user friendly scientific notation.
134 * @param number the number to format.
135 * @param appendTo formatted string appended here.
136 * @param status any error returned here.
141 UnicodeString
&format(
142 const Formattable
&number
,
143 UnicodeString
&appendTo
,
144 UErrorCode
&status
) const;
146 class U_I18N_API Style
: public UObject
{
148 virtual Style
*clone() const = 0;
150 virtual UnicodeString
&format(
151 const UnicodeString
&original
,
152 FieldPositionIterator
&fpi
,
153 const UnicodeString
&preExponent
,
154 const DecimalFormatStaticSets
&decimalFormatSets
,
155 UnicodeString
&appendTo
,
156 UErrorCode
&status
) const = 0;
158 friend class ScientificNumberFormatter
;
161 class U_I18N_API SuperscriptStyle
: public Style
{
163 virtual Style
*clone() const;
165 virtual UnicodeString
&format(
166 const UnicodeString
&original
,
167 FieldPositionIterator
&fpi
,
168 const UnicodeString
&preExponent
,
169 const DecimalFormatStaticSets
&decimalFormatSets
,
170 UnicodeString
&appendTo
,
171 UErrorCode
&status
) const;
174 class U_I18N_API MarkupStyle
: public Style
{
177 const UnicodeString
&beginMarkup
,
178 const UnicodeString
&endMarkup
)
180 fBeginMarkup(beginMarkup
),
181 fEndMarkup(endMarkup
) { }
182 virtual Style
*clone() const;
184 virtual UnicodeString
&format(
185 const UnicodeString
&original
,
186 FieldPositionIterator
&fpi
,
187 const UnicodeString
&preExponent
,
188 const DecimalFormatStaticSets
&decimalFormatSets
,
189 UnicodeString
&appendTo
,
190 UErrorCode
&status
) const;
192 UnicodeString fBeginMarkup
;
193 UnicodeString fEndMarkup
;
196 ScientificNumberFormatter(
197 DecimalFormat
*fmtToAdopt
,
201 ScientificNumberFormatter(const ScientificNumberFormatter
&other
);
202 ScientificNumberFormatter
&operator=(const ScientificNumberFormatter
&);
204 static void getPreExponent(
205 const DecimalFormatSymbols
&dfs
, UnicodeString
&preExponent
);
207 static ScientificNumberFormatter
*createInstance(
208 DecimalFormat
*fmtToAdopt
,
212 UnicodeString fPreExponent
;
213 DecimalFormat
*fDecimalFormat
;
215 const DecimalFormatStaticSets
*fStaticSets
;
220 #endif // U_SHOW_CPLUSPLUS_API
223 #endif /* !UCONFIG_NO_FORMATTING */