ICU 70.1  70.1
scientificnumberformatter.h
Go to the documentation of this file.
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 U_SHOW_CPLUSPLUS_API
15 
16 #if !UCONFIG_NO_FORMATTING
17 
18 
19 #include "unicode/unistr.h"
20 
26 U_NAMESPACE_BEGIN
27 
28 class FieldPositionIterator;
29 class DecimalFormatSymbols;
30 class DecimalFormat;
31 class Formattable;
32 
53 public:
54 
66  DecimalFormat *fmtToAdopt, UErrorCode &status);
67 
78  const Locale &locale, UErrorCode &status);
79 
80 
94  DecimalFormat *fmtToAdopt,
95  const UnicodeString &beginMarkup,
96  const UnicodeString &endMarkup,
97  UErrorCode &status);
98 
111  const Locale &locale,
112  const UnicodeString &beginMarkup,
113  const UnicodeString &endMarkup,
114  UErrorCode &status);
115 
116 
122  return new ScientificNumberFormatter(*this);
123  }
124 
130 
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,
153  const UnicodeString &preExponent,
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 SuperscriptStyle *clone() const override;
163  protected:
164  virtual UnicodeString &format(
165  const UnicodeString &original,
167  const UnicodeString &preExponent,
168  UnicodeString &appendTo,
169  UErrorCode &status) const override;
170  };
171 
172  class U_I18N_API MarkupStyle : public Style {
173  public:
174  MarkupStyle(
175  const UnicodeString &beginMarkup,
176  const UnicodeString &endMarkup)
177  : Style(),
178  fBeginMarkup(beginMarkup),
179  fEndMarkup(endMarkup) { }
180  virtual MarkupStyle *clone() const override;
181  protected:
182  virtual UnicodeString &format(
183  const UnicodeString &original,
184  FieldPositionIterator &fpi,
185  const UnicodeString &preExponent,
186  UnicodeString &appendTo,
187  UErrorCode &status) const override;
188  private:
189  UnicodeString fBeginMarkup;
190  UnicodeString fEndMarkup;
191  };
192 
193  ScientificNumberFormatter(
194  DecimalFormat *fmtToAdopt,
195  Style *styleToAdopt,
196  UErrorCode &status);
197 
198  ScientificNumberFormatter(const ScientificNumberFormatter &other);
199  ScientificNumberFormatter &operator=(const ScientificNumberFormatter &);
200 
201  static void getPreExponent(
202  const DecimalFormatSymbols &dfs, UnicodeString &preExponent);
203 
204  static ScientificNumberFormatter *createInstance(
205  DecimalFormat *fmtToAdopt,
206  Style *styleToAdopt,
207  UErrorCode &status);
208 
209  UnicodeString fPreExponent;
210  DecimalFormat *fDecimalFormat;
211  Style *fStyle;
212 
213 };
214 
215 U_NAMESPACE_END
216 
217 
218 #endif /* !UCONFIG_NO_FORMATTING */
219 
220 #endif /* U_SHOW_CPLUSPLUS_API */
221 
222 #endif
IMPORTANT: New users are strongly encouraged to see if numberformatter.h fits their use case.
Definition: decimfmt.h:668
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:58
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:64
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195
A formatter that formats numbers in user-friendly scientific notation.
static ScientificNumberFormatter * createMarkupInstance(DecimalFormat *fmtToAdopt, const UnicodeString &beginMarkup, const UnicodeString &endMarkup, UErrorCode &status)
Creates a ScientificNumberFormatter instance that uses markup for exponents.
ScientificNumberFormatter * clone() const
Returns a copy of this object.
virtual ~ScientificNumberFormatter()
Destructor.
static ScientificNumberFormatter * createSuperscriptInstance(const Locale &locale, UErrorCode &status)
Creates a ScientificNumberFormatter instance that uses superscript characters for exponents for this ...
UnicodeString & format(const Formattable &number, UnicodeString &appendTo, UErrorCode &status) const
Formats a number into user friendly scientific notation.
static ScientificNumberFormatter * createMarkupInstance(const Locale &locale, const UnicodeString &beginMarkup, const UnicodeString &endMarkup, UErrorCode &status)
Creates a ScientificNumberFormatter instance that uses markup for exponents for this locale.
static ScientificNumberFormatter * createSuperscriptInstance(DecimalFormat *fmtToAdopt, UErrorCode &status)
Creates a ScientificNumberFormatter instance that uses superscript characters for exponents.
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:223
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:296
C++ API: Unicode String.
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition: utypes.h:301