Abstract base class for all number formats
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.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:
. 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;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.. 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;To format a number for a different Locale, specify it in the call to createInstance().. nf = NumberFormat::createInstance( Locale::FRENCH, success );You can use a NumberFormat to parse also.. UErrorCode success; . Formattable result(-999); // initialized with error code . nf->parse(myString, result, 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%.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:
For example, you can align numbers in two ways.
- (a) progressively parse through pieces of a string.
- (b) align the decimal point and other areas.
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.
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.
If no object can be parsed, index is unchanged, and NULL is
returned.
This is a pure virtual which concrete subclasses must implement.
virtual UnicodeString& format(const Formattable& obj, UnicodeString& toAppendTo, FieldPosition& pos, UErrorCode& status) const
toAppendTo - Where the text is to be appended.
pos - On input: an alignment field, if desired.
On output: the offsets of the alignment field.
status - Output param filled with success/failure status.
virtual void parseObject(const UnicodeString& source, Formattable& result, ParsePosition& parse_pos) const
result - Formattable to be set to the parse result.
If parse fails, return contents are undefined.
parse_pos - The position to start parsing at. Upon return
this param is set to the position after the
last character successfully parsed. If the
source is not parsed successfully, this param
will remain unchanged.
UnicodeString& format( double number, UnicodeString& output) const
output - Output param with the formatted string.
virtual UnicodeString& format(double number, UnicodeString& toAppendTo, FieldPosition& pos) const
toAppendTo - The string to append the formatted string to.
This is an output parameter.
pos - On input: an alignment field, if desired.
On output: the offsets of the alignment field.
UnicodeString& format(const Formattable& obj, UnicodeString& result, UErrorCode& status) const
virtual void parse(const UnicodeString& text, Formattable& result, ParsePosition& parsePosition) const
result - Formattable to be set to the parse result.
If parse fails, return contents are undefined.
parsePosition - The position to start parsing at on input.
On output, moved to after the last successfully
parse character. On parse failure, does not change.
virtual void parse( const UnicodeString& text, Formattable& result, UErrorCode& status) const
result - Formattable to be set to the parse result.
If parse fails, return contents are undefined.
status - Success or failure output parameter.
bool_t isParseIntegerOnly(void) const
virtual void setParseIntegerOnly(bool_t value)
static NumberFormat* createInstance(UErrorCode&)
static NumberFormat* createInstance(const Locale& inLocale, UErrorCode&)
static NumberFormat* createCurrencyInstance(UErrorCode&)
static NumberFormat* createCurrencyInstance(const Locale& inLocale, UErrorCode&)
static NumberFormat* createPercentInstance(UErrorCode&)
static NumberFormat* createPercentInstance(const Locale& inLocale, UErrorCode&)
static NumberFormat* createScientificInstance(UErrorCode&)
static NumberFormat* createScientificInstance(const Locale& inLocale, UErrorCode&)
static const Locale* getAvailableLocales(int32_t& count)
bool_t isGroupingUsed(void) const
virtual void setGroupingUsed(bool_t newValue)
int32_t getMaximumIntegerDigits(void) const
virtual void setMaximumIntegerDigits(int32_t newValue)
int32_t getMinimumIntegerDigits(void) const
virtual void setMinimumIntegerDigits(int32_t newValue)
int32_t getMaximumFractionDigits(void) const
virtual void setMaximumFractionDigits(int32_t newValue)
int32_t getMinimumFractionDigits(void) const
virtual void setMinimumFractionDigits(int32_t newValue)
static UClassID getStaticClassID(void)
. Base* polymorphic_pointer = createPolymorphicObject();
. if (polymorphic_pointer->getDynamicClassID() ==
. Derived::getStaticClassID()) ...
virtual UClassID getDynamicClassID(void) const
NumberFormat()
NumberFormat(const NumberFormat&)
NumberFormat& operator=(const NumberFormat&)
alphabetic index hierarchy of classes
this page has been generated automatically by doc++
(c)opyright by Malte Zöckler, Roland Wunderling
contact: doc++@zib.de