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 DecimalFormatSymbols
;
33 * A formatter that formats numbers in user-friendly scientific notation.
37 * UErrorCode status = U_ZERO_ERROR;
38 * LocalPointer<ScientificNumberFormatter> fmt(
39 * ScientificNumberFormatter::createMarkupInstance(
40 * "en", "<sup>", "</sup>", status));
41 * if (U_FAILURE(status)) {
44 * UnicodeString appendTo;
45 * // appendTo = "1.23456x10<sup>-78</sup>"
46 * fmt->format(1.23456e-78, appendTo, status);
51 class U_I18N_API ScientificNumberFormatter
: public UObject
{
55 * Creates a ScientificNumberFormatter instance that uses
56 * superscript characters for exponents.
57 * @param fmtToAdopt The DecimalFormat which must be configured for
58 * scientific notation.
59 * @param status error returned here.
60 * @return The new ScientificNumberFormatter instance.
64 static ScientificNumberFormatter
*createSuperscriptInstance(
65 DecimalFormat
*fmtToAdopt
, UErrorCode
&status
);
68 * Creates a ScientificNumberFormatter instance that uses
69 * superscript characters for exponents for this locale.
70 * @param locale The locale
71 * @param status error returned here.
72 * @return The ScientificNumberFormatter instance.
76 static ScientificNumberFormatter
*createSuperscriptInstance(
77 const Locale
&locale
, UErrorCode
&status
);
81 * Creates a ScientificNumberFormatter instance that uses
82 * markup for exponents.
83 * @param fmtToAdopt The DecimalFormat which must be configured for
84 * scientific notation.
85 * @param beginMarkup the markup to start superscript.
86 * @param endMarkup the markup to end superscript.
87 * @param status error returned here.
88 * @return The new ScientificNumberFormatter instance.
92 static ScientificNumberFormatter
*createMarkupInstance(
93 DecimalFormat
*fmtToAdopt
,
94 const UnicodeString
&beginMarkup
,
95 const UnicodeString
&endMarkup
,
99 * Creates a ScientificNumberFormatter instance that uses
100 * markup for exponents for this locale.
101 * @param locale The locale
102 * @param beginMarkup the markup to start superscript.
103 * @param endMarkup the markup to end superscript.
104 * @param status error returned here.
105 * @return The ScientificNumberFormatter instance.
109 static ScientificNumberFormatter
*createMarkupInstance(
110 const Locale
&locale
,
111 const UnicodeString
&beginMarkup
,
112 const UnicodeString
&endMarkup
,
117 * Returns a copy of this object. Caller must free returned copy.
120 ScientificNumberFormatter
*clone() const {
121 return new ScientificNumberFormatter(*this);
128 virtual ~ScientificNumberFormatter();
131 * Formats a number into user friendly scientific notation.
133 * @param number the number to format.
134 * @param appendTo formatted string appended here.
135 * @param status any error returned here.
140 UnicodeString
&format(
141 const Formattable
&number
,
142 UnicodeString
&appendTo
,
143 UErrorCode
&status
) const;
145 class U_I18N_API Style
: public UObject
{
147 virtual Style
*clone() const = 0;
149 virtual UnicodeString
&format(
150 const UnicodeString
&original
,
151 FieldPositionIterator
&fpi
,
152 const UnicodeString
&preExponent
,
153 UnicodeString
&appendTo
,
154 UErrorCode
&status
) const = 0;
156 friend class ScientificNumberFormatter
;
159 class U_I18N_API SuperscriptStyle
: public Style
{
161 virtual Style
*clone() const;
163 virtual UnicodeString
&format(
164 const UnicodeString
&original
,
165 FieldPositionIterator
&fpi
,
166 const UnicodeString
&preExponent
,
167 UnicodeString
&appendTo
,
168 UErrorCode
&status
) const;
171 class U_I18N_API MarkupStyle
: public Style
{
174 const UnicodeString
&beginMarkup
,
175 const UnicodeString
&endMarkup
)
177 fBeginMarkup(beginMarkup
),
178 fEndMarkup(endMarkup
) { }
179 virtual Style
*clone() const;
181 virtual UnicodeString
&format(
182 const UnicodeString
&original
,
183 FieldPositionIterator
&fpi
,
184 const UnicodeString
&preExponent
,
185 UnicodeString
&appendTo
,
186 UErrorCode
&status
) const;
188 UnicodeString fBeginMarkup
;
189 UnicodeString fEndMarkup
;
192 ScientificNumberFormatter(
193 DecimalFormat
*fmtToAdopt
,
197 ScientificNumberFormatter(const ScientificNumberFormatter
&other
);
198 ScientificNumberFormatter
&operator=(const ScientificNumberFormatter
&);
200 static void getPreExponent(
201 const DecimalFormatSymbols
&dfs
, UnicodeString
&preExponent
);
203 static ScientificNumberFormatter
*createInstance(
204 DecimalFormat
*fmtToAdopt
,
208 UnicodeString fPreExponent
;
209 DecimalFormat
*fDecimalFormat
;
215 #endif // U_SHOW_CPLUSPLUS_API
218 #endif /* !UCONFIG_NO_FORMATTING */