Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:13:07

0001 // © 2016 and later: Unicode, Inc. and others.
0002 // License & terms of use: http://www.unicode.org/copyright.html
0003 /*
0004 **********************************************************************
0005 * Copyright (c) 2014-2016, International Business Machines
0006 * Corporation and others.  All Rights Reserved.
0007 **********************************************************************
0008 */
0009 #ifndef SCINUMBERFORMATTER_H
0010 #define SCINUMBERFORMATTER_H
0011 
0012 #include "unicode/utypes.h"
0013 
0014 #if U_SHOW_CPLUSPLUS_API
0015 
0016 #if !UCONFIG_NO_FORMATTING
0017 
0018 
0019 #include "unicode/unistr.h"
0020 
0021 /**
0022  * \file 
0023  * \brief C++ API: Formats in scientific notation.
0024  */
0025 
0026 U_NAMESPACE_BEGIN
0027 
0028 class FieldPositionIterator;
0029 class DecimalFormatSymbols;
0030 class DecimalFormat;
0031 class Formattable;
0032 
0033 /**
0034  * A formatter that formats numbers in user-friendly scientific notation.
0035  *
0036  * Sample code:
0037  * <pre>
0038  * UErrorCode status = U_ZERO_ERROR;
0039  * LocalPointer<ScientificNumberFormatter> fmt(
0040  *         ScientificNumberFormatter::createMarkupInstance(
0041  *                 "en", "<sup>", "</sup>", status));
0042  * if (U_FAILURE(status)) {
0043  *     return;
0044  * }
0045  * UnicodeString appendTo;
0046  * // appendTo = "1.23456x10<sup>-78</sup>"
0047  * fmt->format(1.23456e-78, appendTo, status);
0048  * </pre>
0049  *
0050  * @stable ICU 55
0051  */
0052 class U_I18N_API ScientificNumberFormatter : public UObject {
0053 public:
0054 
0055     /**
0056      * Creates a ScientificNumberFormatter instance that uses
0057      * superscript characters for exponents.
0058      * @param fmtToAdopt The DecimalFormat which must be configured for
0059      *   scientific notation.
0060      * @param status error returned here.
0061      * @return The new ScientificNumberFormatter instance.
0062      *
0063      * @stable ICU 55
0064      */
0065     static ScientificNumberFormatter *createSuperscriptInstance(
0066             DecimalFormat *fmtToAdopt, UErrorCode &status);
0067 
0068     /**
0069      * Creates a ScientificNumberFormatter instance that uses
0070      * superscript characters for exponents for this locale.
0071      * @param locale The locale
0072      * @param status error returned here.
0073      * @return The ScientificNumberFormatter instance.
0074      *
0075      * @stable ICU 55
0076      */
0077     static ScientificNumberFormatter *createSuperscriptInstance(
0078             const Locale &locale, UErrorCode &status);
0079 
0080 
0081     /**
0082      * Creates a ScientificNumberFormatter instance that uses
0083      * markup for exponents.
0084      * @param fmtToAdopt The DecimalFormat which must be configured for
0085      *   scientific notation.
0086      * @param beginMarkup the markup to start superscript.
0087      * @param endMarkup the markup to end superscript.
0088      * @param status error returned here.
0089      * @return The new ScientificNumberFormatter instance.
0090      *
0091      * @stable ICU 55
0092      */
0093     static ScientificNumberFormatter *createMarkupInstance(
0094             DecimalFormat *fmtToAdopt,
0095             const UnicodeString &beginMarkup,
0096             const UnicodeString &endMarkup,
0097             UErrorCode &status);
0098 
0099     /**
0100      * Creates a ScientificNumberFormatter instance that uses
0101      * markup for exponents for this locale.
0102      * @param locale The locale
0103      * @param beginMarkup the markup to start superscript.
0104      * @param endMarkup the markup to end superscript.
0105      * @param status error returned here.
0106      * @return The ScientificNumberFormatter instance.
0107      *
0108      * @stable ICU 55
0109      */
0110     static ScientificNumberFormatter *createMarkupInstance(
0111             const Locale &locale,
0112             const UnicodeString &beginMarkup,
0113             const UnicodeString &endMarkup,
0114             UErrorCode &status);
0115 
0116 
0117     /**
0118      * Returns a copy of this object. Caller must free returned copy.
0119      * @stable ICU 55
0120      */
0121     ScientificNumberFormatter *clone() const {
0122         return new ScientificNumberFormatter(*this);
0123     }
0124 
0125     /**
0126      * Destructor.
0127      * @stable ICU 55
0128      */
0129     virtual ~ScientificNumberFormatter();
0130 
0131     /**
0132      * Formats a number into user friendly scientific notation.
0133      *
0134      * @param number the number to format.
0135      * @param appendTo formatted string appended here.
0136      * @param status any error returned here.
0137      * @return appendTo
0138      *
0139      * @stable ICU 55
0140      */
0141     UnicodeString &format(
0142             const Formattable &number,
0143             UnicodeString &appendTo,
0144             UErrorCode &status) const;
0145  private:
0146     class U_I18N_API Style : public UObject {
0147     public:
0148         virtual Style *clone() const = 0;
0149     protected:
0150         virtual UnicodeString &format(
0151                 const UnicodeString &original,
0152                 FieldPositionIterator &fpi,
0153                 const UnicodeString &preExponent,
0154                 UnicodeString &appendTo,
0155                 UErrorCode &status) const = 0;
0156     private:
0157         friend class ScientificNumberFormatter;
0158     };
0159 
0160     class U_I18N_API SuperscriptStyle : public Style {
0161     public:
0162         virtual SuperscriptStyle *clone() const override;
0163     protected:
0164         virtual UnicodeString &format(
0165                 const UnicodeString &original,
0166                 FieldPositionIterator &fpi,
0167                 const UnicodeString &preExponent,
0168                 UnicodeString &appendTo,
0169                 UErrorCode &status) const override;
0170     };
0171 
0172     class U_I18N_API MarkupStyle : public Style {
0173     public:
0174         MarkupStyle(
0175                 const UnicodeString &beginMarkup,
0176                 const UnicodeString &endMarkup)
0177                 : Style(),
0178                   fBeginMarkup(beginMarkup),
0179                   fEndMarkup(endMarkup) { }
0180         virtual MarkupStyle *clone() const override;
0181     protected:
0182         virtual UnicodeString &format(
0183                 const UnicodeString &original,
0184                 FieldPositionIterator &fpi,
0185                 const UnicodeString &preExponent,
0186                 UnicodeString &appendTo,
0187                 UErrorCode &status) const override;
0188     private:
0189         UnicodeString fBeginMarkup;
0190         UnicodeString fEndMarkup;
0191     };
0192 
0193     ScientificNumberFormatter(
0194             DecimalFormat *fmtToAdopt,
0195             Style *styleToAdopt,
0196             UErrorCode &status);
0197 
0198     ScientificNumberFormatter(const ScientificNumberFormatter &other);
0199     ScientificNumberFormatter &operator=(const ScientificNumberFormatter &) = delete;
0200 
0201     static void getPreExponent(
0202             const DecimalFormatSymbols &dfs, UnicodeString &preExponent);
0203 
0204     static ScientificNumberFormatter *createInstance(
0205             DecimalFormat *fmtToAdopt,
0206             Style *styleToAdopt,
0207             UErrorCode &status);
0208 
0209     UnicodeString fPreExponent;
0210     DecimalFormat *fDecimalFormat;
0211     Style *fStyle;
0212 
0213 };
0214 
0215 U_NAMESPACE_END
0216 
0217 
0218 #endif /* !UCONFIG_NO_FORMATTING */
0219 
0220 #endif /* U_SHOW_CPLUSPLUS_API */
0221 
0222 #endif