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