• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 U_NAMESPACE_BEGIN
25 
26 class FieldPositionIterator;
27 class DecimalFormatSymbols;
28 class DecimalFormat;
29 class Formattable;
30 
31 /**
32  * A formatter that formats numbers in user-friendly scientific notation.
33  *
34  * Sample code:
35  * <pre>
36  * UErrorCode status = U_ZERO_ERROR;
37  * LocalPointer<ScientificNumberFormatter> fmt(
38  *         ScientificNumberFormatter::createMarkupInstance(
39  *                 "en", "<sup>", "</sup>", status));
40  * if (U_FAILURE(status)) {
41  *     return;
42  * }
43  * UnicodeString appendTo;
44  * // appendTo = "1.23456x10<sup>-78</sup>"
45  * fmt->format(1.23456e-78, appendTo, status);
46  * </pre>
47  *
48  * @stable ICU 55
49  */
50 class U_I18N_API ScientificNumberFormatter : public UObject {
51 public:
52 
53     /**
54      * Creates a ScientificNumberFormatter instance that uses
55      * superscript characters for exponents.
56      * @param fmtToAdopt The DecimalFormat which must be configured for
57      *   scientific notation.
58      * @param status error returned here.
59      * @return The new ScientificNumberFormatter instance.
60      *
61      * @stable ICU 55
62      */
63     static ScientificNumberFormatter *createSuperscriptInstance(
64             DecimalFormat *fmtToAdopt, UErrorCode &status);
65 
66     /**
67      * Creates a ScientificNumberFormatter instance that uses
68      * superscript characters for exponents for this locale.
69      * @param locale The locale
70      * @param status error returned here.
71      * @return The ScientificNumberFormatter instance.
72      *
73      * @stable ICU 55
74      */
75     static ScientificNumberFormatter *createSuperscriptInstance(
76             const Locale &locale, UErrorCode &status);
77 
78 
79     /**
80      * Creates a ScientificNumberFormatter instance that uses
81      * markup for exponents.
82      * @param fmtToAdopt The DecimalFormat which must be configured for
83      *   scientific notation.
84      * @param beginMarkup the markup to start superscript.
85      * @param endMarkup the markup to end superscript.
86      * @param status error returned here.
87      * @return The new ScientificNumberFormatter instance.
88      *
89      * @stable ICU 55
90      */
91     static ScientificNumberFormatter *createMarkupInstance(
92             DecimalFormat *fmtToAdopt,
93             const UnicodeString &beginMarkup,
94             const UnicodeString &endMarkup,
95             UErrorCode &status);
96 
97     /**
98      * Creates a ScientificNumberFormatter instance that uses
99      * markup for exponents for this locale.
100      * @param locale The locale
101      * @param beginMarkup the markup to start superscript.
102      * @param endMarkup the markup to end superscript.
103      * @param status error returned here.
104      * @return The ScientificNumberFormatter instance.
105      *
106      * @stable ICU 55
107      */
108     static ScientificNumberFormatter *createMarkupInstance(
109             const Locale &locale,
110             const UnicodeString &beginMarkup,
111             const UnicodeString &endMarkup,
112             UErrorCode &status);
113 
114 
115     /**
116      * Returns a copy of this object. Caller must free returned copy.
117      * @stable ICU 55
118      */
clone()119     ScientificNumberFormatter *clone() const {
120         return new ScientificNumberFormatter(*this);
121     }
122 
123     /**
124      * Destructor.
125      * @stable ICU 55
126      */
127     virtual ~ScientificNumberFormatter();
128 
129     /**
130      * Formats a number into user friendly scientific notation.
131      *
132      * @param number the number to format.
133      * @param appendTo formatted string appended here.
134      * @param status any error returned here.
135      * @return appendTo
136      *
137      * @stable ICU 55
138      */
139     UnicodeString &format(
140             const Formattable &number,
141             UnicodeString &appendTo,
142             UErrorCode &status) const;
143  private:
144     class U_I18N_API Style : public UObject {
145     public:
146         virtual Style *clone() const = 0;
147     protected:
148         virtual UnicodeString &format(
149                 const UnicodeString &original,
150                 FieldPositionIterator &fpi,
151                 const UnicodeString &preExponent,
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                 UnicodeString &appendTo,
167                 UErrorCode &status) const;
168     };
169 
170     class U_I18N_API MarkupStyle : public Style {
171     public:
MarkupStyle(const UnicodeString & beginMarkup,const UnicodeString & endMarkup)172         MarkupStyle(
173                 const UnicodeString &beginMarkup,
174                 const UnicodeString &endMarkup)
175                 : Style(),
176                   fBeginMarkup(beginMarkup),
177                   fEndMarkup(endMarkup) { }
178         virtual Style *clone() const;
179     protected:
180         virtual UnicodeString &format(
181                 const UnicodeString &original,
182                 FieldPositionIterator &fpi,
183                 const UnicodeString &preExponent,
184                 UnicodeString &appendTo,
185                 UErrorCode &status) const;
186     private:
187         UnicodeString fBeginMarkup;
188         UnicodeString fEndMarkup;
189     };
190 
191     ScientificNumberFormatter(
192             DecimalFormat *fmtToAdopt,
193             Style *styleToAdopt,
194             UErrorCode &status);
195 
196     ScientificNumberFormatter(const ScientificNumberFormatter &other);
197     ScientificNumberFormatter &operator=(const ScientificNumberFormatter &);
198 
199     static void getPreExponent(
200             const DecimalFormatSymbols &dfs, UnicodeString &preExponent);
201 
202     static ScientificNumberFormatter *createInstance(
203             DecimalFormat *fmtToAdopt,
204             Style *styleToAdopt,
205             UErrorCode &status);
206 
207     UnicodeString fPreExponent;
208     DecimalFormat *fDecimalFormat;
209     Style *fStyle;
210 
211 };
212 
213 U_NAMESPACE_END
214 
215 
216 #endif /* !UCONFIG_NO_FORMATTING */
217 #endif
218