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