Imported Upstream version 58.1
[platform/upstream/icu.git] / source / i18n / unicode / scientificnumberformatter.h
1 // Copyright (C) 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 U_NAMESPACE_BEGIN
25
26 class FieldPositionIterator;
27 class DecimalFormatStaticSets;
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                 const DecimalFormatStaticSets &decimalFormatSets,
154                 UnicodeString &appendTo,
155                 UErrorCode &status) const = 0;
156     private:
157         friend class ScientificNumberFormatter;
158     };
159
160     class U_I18N_API SuperscriptStyle : public Style {
161     public:
162         virtual Style *clone() const;
163     protected:
164         virtual UnicodeString &format(
165                 const UnicodeString &original,
166                 FieldPositionIterator &fpi,
167                 const UnicodeString &preExponent,
168                 const DecimalFormatStaticSets &decimalFormatSets,
169                 UnicodeString &appendTo,
170                 UErrorCode &status) const;
171     };
172
173     class U_I18N_API MarkupStyle : public Style {
174     public:
175         MarkupStyle(
176                 const UnicodeString &beginMarkup,
177                 const UnicodeString &endMarkup)
178                 : Style(),
179                   fBeginMarkup(beginMarkup),
180                   fEndMarkup(endMarkup) { }
181         virtual Style *clone() const;
182     protected:
183         virtual UnicodeString &format(
184                 const UnicodeString &original,
185                 FieldPositionIterator &fpi,
186                 const UnicodeString &preExponent,
187                 const DecimalFormatStaticSets &decimalFormatSets,
188                 UnicodeString &appendTo,
189                 UErrorCode &status) const;
190     private:
191         UnicodeString fBeginMarkup;
192         UnicodeString fEndMarkup;
193     };
194
195     ScientificNumberFormatter(
196             DecimalFormat *fmtToAdopt,
197             Style *styleToAdopt,
198             UErrorCode &status);
199
200     ScientificNumberFormatter(const ScientificNumberFormatter &other);
201     ScientificNumberFormatter &operator=(const ScientificNumberFormatter &);
202
203     static void getPreExponent(
204             const DecimalFormatSymbols &dfs, UnicodeString &preExponent);
205
206     static ScientificNumberFormatter *createInstance(
207             DecimalFormat *fmtToAdopt,
208             Style *styleToAdopt,
209             UErrorCode &status);
210
211     UnicodeString fPreExponent;
212     DecimalFormat *fDecimalFormat;
213     Style *fStyle;
214     const DecimalFormatStaticSets *fStaticSets;
215
216 };
217
218 U_NAMESPACE_END
219
220
221 #endif /* !UCONFIG_NO_FORMATTING */
222 #endif