1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ********************************************************************************
5 * Copyright (C) 1997-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 ********************************************************************************
11 * Modification History:
13 * Date Name Description
14 * 02/19/97 aliu Converted from java.
15 * 03/18/97 clhuang Updated per C++ implementation.
16 * 03/27/97 helena Updated to pass the simple test after code review.
17 * 08/26/97 aliu Added currency/intl currency symbol support.
18 * 07/22/98 stephen Changed to match C++ style
19 * currencySymbol -> fCurrencySymbol
20 * Constants changed from CAPS to kCaps
21 * 06/24/99 helena Integrated Alan's NF enhancements and Java2 bug fixes
22 * 09/22/00 grhoten Marked deprecation tags with a pointer to replacement
24 ********************************************************************************
30 #include "unicode/utypes.h"
31 #include "unicode/uchar.h"
33 #if !UCONFIG_NO_FORMATTING
35 #include "unicode/uobject.h"
36 #include "unicode/locid.h"
37 #include "unicode/unum.h"
38 #include "unicode/unistr.h"
42 * \brief C++ API: Symbols for formatting numbers.
46 #if U_SHOW_CPLUSPLUS_API
50 * This class represents the set of symbols needed by DecimalFormat
51 * to format numbers. DecimalFormat creates for itself an instance of
52 * DecimalFormatSymbols from its locale data. If you need to change any
53 * of these symbols, you can get the DecimalFormatSymbols object from
54 * your DecimalFormat and modify it.
56 * Here are the special characters used in the parts of the
57 * subpattern, with notes on their usage.
62 * # a digit, zero shows as absent
63 * . placeholder for decimal separator
64 * , placeholder for grouping separator.
65 * ; separates formats.
66 * - default negative prefix.
67 * % divide by 100 and show as percentage
68 * X any other characters can be used in the prefix or suffix
69 * ' used to quote special characters in a prefix or suffix.
74 * If there is no explicit negative subpattern, - is prefixed to the
75 * positive form. That is, "0.00" alone is equivalent to "0.00;-0.00".
77 * The grouping separator is commonly used for thousands, but in some
78 * countries for ten-thousands. The interval is a constant number of
79 * digits between the grouping characters, such as 100,000,000 or 1,0000,0000.
80 * If you supply a pattern with multiple grouping characters, the interval
81 * between the last one and the end of the integer is the one that is
82 * used. So "#,##,###,####" == "######,####" == "##,####,####".
84 * This class only handles localized digits where the 10 digits are
85 * contiguous in Unicode, from 0 to 9. Other digits sets (such as
86 * superscripts) would need a different subclass.
88 class U_I18N_API DecimalFormatSymbols
: public UObject
{
91 * Constants for specifying a number format symbol.
94 enum ENumberFormatSymbol
{
95 /** The decimal separator */
96 kDecimalSeparatorSymbol
,
97 /** The grouping separator */
98 kGroupingSeparatorSymbol
,
99 /** The pattern separator */
100 kPatternSeparatorSymbol
,
101 /** The percent sign */
105 /** Character representing a digit in the pattern */
107 /** The minus sign */
111 /** The currency symbol */
113 /** The international currency symbol */
115 /** The monetary separator */
116 kMonetarySeparatorSymbol
,
117 /** The exponential symbol */
119 /** Per mill symbol - replaces kPermillSymbol */
121 /** Escape padding character */
123 /** Infinity symbol */
127 /** Significant digit symbol
129 kSignificantDigitSymbol
,
130 /** The monetary grouping separator
133 kMonetaryGroupingSeparatorSymbol
,
170 /** Multiplication sign.
173 kExponentMultiplicationSymbol
,
174 /** count symbol constants */
175 kFormatSymbolCount
= kNineDigitSymbol
+ 2
179 * Create a DecimalFormatSymbols object for the given locale.
181 * @param locale The locale to get symbols for.
182 * @param status Input/output parameter, set to success or
183 * failure code upon return.
186 DecimalFormatSymbols(const Locale
& locale
, UErrorCode
& status
);
189 * Create a DecimalFormatSymbols object for the default locale.
190 * This constructor will not fail. If the resource file data is
191 * not available, it will use hard-coded last-resort data and
192 * set status to U_USING_FALLBACK_ERROR.
194 * @param status Input/output parameter, set to success or
195 * failure code upon return.
198 DecimalFormatSymbols(UErrorCode
& status
);
201 * Creates a DecimalFormatSymbols object with last-resort data.
202 * Intended for callers who cache the symbols data and
203 * set all symbols on the resulting object.
205 * The last-resort symbols are similar to those for the root data,
206 * except that the grouping separators are empty,
207 * the NaN symbol is U+FFFD rather than "NaN",
208 * and the CurrencySpacing patterns are empty.
210 * @param status Input/output parameter, set to success or
211 * failure code upon return.
212 * @return last-resort symbols
215 static DecimalFormatSymbols
* createWithLastResortData(UErrorCode
& status
);
221 DecimalFormatSymbols(const DecimalFormatSymbols
&);
224 * Assignment operator.
227 DecimalFormatSymbols
& operator=(const DecimalFormatSymbols
&);
233 virtual ~DecimalFormatSymbols();
236 * Return true if another object is semantically equal to this one.
238 * @param other the object to be compared with.
239 * @return true if another object is semantically equal to this one.
242 UBool
operator==(const DecimalFormatSymbols
& other
) const;
245 * Return true if another object is semantically unequal to this one.
247 * @param other the object to be compared with.
248 * @return true if another object is semantically unequal to this one.
251 UBool
operator!=(const DecimalFormatSymbols
& other
) const { return !operator==(other
); }
254 * Get one of the format symbols by its enum constant.
255 * Each symbol is stored as a string so that graphemes
256 * (characters with modifier letters) can be used.
258 * @param symbol Constant to indicate a number format symbol.
259 * @return the format symbols by the param 'symbol'
262 inline UnicodeString
getSymbol(ENumberFormatSymbol symbol
) const;
265 * Set one of the format symbols by its enum constant.
266 * Each symbol is stored as a string so that graphemes
267 * (characters with modifier letters) can be used.
269 * @param symbol Constant to indicate a number format symbol.
270 * @param value value of the format symbol
271 * @param propogateDigits If false, setting the zero digit will not automatically set 1-9.
272 * The default behavior is to automatically set 1-9 if zero is being set and the value
273 * it is being set to corresponds to a known Unicode zero digit.
276 void setSymbol(ENumberFormatSymbol symbol
, const UnicodeString
&value
, const UBool propogateDigits
);
279 * Returns the locale for which this object was constructed.
282 inline Locale
getLocale() const;
285 * Returns the locale for this object. Two flavors are available:
286 * valid and actual locale.
289 Locale
getLocale(ULocDataLocaleType type
, UErrorCode
& status
) const;
292 * Get pattern string for 'CurrencySpacing' that can be applied to
294 * This API gets the CurrencySpacing data from ResourceBundle. The pattern can
295 * be empty if there is no data from current locale and its parent locales.
297 * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
298 * @param beforeCurrency : true if the pattern is for before currency symbol.
299 * false if the pattern is for after currency symbol.
300 * @param status: Input/output parameter, set to success or
301 * failure code upon return.
302 * @return pattern string for currencyMatch, surroundingMatch or spaceInsert.
303 * Return empty string if there is no data for this locale and its parent
307 const UnicodeString
& getPatternForCurrencySpacing(UCurrencySpacing type
,
308 UBool beforeCurrency
,
309 UErrorCode
& status
) const;
311 * Set pattern string for 'CurrencySpacing' that can be applied to
314 * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
315 * @param beforeCurrency : true if the pattern is for before currency symbol.
316 * false if the pattern is for after currency symbol.
317 * @param pattern : pattern string to override current setting.
320 void setPatternForCurrencySpacing(UCurrencySpacing type
,
321 UBool beforeCurrency
,
322 const UnicodeString
& pattern
);
325 * ICU "poor man's RTTI", returns a UClassID for the actual class.
329 virtual UClassID
getDynamicClassID() const;
332 * ICU "poor man's RTTI", returns a UClassID for this class.
336 static UClassID U_EXPORT2
getStaticClassID();
339 DecimalFormatSymbols();
342 * Initializes the symbols from the LocaleElements resource bundle.
343 * Note: The organization of LocaleElements badly needs to be
346 * @param locale The locale to get symbols for.
347 * @param success Input/output parameter, set to success or
348 * failure code upon return.
349 * @param useLastResortData determine if use last resort data
351 void initialize(const Locale
& locale
, UErrorCode
& success
, UBool useLastResortData
= FALSE
);
354 * Initialize the symbols with default values.
358 void setCurrencyForSymbols();
362 #ifndef U_HIDE_INTERNAL_API
364 * @internal For ICU use only
366 inline UBool
isCustomCurrencySymbol() const {
367 return fIsCustomCurrencySymbol
;
371 * @internal For ICU use only
373 inline UBool
isCustomIntlCurrencySymbol() const {
374 return fIsCustomIntlCurrencySymbol
;
376 #endif /* U_HIDE_INTERNAL_API */
379 * _Internal_ function - more efficient version of getSymbol,
380 * returning a const reference to one of the symbol strings.
381 * The returned reference becomes invalid when the symbol is changed
382 * or when the DecimalFormatSymbols are destroyed.
383 * ### TODO markus 2002oct11: Consider proposing getConstSymbol() to be really public.
384 * Note: moved #ifndef U_HIDE_INTERNAL_API after this, since this is needed for inline in DecimalFormat
386 * @param symbol Constant to indicate a number format symbol.
387 * @return the format symbol by the param 'symbol'
390 inline const UnicodeString
&getConstSymbol(ENumberFormatSymbol symbol
) const;
392 #ifndef U_HIDE_INTERNAL_API
394 * Returns that pattern stored in currecy info. Internal API for use by NumberFormat API.
397 inline const char16_t* getCurrencyPattern(void) const;
398 #endif /* U_HIDE_INTERNAL_API */
402 * Private symbol strings.
403 * They are either loaded from a resource bundle or otherwise owned.
404 * setSymbol() clones the symbol string.
405 * Readonly aliases can only come from a resource bundle, so that we can always
406 * use fastCopyFrom() with them.
408 * If DecimalFormatSymbols becomes subclassable and the status of fSymbols changes
409 * from private to protected,
410 * or when fSymbols can be set any other way that allows them to be readonly aliases
411 * to non-resource bundle strings,
412 * then regular UnicodeString copies must be used instead of fastCopyFrom().
416 UnicodeString fSymbols
[kFormatSymbolCount
];
419 * Non-symbol variable for getConstSymbol(). Always empty.
422 UnicodeString fNoSymbol
;
426 char actualLocale
[ULOC_FULLNAME_CAPACITY
];
427 char validLocale
[ULOC_FULLNAME_CAPACITY
];
428 const char16_t* currPattern
;
430 UnicodeString currencySpcBeforeSym
[UNUM_CURRENCY_SPACING_COUNT
];
431 UnicodeString currencySpcAfterSym
[UNUM_CURRENCY_SPACING_COUNT
];
432 UBool fIsCustomCurrencySymbol
;
433 UBool fIsCustomIntlCurrencySymbol
;
436 // -------------------------------------
439 DecimalFormatSymbols::getSymbol(ENumberFormatSymbol symbol
) const {
440 const UnicodeString
*strPtr
;
441 if(symbol
< kFormatSymbolCount
) {
442 strPtr
= &fSymbols
[symbol
];
449 // See comments above for this function. Not hidden with #ifndef U_HIDE_INTERNAL_API
450 inline const UnicodeString
&
451 DecimalFormatSymbols::getConstSymbol(ENumberFormatSymbol symbol
) const {
452 const UnicodeString
*strPtr
;
453 if(symbol
< kFormatSymbolCount
) {
454 strPtr
= &fSymbols
[symbol
];
461 // -------------------------------------
464 DecimalFormatSymbols::setSymbol(ENumberFormatSymbol symbol
, const UnicodeString
&value
, const UBool propogateDigits
= TRUE
) {
465 if (symbol
== kCurrencySymbol
) {
466 fIsCustomCurrencySymbol
= TRUE
;
468 else if (symbol
== kIntlCurrencySymbol
) {
469 fIsCustomIntlCurrencySymbol
= TRUE
;
471 if(symbol
<kFormatSymbolCount
) {
472 fSymbols
[symbol
]=value
;
475 // If the zero digit is being set to a known zero digit according to Unicode,
476 // then we automatically set the corresponding 1-9 digits
477 if ( propogateDigits
&& symbol
== kZeroDigitSymbol
&& value
.countChar32() == 1 ) {
478 UChar32 sym
= value
.char32At(0);
479 if ( u_charDigitValue(sym
) == 0 ) {
480 for ( int8_t i
= 1 ; i
<= 9 ; i
++ ) {
482 fSymbols
[(int)kOneDigitSymbol
+i
-1] = UnicodeString(sym
);
488 // -------------------------------------
491 DecimalFormatSymbols::getLocale() const {
495 #ifndef U_HIDE_INTERNAL_API
496 inline const char16_t*
497 DecimalFormatSymbols::getCurrencyPattern() const {
500 #endif /* U_HIDE_INTERNAL_API */
503 #endif // U_SHOW_CPLUSPLUS_API
505 #endif /* #if !UCONFIG_NO_FORMATTING */