]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/unicode/scientificnumberformatter.h
ICU-64260.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / scientificnumberformatter.h
1 // © 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
12 #include "unicode/utypes.h"
13
14 #if !UCONFIG_NO_FORMATTING
15
16
17 #include "unicode/unistr.h"
18
19 /**
20 * \file
21 * \brief C++ API: Formats in scientific notation.
22 */
23
24 #if U_SHOW_CPLUSPLUS_API
25 U_NAMESPACE_BEGIN
26
27 class FieldPositionIterator;
28 class DecimalFormatSymbols;
29 class DecimalFormat;
30 class Formattable;
31
32 /**
33 * A formatter that formats numbers in user-friendly scientific notation.
34 *
35 * Sample code:
36 * <pre>
37 * UErrorCode status = U_ZERO_ERROR;
38 * LocalPointer<ScientificNumberFormatter> fmt(
39 * ScientificNumberFormatter::createMarkupInstance(
40 * "en", "<sup>", "</sup>", status));
41 * if (U_FAILURE(status)) {
42 * return;
43 * }
44 * UnicodeString appendTo;
45 * // appendTo = "1.23456x10<sup>-78</sup>"
46 * fmt->format(1.23456e-78, appendTo, status);
47 * </pre>
48 *
49 * @stable ICU 55
50 */
51 class U_I18N_API ScientificNumberFormatter : public UObject {
52 public:
53
54 /**
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.
61 *
62 * @stable ICU 55
63 */
64 static ScientificNumberFormatter *createSuperscriptInstance(
65 DecimalFormat *fmtToAdopt, UErrorCode &status);
66
67 /**
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.
73 *
74 * @stable ICU 55
75 */
76 static ScientificNumberFormatter *createSuperscriptInstance(
77 const Locale &locale, UErrorCode &status);
78
79
80 /**
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.
89 *
90 * @stable ICU 55
91 */
92 static ScientificNumberFormatter *createMarkupInstance(
93 DecimalFormat *fmtToAdopt,
94 const UnicodeString &beginMarkup,
95 const UnicodeString &endMarkup,
96 UErrorCode &status);
97
98 /**
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.
106 *
107 * @stable ICU 55
108 */
109 static ScientificNumberFormatter *createMarkupInstance(
110 const Locale &locale,
111 const UnicodeString &beginMarkup,
112 const UnicodeString &endMarkup,
113 UErrorCode &status);
114
115
116 /**
117 * Returns a copy of this object. Caller must free returned copy.
118 * @stable ICU 55
119 */
120 ScientificNumberFormatter *clone() const {
121 return new ScientificNumberFormatter(*this);
122 }
123
124 /**
125 * Destructor.
126 * @stable ICU 55
127 */
128 virtual ~ScientificNumberFormatter();
129
130 /**
131 * Formats a number into user friendly scientific notation.
132 *
133 * @param number the number to format.
134 * @param appendTo formatted string appended here.
135 * @param status any error returned here.
136 * @return appendTo
137 *
138 * @stable ICU 55
139 */
140 UnicodeString &format(
141 const Formattable &number,
142 UnicodeString &appendTo,
143 UErrorCode &status) const;
144 private:
145 class U_I18N_API Style : public UObject {
146 public:
147 virtual Style *clone() const = 0;
148 protected:
149 virtual UnicodeString &format(
150 const UnicodeString &original,
151 FieldPositionIterator &fpi,
152 const UnicodeString &preExponent,
153 UnicodeString &appendTo,
154 UErrorCode &status) const = 0;
155 private:
156 friend class ScientificNumberFormatter;
157 };
158
159 class U_I18N_API SuperscriptStyle : public Style {
160 public:
161 virtual Style *clone() const;
162 protected:
163 virtual UnicodeString &format(
164 const UnicodeString &original,
165 FieldPositionIterator &fpi,
166 const UnicodeString &preExponent,
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 UnicodeString &appendTo,
186 UErrorCode &status) const;
187 private:
188 UnicodeString fBeginMarkup;
189 UnicodeString fEndMarkup;
190 };
191
192 ScientificNumberFormatter(
193 DecimalFormat *fmtToAdopt,
194 Style *styleToAdopt,
195 UErrorCode &status);
196
197 ScientificNumberFormatter(const ScientificNumberFormatter &other);
198 ScientificNumberFormatter &operator=(const ScientificNumberFormatter &);
199
200 static void getPreExponent(
201 const DecimalFormatSymbols &dfs, UnicodeString &preExponent);
202
203 static ScientificNumberFormatter *createInstance(
204 DecimalFormat *fmtToAdopt,
205 Style *styleToAdopt,
206 UErrorCode &status);
207
208 UnicodeString fPreExponent;
209 DecimalFormat *fDecimalFormat;
210 Style *fStyle;
211
212 };
213
214 U_NAMESPACE_END
215 #endif // U_SHOW_CPLUSPLUS_API
216
217
218 #endif /* !UCONFIG_NO_FORMATTING */
219 #endif