2 ********************************************************************************
3 * Copyright (C) 1997-2013, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ********************************************************************************
9 * Modification History:
11 * Date Name Description
12 * 02/19/97 aliu Converted from java.
13 * 03/18/97 clhuang Updated per C++ implementation.
14 * 03/27/97 helena Updated to pass the simple test after code review.
15 * 08/26/97 aliu Added currency/intl currency symbol support.
16 * 07/22/98 stephen Changed to match C++ style
17 * currencySymbol -> fCurrencySymbol
18 * Constants changed from CAPS to kCaps
19 * 06/24/99 helena Integrated Alan's NF enhancements and Java2 bug fixes
20 * 09/22/00 grhoten Marked deprecation tags with a pointer to replacement
22 ********************************************************************************
28 #include "unicode/utypes.h"
29 #include "unicode/uchar.h"
31 #if !UCONFIG_NO_FORMATTING
33 #include "unicode/uobject.h"
34 #include "unicode/locid.h"
35 #include "unicode/unum.h"
39 * \brief C++ API: Symbols for formatting numbers.
46 * This class represents the set of symbols needed by DecimalFormat
47 * to format numbers. DecimalFormat creates for itself an instance of
48 * DecimalFormatSymbols from its locale data. If you need to change any
49 * of these symbols, you can get the DecimalFormatSymbols object from
50 * your DecimalFormat and modify it.
52 * Here are the special characters used in the parts of the
53 * subpattern, with notes on their usage.
58 * # a digit, zero shows as absent
59 * . placeholder for decimal separator
60 * , placeholder for grouping separator.
61 * ; separates formats.
62 * - default negative prefix.
63 * % divide by 100 and show as percentage
64 * X any other characters can be used in the prefix or suffix
65 * ' used to quote special characters in a prefix or suffix.
70 * If there is no explicit negative subpattern, - is prefixed to the
71 * positive form. That is, "0.00" alone is equivalent to "0.00;-0.00".
73 * The grouping separator is commonly used for thousands, but in some
74 * countries for ten-thousands. The interval is a constant number of
75 * digits between the grouping characters, such as 100,000,000 or 1,0000,0000.
76 * If you supply a pattern with multiple grouping characters, the interval
77 * between the last one and the end of the integer is the one that is
78 * used. So "#,##,###,####" == "######,####" == "##,####,####".
80 * This class only handles localized digits where the 10 digits are
81 * contiguous in Unicode, from 0 to 9. Other digits sets (such as
82 * superscripts) would need a different subclass.
84 class U_I18N_API DecimalFormatSymbols
: public UObject
{
87 * Constants for specifying a number format symbol.
90 enum ENumberFormatSymbol
{
91 /** The decimal separator */
92 kDecimalSeparatorSymbol
,
93 /** The grouping separator */
94 kGroupingSeparatorSymbol
,
95 /** The pattern separator */
96 kPatternSeparatorSymbol
,
97 /** The percent sign */
101 /** Character representing a digit in the pattern */
103 /** The minus sign */
107 /** The currency symbol */
109 /** The international currency symbol */
111 /** The monetary separator */
112 kMonetarySeparatorSymbol
,
113 /** The exponential symbol */
115 /** Per mill symbol - replaces kPermillSymbol */
117 /** Escape padding character */
119 /** Infinity symbol */
123 /** Significant digit symbol
125 kSignificantDigitSymbol
,
126 /** The monetary grouping separator
129 kMonetaryGroupingSeparatorSymbol
,
166 /** count symbol constants */
171 * Create a DecimalFormatSymbols object for the given locale.
173 * @param locale The locale to get symbols for.
174 * @param status Input/output parameter, set to success or
175 * failure code upon return.
178 DecimalFormatSymbols(const Locale
& locale
, UErrorCode
& status
);
181 * Create a DecimalFormatSymbols object for the default locale.
182 * This constructor will not fail. If the resource file data is
183 * not available, it will use hard-coded last-resort data and
184 * set status to U_USING_FALLBACK_ERROR.
186 * @param status Input/output parameter, set to success or
187 * failure code upon return.
190 DecimalFormatSymbols( UErrorCode
& status
);
196 DecimalFormatSymbols(const DecimalFormatSymbols
&);
199 * Assignment operator.
202 DecimalFormatSymbols
& operator=(const DecimalFormatSymbols
&);
208 virtual ~DecimalFormatSymbols();
211 * Return true if another object is semantically equal to this one.
213 * @param other the object to be compared with.
214 * @return true if another object is semantically equal to this one.
217 UBool
operator==(const DecimalFormatSymbols
& other
) const;
220 * Return true if another object is semantically unequal to this one.
222 * @param other the object to be compared with.
223 * @return true if another object is semantically unequal to this one.
226 UBool
operator!=(const DecimalFormatSymbols
& other
) const { return !operator==(other
); }
229 * Get one of the format symbols by its enum constant.
230 * Each symbol is stored as a string so that graphemes
231 * (characters with modifier letters) can be used.
233 * @param symbol Constant to indicate a number format symbol.
234 * @return the format symbols by the param 'symbol'
237 inline UnicodeString
getSymbol(ENumberFormatSymbol symbol
) const;
240 * Set one of the format symbols by its enum constant.
241 * Each symbol is stored as a string so that graphemes
242 * (characters with modifier letters) can be used.
244 * @param symbol Constant to indicate a number format symbol.
245 * @param value value of the format symbol
246 * @param propogateDigits If false, setting the zero digit will not automatically set 1-9.
247 * The default behavior is to automatically set 1-9 if zero is being set and the value
248 * it is being set to corresponds to a known Unicode zero digit.
251 void setSymbol(ENumberFormatSymbol symbol
, const UnicodeString
&value
, const UBool propogateDigits
);
254 * Returns the locale for which this object was constructed.
257 inline Locale
getLocale() const;
260 * Returns the locale for this object. Two flavors are available:
261 * valid and actual locale.
264 Locale
getLocale(ULocDataLocaleType type
, UErrorCode
& status
) const;
267 * Get pattern string for 'CurrencySpacing' that can be applied to
269 * This API gets the CurrencySpacing data from ResourceBundle. The pattern can
270 * be empty if there is no data from current locale and its parent locales.
272 * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
273 * @param beforeCurrency : true if the pattern is for before currency symbol.
274 * false if the pattern is for after currency symbol.
275 * @param status: Input/output parameter, set to success or
276 * failure code upon return.
277 * @return pattern string for currencyMatch, surroundingMatch or spaceInsert.
278 * Return empty string if there is no data for this locale and its parent
282 const UnicodeString
& getPatternForCurrencySpacing(UCurrencySpacing type
,
283 UBool beforeCurrency
,
284 UErrorCode
& status
) const;
286 * Set pattern string for 'CurrencySpacing' that can be applied to
289 * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
290 * @param beforeCurrency : true if the pattern is for before currency symbol.
291 * false if the pattern is for after currency symbol.
292 * @param pattern : pattern string to override current setting.
295 void setPatternForCurrencySpacing(UCurrencySpacing type
,
296 UBool beforeCurrency
,
297 const UnicodeString
& pattern
);
300 * ICU "poor man's RTTI", returns a UClassID for the actual class.
304 virtual UClassID
getDynamicClassID() const;
307 * ICU "poor man's RTTI", returns a UClassID for this class.
311 static UClassID U_EXPORT2
getStaticClassID();
314 DecimalFormatSymbols(); // default constructor not implemented
317 * Initializes the symbols from the LocaleElements resource bundle.
318 * Note: The organization of LocaleElements badly needs to be
321 * @param locale The locale to get symbols for.
322 * @param success Input/output parameter, set to success or
323 * failure code upon return.
324 * @param useLastResortData determine if use last resort data
326 void initialize(const Locale
& locale
, UErrorCode
& success
, UBool useLastResortData
= FALSE
);
329 * Initialize the symbols with default values.
333 void setCurrencyForSymbols();
336 #ifndef U_HIDE_INTERNAL_API
338 * _Internal_ function - more efficient version of getSymbol,
339 * returning a const reference to one of the symbol strings.
340 * The returned reference becomes invalid when the symbol is changed
341 * or when the DecimalFormatSymbols are destroyed.
342 * ### TODO markus 2002oct11: Consider proposing getConstSymbol() to be really public.
344 * @param symbol Constant to indicate a number format symbol.
345 * @return the format symbol by the param 'symbol'
348 inline const UnicodeString
&getConstSymbol(ENumberFormatSymbol symbol
) const;
351 * Returns that pattern stored in currecy info. Internal API for use by NumberFormat API.
354 inline const UChar
* getCurrencyPattern(void) const;
355 #endif /* U_HIDE_INTERNAL_API */
359 * Private symbol strings.
360 * They are either loaded from a resource bundle or otherwise owned.
361 * setSymbol() clones the symbol string.
362 * Readonly aliases can only come from a resource bundle, so that we can always
363 * use fastCopyFrom() with them.
365 * If DecimalFormatSymbols becomes subclassable and the status of fSymbols changes
366 * from private to protected,
367 * or when fSymbols can be set any other way that allows them to be readonly aliases
368 * to non-resource bundle strings,
369 * then regular UnicodeString copies must be used instead of fastCopyFrom().
373 UnicodeString fSymbols
[kFormatSymbolCount
];
376 * Non-symbol variable for getConstSymbol(). Always empty.
379 UnicodeString fNoSymbol
;
383 char actualLocale
[ULOC_FULLNAME_CAPACITY
];
384 char validLocale
[ULOC_FULLNAME_CAPACITY
];
385 const UChar
* currPattern
;
387 UnicodeString currencySpcBeforeSym
[UNUM_CURRENCY_SPACING_COUNT
];
388 UnicodeString currencySpcAfterSym
[UNUM_CURRENCY_SPACING_COUNT
];
391 // -------------------------------------
394 DecimalFormatSymbols::getSymbol(ENumberFormatSymbol symbol
) const {
395 const UnicodeString
*strPtr
;
396 if(symbol
< kFormatSymbolCount
) {
397 strPtr
= &fSymbols
[symbol
];
404 inline const UnicodeString
&
405 DecimalFormatSymbols::getConstSymbol(ENumberFormatSymbol symbol
) const {
406 const UnicodeString
*strPtr
;
407 if(symbol
< kFormatSymbolCount
) {
408 strPtr
= &fSymbols
[symbol
];
416 // -------------------------------------
419 DecimalFormatSymbols::setSymbol(ENumberFormatSymbol symbol
, const UnicodeString
&value
, const UBool propogateDigits
= TRUE
) {
420 if(symbol
<kFormatSymbolCount
) {
421 fSymbols
[symbol
]=value
;
424 // If the zero digit is being set to a known zero digit according to Unicode,
425 // then we automatically set the corresponding 1-9 digits
426 if ( propogateDigits
&& symbol
== kZeroDigitSymbol
&& value
.countChar32() == 1 ) {
427 UChar32 sym
= value
.char32At(0);
428 if ( u_charDigitValue(sym
) == 0 ) {
429 for ( int8_t i
= 1 ; i
<= 9 ; i
++ ) {
431 fSymbols
[(int)kOneDigitSymbol
+i
-1] = UnicodeString(sym
);
437 // -------------------------------------
440 DecimalFormatSymbols::getLocale() const {
445 DecimalFormatSymbols::getCurrencyPattern() const {
451 #endif /* #if !UCONFIG_NO_FORMATTING */