2
********************************************************************************
3
* Copyright (C) 1997-2010, 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"
38
* \brief C++ API: Symbols for formatting numbers.
45
* This class represents the set of symbols needed by DecimalFormat
46
* to format numbers. DecimalFormat creates for itself an instance of
47
* DecimalFormatSymbols from its locale data. If you need to change any
48
* of these symbols, you can get the DecimalFormatSymbols object from
49
* your DecimalFormat and modify it.
51
* Here are the special characters used in the parts of the
52
* subpattern, with notes on their usage.
57
* # a digit, zero shows as absent
58
* . placeholder for decimal separator
59
* , placeholder for grouping separator.
60
* ; separates formats.
61
* - default negative prefix.
62
* % divide by 100 and show as percentage
63
* X any other characters can be used in the prefix or suffix
64
* ' used to quote special characters in a prefix or suffix.
69
* If there is no explicit negative subpattern, - is prefixed to the
70
* positive form. That is, "0.00" alone is equivalent to "0.00;-0.00".
72
* The grouping separator is commonly used for thousands, but in some
73
* countries for ten-thousands. The interval is a constant number of
74
* digits between the grouping characters, such as 100,000,000 or 1,0000,0000.
75
* If you supply a pattern with multiple grouping characters, the interval
76
* between the last one and the end of the integer is the one that is
77
* used. So "#,##,###,####" == "######,####" == "##,####,####".
79
* This class only handles localized digits where the 10 digits are
80
* contiguous in Unicode, from 0 to 9. Other digits sets (such as
81
* superscripts) would need a different subclass.
83
class U_I18N_API DecimalFormatSymbols : public UObject {
86
* Constants for specifying a number format symbol.
89
enum ENumberFormatSymbol {
90
/** The decimal separator */
91
kDecimalSeparatorSymbol,
92
/** The grouping separator */
93
kGroupingSeparatorSymbol,
94
/** The pattern separator */
95
kPatternSeparatorSymbol,
96
/** The percent sign */
100
/** Character representing a digit in the pattern */
102
/** The minus sign */
106
/** The currency symbol */
108
/** The international currency symbol */
110
/** The monetary separator */
111
kMonetarySeparatorSymbol,
112
/** The exponential symbol */
114
/** Per mill symbol - replaces kPermillSymbol */
116
/** Escape padding character */
118
/** Infinity symbol */
122
/** Significant digit symbol
124
kSignificantDigitSymbol,
125
/** The monetary grouping separator
128
kMonetaryGroupingSeparatorSymbol,
165
/** count symbol constants */
170
* Constants for specifying currency spacing
173
enum ECurrencySpacing {
177
kCurrencySpacingCount
181
* Create a DecimalFormatSymbols object for the given locale.
183
* @param locale The locale to get symbols for.
184
* @param status Input/output parameter, set to success or
185
* failure code upon return.
188
DecimalFormatSymbols(const Locale& locale, UErrorCode& status);
191
* Create a DecimalFormatSymbols object for the default locale.
192
* This constructor will not fail. If the resource file data is
193
* not available, it will use hard-coded last-resort data and
194
* set status to U_USING_FALLBACK_ERROR.
196
* @param status Input/output parameter, set to success or
197
* failure code upon return.
200
DecimalFormatSymbols( UErrorCode& status);
206
DecimalFormatSymbols(const DecimalFormatSymbols&);
209
* Assignment operator.
212
DecimalFormatSymbols& operator=(const DecimalFormatSymbols&);
218
virtual ~DecimalFormatSymbols();
221
* Return true if another object is semantically equal to this one.
223
* @param other the object to be compared with.
224
* @return true if another object is semantically equal to this one.
227
UBool operator==(const DecimalFormatSymbols& other) const;
230
* Return true if another object is semantically unequal to this one.
232
* @param other the object to be compared with.
233
* @return true if another object is semantically unequal to this one.
236
UBool operator!=(const DecimalFormatSymbols& other) const { return !operator==(other); }
239
* Get one of the format symbols by its enum constant.
240
* Each symbol is stored as a string so that graphemes
241
* (characters with modifier letters) can be used.
243
* @param symbol Constant to indicate a number format symbol.
244
* @return the format symbols by the param 'symbol'
247
inline UnicodeString getSymbol(ENumberFormatSymbol symbol) const;
250
* Set one of the format symbols by its enum constant.
251
* Each symbol is stored as a string so that graphemes
252
* (characters with modifier letters) can be used.
254
* @param symbol Constant to indicate a number format symbol.
255
* @param value value of the format symbol
256
* @param propogateDigits If false, setting the zero digit will not automatically set 1-9.
257
* The default behavior is to automatically set 1-9 if zero is being set and the value
258
* it is being set to corresponds to a known Unicode zero digit.
261
void setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits);
264
* Returns the locale for which this object was constructed.
267
inline Locale getLocale() const;
270
* Returns the locale for this object. Two flavors are available:
271
* valid and actual locale.
274
Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const;
277
* Get pattern string for 'CurrencySpacing' that can be applied to
279
* This API gets the CurrencySpacing data from ResourceBundle. The pattern can
280
* be empty if there is no data from current locale and its parent locales.
282
* @param type : kCurrencyMatch, kSurroundingMatch or kInsert.
283
* @param beforeCurrency : true if the pattern is for before currency symbol.
284
* false if the pattern is for after currency symbol.
285
* @param status: Input/output parameter, set to success or
286
* failure code upon return.
287
* @return pattern string for currencyMatch, surroundingMatch or spaceInsert.
288
* Return empty string if there is no data for this locale and its parent
292
const UnicodeString& getPatternForCurrencySpacing(ECurrencySpacing type,
293
UBool beforeCurrency,
294
UErrorCode& status) const;
296
* Set pattern string for 'CurrencySpacing' that can be applied to
299
* @param type : kCurrencyMatch, kSurroundingMatch or kInsert.
300
* @param beforeCurrency : true if the pattern is for before currency symbol.
301
* false if the pattern is for after currency symbol.
302
* @param pattern : pattern string to override current setting.
305
void setPatternForCurrencySpacing(ECurrencySpacing type,
306
UBool beforeCurrency,
307
const UnicodeString& pattern);
310
* ICU "poor man's RTTI", returns a UClassID for the actual class.
314
virtual UClassID getDynamicClassID() const;
317
* ICU "poor man's RTTI", returns a UClassID for this class.
321
static UClassID U_EXPORT2 getStaticClassID();
324
DecimalFormatSymbols(); // default constructor not implemented
327
* Initializes the symbols from the LocaleElements resource bundle.
328
* Note: The organization of LocaleElements badly needs to be
331
* @param locale The locale to get symbols for.
332
* @param success Input/output parameter, set to success or
333
* failure code upon return.
334
* @param useLastResortData determine if use last resort data
336
void initialize(const Locale& locale, UErrorCode& success, UBool useLastResortData = FALSE);
339
* Initialize the symbols with default values.
343
void setCurrencyForSymbols();
347
* _Internal_ function - more efficient version of getSymbol,
348
* returning a const reference to one of the symbol strings.
349
* The returned reference becomes invalid when the symbol is changed
350
* or when the DecimalFormatSymbols are destroyed.
351
* ### TODO markus 2002oct11: Consider proposing getConstSymbol() to be really public.
353
* @param symbol Constant to indicate a number format symbol.
354
* @return the format symbol by the param 'symbol'
357
inline const UnicodeString &getConstSymbol(ENumberFormatSymbol symbol) const;
360
* Returns that pattern stored in currecy info. Internal API for use by NumberFormat API.
363
inline const UChar* getCurrencyPattern(void) const;
367
* Private symbol strings.
368
* They are either loaded from a resource bundle or otherwise owned.
369
* setSymbol() clones the symbol string.
370
* Readonly aliases can only come from a resource bundle, so that we can always
371
* use fastCopyFrom() with them.
373
* If DecimalFormatSymbols becomes subclassable and the status of fSymbols changes
374
* from private to protected,
375
* or when fSymbols can be set any other way that allows them to be readonly aliases
376
* to non-resource bundle strings,
377
* then regular UnicodeString copies must be used instead of fastCopyFrom().
381
UnicodeString fSymbols[kFormatSymbolCount];
384
* Non-symbol variable for getConstSymbol(). Always empty.
387
UnicodeString fNoSymbol;
391
char actualLocale[ULOC_FULLNAME_CAPACITY];
392
char validLocale[ULOC_FULLNAME_CAPACITY];
393
const UChar* currPattern;
395
UnicodeString currencySpcBeforeSym[kCurrencySpacingCount];
396
UnicodeString currencySpcAfterSym[kCurrencySpacingCount];
399
// -------------------------------------
402
DecimalFormatSymbols::getSymbol(ENumberFormatSymbol symbol) const {
403
const UnicodeString *strPtr;
404
if(symbol < kFormatSymbolCount) {
405
strPtr = &fSymbols[symbol];
412
inline const UnicodeString &
413
DecimalFormatSymbols::getConstSymbol(ENumberFormatSymbol symbol) const {
414
const UnicodeString *strPtr;
415
if(symbol < kFormatSymbolCount) {
416
strPtr = &fSymbols[symbol];
423
// -------------------------------------
426
DecimalFormatSymbols::setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits = TRUE) {
427
if(symbol<kFormatSymbolCount) {
428
fSymbols[symbol]=value;
431
// If the zero digit is being set to a known zero digit according to Unicode,
432
// then we automatically set the corresponding 1-9 digits
433
if ( propogateDigits && symbol == kZeroDigitSymbol && value.countChar32() == 1 ) {
434
UChar32 sym = value.char32At(0);
435
if ( u_charDigitValue(sym) == 0 ) {
436
for ( int8_t i = 1 ; i<= 9 ; i++ ) {
438
fSymbols[(int)kOneDigitSymbol+i-1] = UnicodeString(sym);
444
// -------------------------------------
447
DecimalFormatSymbols::getLocale() const {
452
DecimalFormatSymbols::getCurrencyPattern() const {
457
#endif /* #if !UCONFIG_NO_FORMATTING */