#include <numfmt.h>
Inheritance diagram for NumberFormat:
Public Types | |
enum | EAlignmentFields { kIntegerField, kFractionField, INTEGER_FIELD = kIntegerField, FRACTION_FIELD = kFractionField } |
Alignment Field constants used to construct a FieldPosition object. More... | |
Public Member Functions | |
virtual | ~NumberFormat () |
Destructor. | |
virtual UBool | operator== (const Format &other) const |
Return true if the given Format objects are semantically equal. | |
virtual UnicodeString & | format (const Formattable &obj, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const |
Format an object to produce a string. | |
virtual void | parseObject (const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const |
Parse a string to produce an object. | |
UnicodeString & | format (double number, UnicodeString &appendTo) const |
Format a double number. | |
UnicodeString & | format (int32_t number, UnicodeString &appendTo) const |
Format a long number. | |
virtual UnicodeString & | format (double number, UnicodeString &appendTo, FieldPosition &pos) const=0 |
Format a double number. | |
virtual UnicodeString & | format (int32_t number, UnicodeString &appendTo, FieldPosition &pos) const=0 |
Format a long number. | |
UnicodeString & | format (const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const |
Redeclared Format method. | |
virtual void | parse (const UnicodeString &text, Formattable &result, ParsePosition &parsePosition) const=0 |
Return a long if possible (e.g. | |
virtual void | parse (const UnicodeString &text, Formattable &result, UErrorCode &status) const |
Parse a string as a numeric value, and return a Formattable numeric object. | |
UBool | isParseIntegerOnly (void) const |
Return true if this format will parse numbers as integers only. | |
virtual void | setParseIntegerOnly (UBool value) |
Sets whether or not numbers should be parsed as integers only. | |
UBool | isGroupingUsed (void) const |
Returns true if grouping is used in this format. | |
virtual void | setGroupingUsed (UBool newValue) |
Set whether or not grouping will be used in this format. | |
int32_t | getMaximumIntegerDigits (void) const |
Returns the maximum number of digits allowed in the integer portion of a number. | |
virtual void | setMaximumIntegerDigits (int32_t newValue) |
Sets the maximum number of digits allowed in the integer portion of a number. | |
int32_t | getMinimumIntegerDigits (void) const |
Returns the minimum number of digits allowed in the integer portion of a number. | |
virtual void | setMinimumIntegerDigits (int32_t newValue) |
Sets the minimum number of digits allowed in the integer portion of a number. | |
int32_t | getMaximumFractionDigits (void) const |
Returns the maximum number of digits allowed in the fraction portion of a number. | |
virtual void | setMaximumFractionDigits (int32_t newValue) |
Sets the maximum number of digits allowed in the fraction portion of a number. | |
int32_t | getMinimumFractionDigits (void) const |
Returns the minimum number of digits allowed in the fraction portion of a number. | |
virtual void | setMinimumFractionDigits (int32_t newValue) |
Sets the minimum number of digits allowed in the fraction portion of a number. | |
virtual void | setCurrency (const UChar *theCurrency) |
Sets the currency used to display currency amounts. | |
const UChar * | getCurrency () const |
Gets the currency used to display currency amounts. | |
virtual UClassID | getDynamicClassID (void) const=0 |
Returns a unique class ID POLYMORPHICALLY. | |
Static Public Member Functions | |
NumberFormat * | createInstance (UErrorCode &) |
Returns the default number format for the current default locale. | |
NumberFormat * | createInstance (const Locale &inLocale, UErrorCode &) |
Returns the default number format for the specified locale. | |
NumberFormat * | createCurrencyInstance (UErrorCode &) |
Returns a currency format for the current default locale. | |
NumberFormat * | createCurrencyInstance (const Locale &inLocale, UErrorCode &) |
Returns a currency format for the specified locale. | |
NumberFormat * | createPercentInstance (UErrorCode &) |
Returns a percentage format for the current default locale. | |
NumberFormat * | createPercentInstance (const Locale &inLocale, UErrorCode &) |
Returns a percentage format for the specified locale. | |
NumberFormat * | createScientificInstance (UErrorCode &) |
Returns a scientific format for the current default locale. | |
NumberFormat * | createScientificInstance (const Locale &inLocale, UErrorCode &) |
Returns a scientific format for the specified locale. | |
const Locale * | getAvailableLocales (int32_t &count) |
Get the set of Locales for which NumberFormats are installed. | |
URegistryKey | registerFactory (NumberFormatFactory *toAdopt, UErrorCode &status) |
Register a new NumberFormatFactory. | |
UBool | unregister (URegistryKey key, UErrorCode &status) |
Unregister a previously-registered NumberFormatFactory using the key returned from the register call. | |
StringEnumeration * | getAvailableLocales (void) |
Return a StringEnumeration over the locales available at the time of the call, including registered locales. | |
UClassID | getStaticClassID (void) |
Return the class ID for this class. | |
Protected Member Functions | |
NumberFormat () | |
Default constructor for subclass use only. | |
NumberFormat (const NumberFormat &) | |
Copy constructor. | |
NumberFormat & | operator= (const NumberFormat &) |
Assignment operator. | |
Friends | |
class | ICUNumberFormatFactory |
class | ICUNumberFormatService |
Provides interface for formatting and parsing a number. Also provides methods for determining which locales have number formats, and what their names are.
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.
To format a number for the current Locale, use one of the static factory methods:
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.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;You can use a NumberFormat to parse also.UnicodeString myString; UErrorCode success = U_ZERO_ERROR; nf = NumberFormat::createInstance( success ); int32_t a[] = { 123, 3333, -1234567 }; const int32_t a_len = sizeof(a) / sizeof(a[0]); myString.remove(); for (int32_t i = 0; i < a_len; i++) { nf->format(a[i], myString); myString += " ; "; } cout << " Example 2: " << myString << endl; \endcide </PRE> To format a number for a different Locale, specify it in the call to createInstance(). <PRE> \code nf = NumberFormat::createInstance( Locale::FRENCH, success );Use createInstance to get the normal number format for that country. There are other static factory methods available. Use getCurrency to get the currency number format for that country. Use getPercent to get a format for displaying percentages. With this format, a fraction from 0.53 is displayed as 53%.UErrorCode success; Formattable result(-999); // initialized with error code nf->parse(myString, result, success);You can also control the display of numbers with such methods as getMinimumFractionDigits. If you want even more control over the format or parsing, or want to give your users more control, you can try casting the NumberFormat you get from the factory methods to a DecimalNumberFormat. This will work for the vast majority of countries; just remember to put it in a try block in case you encounter an unusual one.
You can also use forms of the parse and format methods with ParsePosition and FieldPosition to allow you to:
If you are using a monospaced font with spacing for alignment, you can pass the FieldPosition in your format call, with field = INTEGER_FIELD. On output, 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.
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. ICU 2.0
|
Alignment Field constants used to construct a FieldPosition object. Signifies that the position of the integer part or fraction part of a formatted number should be returned.
|
|
Destructor. ICU 2.0 |
|
Default constructor for subclass use only. ICU 2.0 |
|
Copy constructor. ICU 2.0 |
|
Returns a currency format for the specified locale.
|
|
Returns a currency format for the current default locale. ICU 2.0 |
|
Returns the default number format for the specified locale. The default format is one of the styles provided by the other factory methods: getNumberInstance, getCurrencyInstance or getPercentInstance. Exactly which one is locale dependant.
|
|
Returns the default number format for the current default locale. The default format is one of the styles provided by the other factory methods: getNumberInstance, getCurrencyInstance or getPercentInstance. Exactly which one is locale dependant. ICU 2.0 |
|
Returns a percentage format for the specified locale.
|
|
Returns a percentage format for the current default locale. ICU 2.0 |
|
Returns a scientific format for the specified locale.
|
|
Returns a scientific format for the current default locale. ICU 2.0 |
|
Redeclared Format method.
Reimplemented from Format. Reimplemented in ChoiceFormat, DecimalFormat, and RuleBasedNumberFormat. |
|
Format a long number. Concrete subclasses must implement these pure virtual methods.
Implemented in ChoiceFormat, DecimalFormat, and RuleBasedNumberFormat. |
|
Format a double number. Concrete subclasses must implement these pure virtual methods.
Implemented in ChoiceFormat, DecimalFormat, and RuleBasedNumberFormat. |
|
Format a long number. These methods call the NumberFormat pure virtual format() methods with the default FieldPosition.
Reimplemented in ChoiceFormat, DecimalFormat, and RuleBasedNumberFormat. |
|
Format a double number. These methods call the NumberFormat pure virtual format() methods with the default FieldPosition.
Reimplemented in ChoiceFormat, DecimalFormat, and RuleBasedNumberFormat. |
|
Format an object to produce a string. This method handles Formattable objects with numeric types. If the Formattable object type is not a numeric type, then it returns a failing UErrorCode.
Implements Format. Reimplemented in ChoiceFormat, DecimalFormat, and RuleBasedNumberFormat. |
|
Return a StringEnumeration over the locales available at the time of the call, including registered locales.
|
|
Get the set of Locales for which NumberFormats are installed.
|
|
Gets the currency used to display currency amounts. This may be an empty string for some subclasses.
|
|
Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This method is to implement a simple version of RTTI, since not all C++ compilers support genuine RTTI. Polymorphic operator==() and clone() methods call this method.
Implements Format. Implemented in ChoiceFormat, DecimalFormat, and RuleBasedNumberFormat. |
|
Returns the maximum number of digits allowed in the fraction portion of a number.
|
|
Returns the maximum number of digits allowed in the integer portion of a number.
|
|
Returns the minimum number of digits allowed in the fraction portion of a number.
|
|
Returns the minimum number of digits allowed in the integer portion of a number.
|
|
Return the class ID for this class. This is useful only for comparing to a return value from getDynamicClassID(). For example:
Reimplemented in ChoiceFormat, DecimalFormat, and RuleBasedNumberFormat. |
|
Returns true if grouping is used in this format. For example, in the English locale, with grouping on, the number 1234567 might be formatted as "1,234,567". The grouping separator as well as the size of each group is locale dependant and is determined by sub-classes of NumberFormat.
|
|
Return true if this format will parse numbers as integers only. For example in the English locale, with ParseIntegerOnly true, the string "1234." would be parsed as the integer value 1234 and parsing would stop at the "." character. Of course, the exact format accepted by the parse operation is locale dependant and determined by sub-classes of NumberFormat.
|
|
Assignment operator. ICU 2.0 |
|
Return true if the given Format objects are semantically equal. Objects of different subclasses are considered unequal.
Implements Format. Reimplemented in ChoiceFormat, DecimalFormat, and RuleBasedNumberFormat. |
|
Parse a string as a numeric value, and return a Formattable numeric object. This method parses integers only if IntegerOnly is set.
Reimplemented in ChoiceFormat, DecimalFormat, and RuleBasedNumberFormat. |
|
Return a long if possible (e.g. within range LONG_MAX, LONG_MAX], and with no decimals), otherwise a double. If IntegerOnly is set, will stop at a decimal point (or equivalent; e.g. for rational numbers "1 2/3", will stop after the 1). If no object can be parsed, index is unchanged, and NULL is returned. This is a pure virtual which concrete subclasses must implement.
Implemented in ChoiceFormat, DecimalFormat, and RuleBasedNumberFormat. |
|
Parse a string to produce an object. This methods handles parsing of numeric strings into Formattable objects with numeric types. Before calling, set parse_pos.index to the offset you want to start parsing at in the source. After calling, parse_pos.index is the end of the text you parsed. If error occurs, index is unchanged. When parsing, leading whitespace is discarded (with successful parse), while trailing whitespace is left as is. See Format::parseObject() for more.
Implements Format. |
|
Register a new NumberFormatFactory. The factory will be adopted.
|
|
Sets the currency used to display currency amounts. This takes effect immediately, if this format is a currency format. If this format is not a currency format, then the currency is used if and when this object becomes a currency format.
Reimplemented in DecimalFormat. |
|
Set whether or not grouping will be used in this format.
|
|
Sets the maximum number of digits allowed in the fraction portion of a number. maximumFractionDigits must be >= minimumFractionDigits. If the new value for maximumFractionDigits is less than the current value of minimumFractionDigits, then minimumFractionDigits will also be set to the new value.
Reimplemented in DecimalFormat. |
|
Sets the maximum number of digits allowed in the integer portion of a number. maximumIntegerDigits must be >= minimumIntegerDigits. If the new value for maximumIntegerDigits is less than the current value of minimumIntegerDigits, then minimumIntegerDigits will also be set to the new value.
Reimplemented in DecimalFormat. |
|
Sets the minimum number of digits allowed in the fraction portion of a number. minimumFractionDigits must be <= maximumFractionDigits. If the new value for minimumFractionDigits exceeds the current value of maximumFractionDigits, then maximumIntegerDigits will also be set to the new value
Reimplemented in DecimalFormat. |
|
Sets the minimum number of digits allowed in the integer portion of a number. minimumIntegerDigits must be <= maximumIntegerDigits. If the new value for minimumIntegerDigits exceeds the current value of maximumIntegerDigits, then maximumIntegerDigits will also be set to the new value.
Reimplemented in DecimalFormat. |
|
Sets whether or not numbers should be parsed as integers only.
|
|
Unregister a previously-registered NumberFormatFactory using the key returned from the register call. Key becomes invalid after a successful call and should not be used again. The NumberFormatFactory corresponding to the key will be deleted.
|