]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/unicode/scientificnumberformatter.h
ICU-59180.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / scientificnumberformatter.h
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
b331163b
A
3/*
4**********************************************************************
2ca993e8 5* Copyright (c) 2014-2016, International Business Machines
b331163b
A
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
b331163b
A
16
17#include "unicode/unistr.h"
18
19/**
20 * \file
21 * \brief C++ API: Formats in scientific notation.
22 */
23
f3c0d7a5 24#if U_SHOW_CPLUSPLUS_API
b331163b
A
25U_NAMESPACE_BEGIN
26
27class FieldPositionIterator;
28class DecimalFormatStaticSets;
29class DecimalFormatSymbols;
30class DecimalFormat;
31class Formattable;
32
33/**
34 * A formatter that formats numbers in user-friendly scientific notation.
35 *
36 * Sample code:
37 * <pre>
38 * UErrorCode status = U_ZERO_ERROR;
39 * LocalPointer<ScientificNumberFormatter> fmt(
40 * ScientificNumberFormatter::createMarkupInstance(
41 * "en", "<sup>", "</sup>", status));
42 * if (U_FAILURE(status)) {
43 * return;
44 * }
45 * UnicodeString appendTo;
46 * // appendTo = "1.23456x10<sup>-78</sup>"
47 * fmt->format(1.23456e-78, appendTo, status);
48 * </pre>
49 *
2ca993e8 50 * @stable ICU 55
b331163b
A
51 */
52class U_I18N_API ScientificNumberFormatter : public UObject {
53public:
54
55 /**
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.
62 *
2ca993e8 63 * @stable ICU 55
b331163b
A
64 */
65 static ScientificNumberFormatter *createSuperscriptInstance(
66 DecimalFormat *fmtToAdopt, UErrorCode &status);
67
68 /**
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.
74 *
2ca993e8 75 * @stable ICU 55
b331163b
A
76 */
77 static ScientificNumberFormatter *createSuperscriptInstance(
78 const Locale &locale, UErrorCode &status);
79
80
81 /**
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.
90 *
2ca993e8 91 * @stable ICU 55
b331163b
A
92 */
93 static ScientificNumberFormatter *createMarkupInstance(
94 DecimalFormat *fmtToAdopt,
95 const UnicodeString &beginMarkup,
96 const UnicodeString &endMarkup,
97 UErrorCode &status);
98
99 /**
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.
107 *
2ca993e8 108 * @stable ICU 55
b331163b
A
109 */
110 static ScientificNumberFormatter *createMarkupInstance(
111 const Locale &locale,
112 const UnicodeString &beginMarkup,
113 const UnicodeString &endMarkup,
114 UErrorCode &status);
115
116
117 /**
118 * Returns a copy of this object. Caller must free returned copy.
2ca993e8 119 * @stable ICU 55
b331163b
A
120 */
121 ScientificNumberFormatter *clone() const {
122 return new ScientificNumberFormatter(*this);
123 }
124
125 /**
126 * Destructor.
2ca993e8 127 * @stable ICU 55
b331163b
A
128 */
129 virtual ~ScientificNumberFormatter();
130
131 /**
132 * Formats a number into user friendly scientific notation.
133 *
134 * @param number the number to format.
135 * @param appendTo formatted string appended here.
136 * @param status any error returned here.
137 * @return appendTo
138 *
2ca993e8 139 * @stable ICU 55
b331163b
A
140 */
141 UnicodeString &format(
142 const Formattable &number,
143 UnicodeString &appendTo,
144 UErrorCode &status) const;
145 private:
146 class U_I18N_API Style : public UObject {
147 public:
148 virtual Style *clone() const = 0;
149 protected:
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;
157 private:
158 friend class ScientificNumberFormatter;
159 };
160
161 class U_I18N_API SuperscriptStyle : public Style {
162 public:
163 virtual Style *clone() const;
164 protected:
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;
172 };
173
174 class U_I18N_API MarkupStyle : public Style {
175 public:
176 MarkupStyle(
177 const UnicodeString &beginMarkup,
178 const UnicodeString &endMarkup)
179 : Style(),
180 fBeginMarkup(beginMarkup),
181 fEndMarkup(endMarkup) { }
182 virtual Style *clone() const;
183 protected:
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;
191 private:
192 UnicodeString fBeginMarkup;
193 UnicodeString fEndMarkup;
194 };
195
196 ScientificNumberFormatter(
197 DecimalFormat *fmtToAdopt,
198 Style *styleToAdopt,
199 UErrorCode &status);
200
201 ScientificNumberFormatter(const ScientificNumberFormatter &other);
202 ScientificNumberFormatter &operator=(const ScientificNumberFormatter &);
203
204 static void getPreExponent(
205 const DecimalFormatSymbols &dfs, UnicodeString &preExponent);
206
207 static ScientificNumberFormatter *createInstance(
208 DecimalFormat *fmtToAdopt,
209 Style *styleToAdopt,
210 UErrorCode &status);
211
212 UnicodeString fPreExponent;
213 DecimalFormat *fDecimalFormat;
214 Style *fStyle;
215 const DecimalFormatStaticSets *fStaticSets;
216
217};
218
219U_NAMESPACE_END
f3c0d7a5 220#endif // U_SHOW_CPLUSPLUS_API
b331163b 221
b331163b
A
222
223#endif /* !UCONFIG_NO_FORMATTING */
224#endif