jpayne@69: // © 2016 and later: Unicode, Inc. and others. jpayne@69: // License & terms of use: http://www.unicode.org/copyright.html jpayne@69: /* jpayne@69: ********************************************************************** jpayne@69: * Copyright (c) 2014-2016, International Business Machines jpayne@69: * Corporation and others. All Rights Reserved. jpayne@69: ********************************************************************** jpayne@69: */ jpayne@69: #ifndef SCINUMBERFORMATTER_H jpayne@69: #define SCINUMBERFORMATTER_H jpayne@69: jpayne@69: #include "unicode/utypes.h" jpayne@69: jpayne@69: #if U_SHOW_CPLUSPLUS_API jpayne@69: jpayne@69: #if !UCONFIG_NO_FORMATTING jpayne@69: jpayne@69: jpayne@69: #include "unicode/unistr.h" jpayne@69: jpayne@69: /** jpayne@69: * \file jpayne@69: * \brief C++ API: Formats in scientific notation. jpayne@69: */ jpayne@69: jpayne@69: U_NAMESPACE_BEGIN jpayne@69: jpayne@69: class FieldPositionIterator; jpayne@69: class DecimalFormatSymbols; jpayne@69: class DecimalFormat; jpayne@69: class Formattable; jpayne@69: jpayne@69: /** jpayne@69: * A formatter that formats numbers in user-friendly scientific notation. jpayne@69: * jpayne@69: * Sample code: jpayne@69: *
jpayne@69: * UErrorCode status = U_ZERO_ERROR; jpayne@69: * LocalPointerjpayne@69: * jpayne@69: * @stable ICU 55 jpayne@69: */ jpayne@69: class U_I18N_API ScientificNumberFormatter : public UObject { jpayne@69: public: jpayne@69: jpayne@69: /** jpayne@69: * Creates a ScientificNumberFormatter instance that uses jpayne@69: * superscript characters for exponents. jpayne@69: * @param fmtToAdopt The DecimalFormat which must be configured for jpayne@69: * scientific notation. jpayne@69: * @param status error returned here. jpayne@69: * @return The new ScientificNumberFormatter instance. jpayne@69: * jpayne@69: * @stable ICU 55 jpayne@69: */ jpayne@69: static ScientificNumberFormatter *createSuperscriptInstance( jpayne@69: DecimalFormat *fmtToAdopt, UErrorCode &status); jpayne@69: jpayne@69: /** jpayne@69: * Creates a ScientificNumberFormatter instance that uses jpayne@69: * superscript characters for exponents for this locale. jpayne@69: * @param locale The locale jpayne@69: * @param status error returned here. jpayne@69: * @return The ScientificNumberFormatter instance. jpayne@69: * jpayne@69: * @stable ICU 55 jpayne@69: */ jpayne@69: static ScientificNumberFormatter *createSuperscriptInstance( jpayne@69: const Locale &locale, UErrorCode &status); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Creates a ScientificNumberFormatter instance that uses jpayne@69: * markup for exponents. jpayne@69: * @param fmtToAdopt The DecimalFormat which must be configured for jpayne@69: * scientific notation. jpayne@69: * @param beginMarkup the markup to start superscript. jpayne@69: * @param endMarkup the markup to end superscript. jpayne@69: * @param status error returned here. jpayne@69: * @return The new ScientificNumberFormatter instance. jpayne@69: * jpayne@69: * @stable ICU 55 jpayne@69: */ jpayne@69: static ScientificNumberFormatter *createMarkupInstance( jpayne@69: DecimalFormat *fmtToAdopt, jpayne@69: const UnicodeString &beginMarkup, jpayne@69: const UnicodeString &endMarkup, jpayne@69: UErrorCode &status); jpayne@69: jpayne@69: /** jpayne@69: * Creates a ScientificNumberFormatter instance that uses jpayne@69: * markup for exponents for this locale. jpayne@69: * @param locale The locale jpayne@69: * @param beginMarkup the markup to start superscript. jpayne@69: * @param endMarkup the markup to end superscript. jpayne@69: * @param status error returned here. jpayne@69: * @return The ScientificNumberFormatter instance. jpayne@69: * jpayne@69: * @stable ICU 55 jpayne@69: */ jpayne@69: static ScientificNumberFormatter *createMarkupInstance( jpayne@69: const Locale &locale, jpayne@69: const UnicodeString &beginMarkup, jpayne@69: const UnicodeString &endMarkup, jpayne@69: UErrorCode &status); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Returns a copy of this object. Caller must free returned copy. jpayne@69: * @stable ICU 55 jpayne@69: */ jpayne@69: ScientificNumberFormatter *clone() const { jpayne@69: return new ScientificNumberFormatter(*this); jpayne@69: } jpayne@69: jpayne@69: /** jpayne@69: * Destructor. jpayne@69: * @stable ICU 55 jpayne@69: */ jpayne@69: virtual ~ScientificNumberFormatter(); jpayne@69: jpayne@69: /** jpayne@69: * Formats a number into user friendly scientific notation. jpayne@69: * jpayne@69: * @param number the number to format. jpayne@69: * @param appendTo formatted string appended here. jpayne@69: * @param status any error returned here. jpayne@69: * @return appendTo jpayne@69: * jpayne@69: * @stable ICU 55 jpayne@69: */ jpayne@69: UnicodeString &format( jpayne@69: const Formattable &number, jpayne@69: UnicodeString &appendTo, jpayne@69: UErrorCode &status) const; jpayne@69: private: jpayne@69: class U_I18N_API Style : public UObject { jpayne@69: public: jpayne@69: virtual Style *clone() const = 0; jpayne@69: protected: jpayne@69: virtual UnicodeString &format( jpayne@69: const UnicodeString &original, jpayne@69: FieldPositionIterator &fpi, jpayne@69: const UnicodeString &preExponent, jpayne@69: UnicodeString &appendTo, jpayne@69: UErrorCode &status) const = 0; jpayne@69: private: jpayne@69: friend class ScientificNumberFormatter; jpayne@69: }; jpayne@69: jpayne@69: class U_I18N_API SuperscriptStyle : public Style { jpayne@69: public: jpayne@69: virtual SuperscriptStyle *clone() const; jpayne@69: protected: jpayne@69: virtual UnicodeString &format( jpayne@69: const UnicodeString &original, jpayne@69: FieldPositionIterator &fpi, jpayne@69: const UnicodeString &preExponent, jpayne@69: UnicodeString &appendTo, jpayne@69: UErrorCode &status) const; jpayne@69: }; jpayne@69: jpayne@69: class U_I18N_API MarkupStyle : public Style { jpayne@69: public: jpayne@69: MarkupStyle( jpayne@69: const UnicodeString &beginMarkup, jpayne@69: const UnicodeString &endMarkup) jpayne@69: : Style(), jpayne@69: fBeginMarkup(beginMarkup), jpayne@69: fEndMarkup(endMarkup) { } jpayne@69: virtual MarkupStyle *clone() const; jpayne@69: protected: jpayne@69: virtual UnicodeString &format( jpayne@69: const UnicodeString &original, jpayne@69: FieldPositionIterator &fpi, jpayne@69: const UnicodeString &preExponent, jpayne@69: UnicodeString &appendTo, jpayne@69: UErrorCode &status) const; jpayne@69: private: jpayne@69: UnicodeString fBeginMarkup; jpayne@69: UnicodeString fEndMarkup; jpayne@69: }; jpayne@69: jpayne@69: ScientificNumberFormatter( jpayne@69: DecimalFormat *fmtToAdopt, jpayne@69: Style *styleToAdopt, jpayne@69: UErrorCode &status); jpayne@69: jpayne@69: ScientificNumberFormatter(const ScientificNumberFormatter &other); jpayne@69: ScientificNumberFormatter &operator=(const ScientificNumberFormatter &); jpayne@69: jpayne@69: static void getPreExponent( jpayne@69: const DecimalFormatSymbols &dfs, UnicodeString &preExponent); jpayne@69: jpayne@69: static ScientificNumberFormatter *createInstance( jpayne@69: DecimalFormat *fmtToAdopt, jpayne@69: Style *styleToAdopt, jpayne@69: UErrorCode &status); jpayne@69: jpayne@69: UnicodeString fPreExponent; jpayne@69: DecimalFormat *fDecimalFormat; jpayne@69: Style *fStyle; jpayne@69: jpayne@69: }; jpayne@69: jpayne@69: U_NAMESPACE_END jpayne@69: jpayne@69: jpayne@69: #endif /* !UCONFIG_NO_FORMATTING */ jpayne@69: jpayne@69: #endif /* U_SHOW_CPLUSPLUS_API */ jpayne@69: jpayne@69: #endiffmt( jpayne@69: * ScientificNumberFormatter::createMarkupInstance( jpayne@69: * "en", "", "", status)); jpayne@69: * if (U_FAILURE(status)) { jpayne@69: * return; jpayne@69: * } jpayne@69: * UnicodeString appendTo; jpayne@69: * // appendTo = "1.23456x10-78" jpayne@69: * fmt->format(1.23456e-78, appendTo, status); jpayne@69: *