Concrete class for formatting decimal numbers, allowing a variety of parameters, and localization to Western, Arabic, or Indic numbers
format()
is padded
format()
is padded
Concrete class for formatting decimal numbers, allowing a variety of parameters, and localization to Western, Arabic, or Indic numbers.Normally, you get the proper NumberFormat for a specific locale (including the default locale) using the NumberFormat factory methods, rather than constructing a DecimalNumberFormat directly.
Either the prefixes or the suffixes must be different for the parse to distinguish positive from negative. Parsing will be unreliable if the digits, thousands or decimal separators are the same, or if any of them occur in the prefixes or suffixes.
[Special cases:]
NaN is formatted as a single character, typically \\uFFFD.
+/-Infinity is formatted as a single character, typically \\u221E, plus the positive and negative pre/suffixes.
Note: this class is designed for common users; for very large or small numbers, use a format that can express exponential values.
[Example:]
. // normally we would have a GUI with a menu for this . int32_t locCount; . const Locale* locales = NumberFormat::getAvailableLocales(locCount); . if (locCount > 12) locCount = 12; //limit output . . double myNumber = -1234.56; . UErrorCode success = U_ZERO_ERROR; . NumberFormat* form; //= NumberFormat::createInstance(success); . . // just for fun, we print out a number with the locale number, currency . // and percent format for each locale we can. . UnicodeString countryName; . UnicodeString displayName; . UnicodeString str; . UnicodeString pattern; . Formattable fmtable; . for (int32_t j = 0; j < 3; ++j) { . cout << endl << "FORMAT " << j << endl; . for (int32_t i = 0; i < locCount; ++i) { . if (locales[i].getCountry(countryName).size() == 0) { . // skip language-only . continue; . } . switch (j) { . default: . form = NumberFormat::createInstance(locales[i], success ); break; . case 1: . form = NumberFormat::createCurrencyInstance(locales[i], success ); break; . case 0: . form = NumberFormat::createPercentInstance(locales[i], success ); break; . } . if (form) { . str.remove(); . pattern = ((DecimalFormat*)form)->toPattern(pattern); . cout << locales[i].getDisplayName(displayName) << ": " << pattern; . cout << " -> " << form->format(myNumber,str) << endl; . form->parse(form->format(myNumber,str), fmtable, success); . //cout << " parsed: " << fmtable << endl; . delete form; . } . } . }[The following shows the structure of the pattern.]. pattern := subpattern{;subpattern} . subpattern := {prefix}integer{.fraction}{suffix} . . prefix := '\\u0000'..'\\uFFFD' - specialCharacters . suffix := '\\u0000'..'\\uFFFD' - specialCharacters . integer := '#'* '0'* '0' . fraction := '0'* '#'* Notation: . X* 0 or more instances of X . (X | Y) either X or Y. . X..Y any character from X up to Y, inclusive. . S - T characters in S, except those in TThe first subpattern is for positive numbers. The second (optional) subpattern is used for negative numbers. (In both cases, ',' can occur inside the integer portion--it is just too messy to indicate in BNF.) For the second subpattern, only the PREFIX and SUFFIX are noted; other attributes are taken only from the first subpattern.Here are the special characters used in the parts of the subpattern, with notes on their usage.
. Symbol Meaning . 0 a digit, showing up a zero if it is zero . # a digit, supressed if zero . . placeholder for decimal separator . , placeholder for grouping separator. . E separates mantissa and exponent for exponential formats. . ; separates formats. . - default negative prefix. . % multiply by 100 and show as percentage . \u2030 multiply by 1000 and show as per mille . \u00A4 currency sign; replaced by currency symbol; if . doubled, replaced by international currency symbol. . If present in a pattern, the monetary decimal separator . is used instead of the decimal separator. . X any other characters can be used in the prefix or suffix . ' used to quote special characters in a prefix or suffix.[Notes]If there is no explicit negative subpattern, - is prefixed to the positive form. That is, "0.00" alone is equivalent to "0.00;-0.00".
Illegal formats, such as "#.#.#" in the same format, will cause a failing UErrorCode to be returned.
The grouping separator is commonly used for thousands, but in some countries for ten-thousands. The interval is a constant number of digits between the grouping characters, such as 100,000,000 or 1,0000,0000. If you supply a pattern with multiple grouping characters, the interval between the last one and the end of the integer is the one that is used. So "#,##,###,####" == "######,####" == "##,####,####".
This class only handles localized digits where the 10 digits are contiguous in Unicode, from 0 to 9. Other digits sets (such as superscripts) would need a different subclass.
To obtain standard formats for a given locale, use the factory methods on NumberFormat such as getNumberInstance. These factories will return the most appropriate sub-class of NumberFormat for a given locale.
To obtain standard formats for a given locale, use the factory methods
on NumberFormat such as getNumberInstance. These factories will
return the most appropriate sub-class of NumberFormat for a given
locale.
To obtain standard formats for a given
locale, use the factory methods on NumberFormat such as
getInstance or getCurrencyInstance. If you need only minor adjustments
to a standard format, you can modify the format returned by
a NumberFormat factory method.
To obtain standard formats for a given
locale, use the factory methods on NumberFormat such as
getInstance or getCurrencyInstance. If you need only minor adjustments
to a standard format, you can modify the format returned by
a NumberFormat factory method.
There is no limit to integer digits are set
by this routine, since that is the typical end-user desire;
use setMaximumInteger if you want to set a real value.
For negative numbers, use a second pattern, separated by a semicolon
There is no limit to integer digits are set
by this routine, since that is the typical end-user desire;
use setMaximumInteger if you want to set a real value.
For negative numbers, use a second pattern, separated by a semicolon
DecimalFormat(const UnicodeString& pattern, UErrorCode& status)
status - Output param set to success/failure code. If the
pattern is invalid this will be set to a failure code. DecimalFormat( const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt, UErrorCode& status)
symbolsToAdopt - the set of symbols to be used. The caller should not
delete this object after making this call.
status - Output param set to success/failure code. If the
pattern is invalid this will be set to a failure code. DecimalFormat( const UnicodeString& pattern, const DecimalFormatSymbols& symbols, UErrorCode& status)
symbols - the set of symbols to be used
status - Output param set to success/failure code. If the
pattern is invalid this will be set to a failure code. DecimalFormat(const DecimalFormat& source)
DecimalFormat& operator=(const DecimalFormat& rhs)
virtual ~DecimalFormat()
virtual Format* clone(void) const
virtual bool_t operator==(const Format& other) const
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
UnicodeString& format(double number, UnicodeString& output) const
UnicodeString& format(int32_t number, UnicodeString& output) 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 const DecimalFormatSymbols* getDecimalFormatSymbols(void) const
virtual void adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt)
virtual void setDecimalFormatSymbols(const DecimalFormatSymbols& symbols)
UnicodeString& getPositivePrefix(UnicodeString& result) const
virtual void setPositivePrefix(const UnicodeString& newValue)
UnicodeString& getNegativePrefix(UnicodeString& result) const
virtual void setNegativePrefix(const UnicodeString& newValue)
UnicodeString& getPositiveSuffix(UnicodeString& result) const
virtual void setPositiveSuffix(const UnicodeString& newValue)
UnicodeString& getNegativeSuffix(UnicodeString& result) const
virtual void setNegativeSuffix(const UnicodeString& newValue)
int32_t getMultiplier(void) const
virtual void setMultiplier(int32_t newValue)
virtual double getRoundingIncrement(void)
getRoundingMode
setRoundingMode virtual void setRoundingIncrement(double newValue)
getRoundingMode
setRoundingMode virtual ERoundingMode getRoundingMode(void)
getRoundingIncrement
setRoundingMode virtual void setRoundingMode(ERoundingMode roundingMode)
getRoundingIncrement
getRoundingMode virtual int32_t getFormatWidth(void)
format()
is padded
getPadCharacter
setPadCharacter
getPadPosition
setPadPosition virtual void setFormatWidth(int32_t width)
format()
is padded.
This method also controls whether padding is enabled.
format()
, or zero to disable padding. A negative
width is equivalent to 0.
getPadCharacter
setPadCharacter
getPadPosition
setPadPosition virtual UChar getPadCharacter(void)
getFormatWidth
setPadCharacter
getPadPosition
setPadPosition virtual void setPadCharacter(UChar padChar)
getFormatWidth
getPadCharacter
getPadPosition
setPadPosition virtual EPadPosition getPadPosition(void)
format()
is shorter than the format width.
kPadBeforePrefix
,
kPadAfterPrefix
, kPadBeforeSuffix
, or
kPadAfterSuffix
.
getFormatWidth
setPadCharacter
getPadCharacter
setPadPosition
kPadBeforePrefix
kPadAfterPrefix
kPadBeforeSuffix
kPadAfterSuffix virtual void setPadPosition(EPadPosition padPos)
format()
is shorter than the format width. This has no effect unless padding is
enabled.
kPadBeforePrefix
,
kPadAfterPrefix
, kPadBeforeSuffix
, or
kPadAfterSuffix
.
getFormatWidth
setPadCharacter
getPadCharacter
getPadPosition
kPadBeforePrefix
kPadAfterPrefix
kPadBeforeSuffix
kPadAfterSuffix virtual bool_t isScientificNotation(void)
getMinimumExponentDigits
setMinimumExponentDigits
isExponentSignAlwaysShown
setExponentSignAlwaysShown virtual void setScientificNotation(bool_t useScientific)
getMinimumExponentDigits
setMinimumExponentDigits
isExponentSignAlwaysShown
setExponentSignAlwaysShown virtual int8_t getMinimumExponentDigits(void)
isScientificNotation
setMinimumExponentDigits
isExponentSignAlwaysShown
setExponentSignAlwaysShown virtual void setMinimumExponentDigits(int8_t minExpDig)
isScientificNotation
getMinimumExponentDigits
isExponentSignAlwaysShown
setExponentSignAlwaysShown virtual bool_t isExponentSignAlwaysShown(void)
isScientificNotation
setMinimumExponentDigits
getMinimumExponentDigits
setExponentSignAlwaysShown virtual void setExponentSignAlwaysShown(bool_t expSignAlways)
isScientificNotation
setMinimumExponentDigits
getMinimumExponentDigits
isExponentSignAlwaysShown int32_t getGroupingSize(void) const
virtual void setGroupingSize(int32_t newValue)
NumberFormat::setGroupingUsed
DecimalFormatSymbols::setGroupingSeparator bool_t isDecimalSeparatorAlwaysShown(void) const
virtual void setDecimalSeparatorAlwaysShown(bool_t newValue)
virtual UnicodeString& toPattern(UnicodeString& result) const
virtual UnicodeString& toLocalizedPattern(UnicodeString& result) const
virtual void applyPattern(const UnicodeString& pattern, UErrorCode& status)
. Example "#,#00.0#" -> 1,234.56
This means a minimum of 2 integer digits, 1 fraction digit, and
a maximum of 2 fraction digits.
. Example: "#,#00.0#;(#,#00.0#)" for negatives in parantheses.
In negative patterns, the minimum and maximum counts are ignored;
these are presumed to be set in the positive pattern.
status - Output param set to success/failure code on
exit. If the pattern is invalid, this will be
set to a failure result. virtual void applyLocalizedPattern(const UnicodeString& pattern, UErrorCode& status)
. Example "#,#00.0#" -> 1,234.56
This means a minimum of 2 integer digits, 1 fraction digit, and
a maximum of 2 fraction digits.
Example: "#,#00.0#;(#,#00.0#)" for negatives in parantheses.
In negative patterns, the minimum and maximum counts are ignored;
these are presumed to be set in the positive pattern.
status - Output param set to success/failure code on
exit. If the pattern is invalid, this will be
set to a failure result. virtual void setMaximumIntegerDigits(int32_t newValue)
virtual void setMinimumIntegerDigits(int32_t newValue)
virtual void setMaximumFractionDigits(int32_t newValue)
virtual void setMinimumFractionDigits(int32_t newValue)
static const UnicodeString fgNumberPatterns
static UClassID getStaticClassID(void)
. Base* polymorphic_pointer = createPolymorphicObject();
. if (polymorphic_pointer->getDynamicClassID() ==
. Derived::getStaticClassID()) ...
virtual UClassID getDynamicClassID(void) const
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