// © 2016 and later: Unicode, Inc. and others. // License & terms of use: http://www.unicode.org/copyright.html /* ******************************************************************************** * Copyright (C) 1997-2016, International Business Machines Corporation and others. * All Rights Reserved. ******************************************************************************** * * File NUMFMT.H * * Modification History: * * Date Name Description * 02/19/97 aliu Converted from java. * 03/18/97 clhuang Updated per C++ implementation. * 04/17/97 aliu Changed DigitCount to int per code review. * 07/20/98 stephen JDK 1.2 sync up. Added scientific support. * Changed naming conventions to match C++ guidelines * Deprecated Java style constants (eg, INTEGER_FIELD) ******************************************************************************** */ #ifndef NUMFMT_H #define NUMFMT_H #include "unicode/utypes.h" #if U_SHOW_CPLUSPLUS_API /** * \file * \brief C++ API: Compatibility APIs for number formatting. */ #if !UCONFIG_NO_FORMATTING #include "unicode/unistr.h" #include "unicode/format.h" #include "unicode/unum.h" // UNumberFormatStyle #include "unicode/locid.h" #include "unicode/stringpiece.h" #include "unicode/curramt.h" #include "unicode/udisplaycontext.h" class NumberFormatTest; U_NAMESPACE_BEGIN class SharedNumberFormat; #if !UCONFIG_NO_SERVICE class NumberFormatFactory; class StringEnumeration; #endif /** * <p><strong>IMPORTANT:</strong> New users are strongly encouraged to see if * numberformatter.h fits their use case. Although not deprecated, this header * is provided for backwards compatibility only. * * Abstract base class for all number formats. Provides interface for * formatting and parsing a number. Also provides methods for * determining which locales have number formats, and what their names * are. * * \headerfile unicode/numfmt.h "unicode/numfmt.h" * <P> * NumberFormat helps you to format and parse numbers for any locale. * Your code can be completely independent of the locale conventions * for decimal points, thousands-separators, or even the particular * decimal digits used, or whether the number format is even decimal. * <P> * To format a number for the current Locale, use one of the static * factory methods: * \code * #include <iostream> * #include "unicode/numfmt.h" * #include "unicode/unistr.h" * #include "unicode/ustream.h" * using namespace std; * * int main() { * double myNumber = 7.0; * UnicodeString myString; * UErrorCode success = U_ZERO_ERROR; * NumberFormat* nf = NumberFormat::createInstance(success); * nf->format(myNumber, myString); * cout << " Example 1: " << myString << endl; * } * \endcode * Note that there are additional factory methods within subclasses of * NumberFormat. * <P> * If you are formatting multiple numbers, it is more efficient to get * the format and use it multiple times so that the system doesn't * have to fetch the information about the local language and country * conventions multiple times. * \code * UnicodeString myString; * UErrorCode success = U_ZERO_ERROR; * NumberFormat *nf = NumberFormat::createInstance( success ); * for (int32_t number: {123, 3333, -1234567}) { * nf->format(number, myString); * myString += "; "; * } * cout << " Example 2: " << myString << endl; * \endcode * To format a number for a different Locale, specify it in the * call to \c createInstance(). * \code * nf = NumberFormat::createInstance(Locale::getFrench(), success); * \endcode * You can use a \c NumberFormat to parse also. * \code * UErrorCode success; * Formattable result(-999); // initialized with error code * nf->parse(myString, result, success); * \endcode * Use \c createInstance() to get the normal number format for a \c Locale. * There are other static factory methods available. Use \c createCurrencyInstance() * to get the currency number format for that country. Use \c createPercentInstance() * to get a format for displaying percentages. With this format, a * fraction from 0.53 is displayed as 53%. * <P> * The type of number formatting can be specified by passing a 'style' parameter to \c createInstance(). * For example, use\n * \c createInstance(locale, UNUM_DECIMAL, errorCode) to get the normal number format,\n * \c createInstance(locale, UNUM_PERCENT, errorCode) to get a format for displaying percentage,\n * \c createInstance(locale, UNUM_SCIENTIFIC, errorCode) to get a format for displaying scientific number,\n * \c createInstance(locale, UNUM_CURRENCY, errorCode) to get the currency number format, * in which the currency is represented by its symbol, for example, "$3.00".\n * \c createInstance(locale, UNUM_CURRENCY_ISO, errorCode) to get the currency number format, * in which the currency is represented by its ISO code, for example "USD3.00".\n * \c createInstance(locale, UNUM_CURRENCY_PLURAL, errorCode) to get the currency number format, * in which the currency is represented by its full name in plural format, * for example, "3.00 US dollars" or "1.00 US dollar". * <P> * You can also control the display of numbers with such methods as * \c getMinimumFractionDigits(). If you want even more control over the * format or parsing, or want to give your users more control, you can * try dynamic_casting the \c NumberFormat you get from the factory methods to a * \c DecimalFormat. This will work for the vast majority of * countries; just remember to test for nullptr in case you * encounter an unusual one. * <P> * You can also use forms of the parse and format methods with * \c ParsePosition and \c FieldPosition to allow you to: * <ul type=round> * <li>(a) progressively parse through pieces of a string. * <li>(b) align the decimal point and other areas. * </ul> * For example, you can align numbers in two ways. * <P> * If you are using a monospaced font with spacing for alignment, you * can pass the \c FieldPosition in your format call, with field = * \c UNUM_INTEGER_FIELD. On output, \c getEndIndex will be set to the offset * between the last character of the integer and the decimal. Add * (desiredSpaceCount - getEndIndex) spaces at the front of the * string. * <P> * If you are using proportional fonts, instead of padding with * spaces, measure the width of the string in pixels from the start to * getEndIndex. Then move the pen by (desiredPixelWidth - * widthToAlignmentPoint) before drawing the text. It also works * where there is no decimal, but possibly additional characters at * the end, e.g. with parentheses in negative numbers: "(12)" for -12. * <p> * <em>User subclasses are not supported.</em> While clients may write * subclasses, such code will not necessarily work and will not be * guaranteed to work stably from release to release. * * @stable ICU 2.0 */ class U_I18N_API NumberFormat : public Format { … }; #if !UCONFIG_NO_SERVICE /** * A NumberFormatFactory is used to register new number formats. The factory * should be able to create any of the predefined formats for each locale it * supports. When registered, the locales it supports extend or override the * locale already supported by ICU. * * @stable ICU 2.6 */ class U_I18N_API NumberFormatFactory : public UObject { … }; /** * A NumberFormatFactory that supports a single locale. It can be visible or invisible. * @stable ICU 2.6 */ class U_I18N_API SimpleNumberFormatFactory : public NumberFormatFactory { … }; #endif /* #if !UCONFIG_NO_SERVICE */ // ------------------------------------- inline UBool NumberFormat::isParseIntegerOnly() const { … } inline UBool NumberFormat::isLenient() const { … } U_NAMESPACE_END #endif /* #if !UCONFIG_NO_FORMATTING */ #endif /* U_SHOW_CPLUSPLUS_API */ #endif // _NUMFMT //eof