2 *******************************************************************************
3 * Copyright (C) 1997-2003, International Business Machines Corporation and *
4 * 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 Implemented with C++ APIs.
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/20/98 stephen Slightly modified initialization of monetarySeparator
17 ********************************************************************************
20 #include "unicode/utypes.h"
22 #if !UCONFIG_NO_FORMATTING
24 #include "unicode/dcfmtsym.h"
25 #include "unicode/resbund.h"
26 #include "unicode/decimfmt.h"
27 #include "unicode/ucurr.h"
28 #include "unicode/choicfmt.h"
30 // *****************************************************************************
31 // class DecimalFormatSymbols
32 // *****************************************************************************
36 const char DecimalFormatSymbols::fgClassID
=0;
38 const char DecimalFormatSymbols::fgNumberElements
[] = "NumberElements";
40 static const UChar INTL_CURRENCY_SYMBOL_STR
[] = {0xa4, 0xa4, 0};
42 // -------------------------------------
43 // Initializes this with the decimal format symbols in the default locale.
45 DecimalFormatSymbols::DecimalFormatSymbols(UErrorCode
& status
)
48 initialize(Locale::getDefault(), status
, TRUE
);
51 // -------------------------------------
52 // Initializes this with the decimal format symbols in the desired locale.
54 DecimalFormatSymbols::DecimalFormatSymbols(const Locale
& loc
, UErrorCode
& status
)
57 initialize(loc
, status
);
60 // -------------------------------------
62 DecimalFormatSymbols::~DecimalFormatSymbols()
66 // -------------------------------------
69 DecimalFormatSymbols::DecimalFormatSymbols(const DecimalFormatSymbols
&source
)
73 for(i
= 0; i
< (int)kFormatSymbolCount
; ++i
) {
74 // fastCopyFrom is safe, see docs on fSymbols
75 fSymbols
[(ENumberFormatSymbol
)i
].fastCopyFrom(source
.fSymbols
[(ENumberFormatSymbol
)i
]);
79 // -------------------------------------
80 // assignment operator
83 DecimalFormatSymbols::operator=(const DecimalFormatSymbols
& rhs
)
88 for(i
= 0; i
< (int)kFormatSymbolCount
; ++i
) {
89 // fastCopyFrom is safe, see docs on fSymbols
90 fSymbols
[(ENumberFormatSymbol
)i
].fastCopyFrom(rhs
.fSymbols
[(ENumberFormatSymbol
)i
]);
96 // -------------------------------------
99 DecimalFormatSymbols::operator==(const DecimalFormatSymbols
& that
) const
106 for(i
= 0; i
< (int)kFormatSymbolCount
; ++i
) {
107 if(fSymbols
[(ENumberFormatSymbol
)i
] != that
.fSymbols
[(ENumberFormatSymbol
)i
]) {
114 // -------------------------------------
117 DecimalFormatSymbols::initialize(const Locale
& loc
, UErrorCode
& status
,
118 UBool useLastResortData
)
120 if (U_FAILURE(status
)) return;
124 ResourceBundle
resource((char *)0, loc
, status
);
125 if (U_FAILURE(status
))
127 // Initializes with last resort data if necessary.
128 if (useLastResortData
)
130 status
= U_USING_FALLBACK_WARNING
;
136 // Gets the number element array.
138 ResourceBundle numberElementsRes
= resource
.get(fgNumberElements
, status
);
139 int32_t numberElementsLength
= numberElementsRes
.getSize();
141 // If the array size is too small, something is wrong with the resource
142 // bundle, returns the failure error code.
143 if (numberElementsLength
< 11) {
144 status
= U_INVALID_FORMAT_ERROR
;
148 UnicodeString numberElements
[kFormatSymbolCount
];
149 for(i
= 0; i
<numberElementsLength
; i
++) {
150 numberElements
[i
].fastCopyFrom(numberElementsRes
.getStringEx(i
, status
));
153 if (U_FAILURE(status
)) {
157 initialize(numberElements
, numberElementsLength
);
160 // Obtain currency data from the currency API. This is strictly
161 // for backward compatibility; we don't use DecimalFormatSymbols
162 // for currency data anymore.
163 UErrorCode ec
= U_ZERO_ERROR
; // don't propagate failures out
164 const char* l
= loc
.getName();
165 const UChar
* curriso
= ucurr_forLocale(l
, &ec
);
166 UBool isChoiceFormat
;
168 const UChar
* currname
= ucurr_getName(curriso
, l
, UCURR_SYMBOL_NAME
,
169 &isChoiceFormat
, &len
, &ec
);
171 fSymbols
[kIntlCurrencySymbol
] = curriso
;
173 // If this is a ChoiceFormat currency, then format an
174 // arbitrary value; pick something != 1; more common.
175 fSymbols
[kCurrencySymbol
].truncate(0);
176 if (isChoiceFormat
) {
177 ChoiceFormat
f(currname
, ec
);
179 f
.format(2.0, fSymbols
[kCurrencySymbol
]);
181 fSymbols
[kCurrencySymbol
] = fSymbols
[kIntlCurrencySymbol
];
184 fSymbols
[kCurrencySymbol
] = UnicodeString(currname
);
187 /* else use the default values. */
190 // Initializes the DecimalFormatSymbol instance with the data obtained
191 // from ResourceBundle in the desired locale.
194 DecimalFormatSymbols::initialize(const UnicodeString
* numberElements
, int32_t numberElementsLength
)
196 fSymbols
[kDecimalSeparatorSymbol
].fastCopyFrom(numberElements
[0]);
197 fSymbols
[kGroupingSeparatorSymbol
].fastCopyFrom(numberElements
[1]);
198 fSymbols
[kPatternSeparatorSymbol
].fastCopyFrom(numberElements
[2]);
199 fSymbols
[kPercentSymbol
].fastCopyFrom(numberElements
[3]);
200 fSymbols
[kZeroDigitSymbol
].fastCopyFrom(numberElements
[4]);
201 fSymbols
[kDigitSymbol
].fastCopyFrom(numberElements
[5]);
202 fSymbols
[kMinusSignSymbol
].fastCopyFrom(numberElements
[6]);
203 fSymbols
[kPlusSignSymbol
] = (UChar
)0x002b; // '+' Hard coded for now; get from resource later
204 fSymbols
[kExponentialSymbol
].fastCopyFrom(numberElements
[7]);
205 fSymbols
[kPerMillSymbol
].fastCopyFrom(numberElements
[8]);
206 fSymbols
[kPadEscapeSymbol
] = (UChar
)0x002a; // '*' Hard coded for now; get from resource later
207 fSymbols
[kInfinitySymbol
].fastCopyFrom(numberElements
[9]);
208 fSymbols
[kNaNSymbol
].fastCopyFrom(numberElements
[10]);
210 // If there is a currency decimal, use it.
211 fSymbols
[kMonetarySeparatorSymbol
].fastCopyFrom(numberElements
[numberElementsLength
>= 12 ? 11 : 0]);
213 // Default values until it's set later on.
214 fSymbols
[kCurrencySymbol
] = (UChar
)0xa4; // 'OX' currency symbol
215 fSymbols
[kIntlCurrencySymbol
] = INTL_CURRENCY_SYMBOL_STR
;
218 // initialize with default values
220 DecimalFormatSymbols::initialize() {
222 * These strings used to be in static arrays, but the HP/UX aCC compiler
223 * cannot initialize a static array with class constructors.
226 fSymbols
[kDecimalSeparatorSymbol
] = (UChar
)0x2e; // '.' decimal separator
227 fSymbols
[kGroupingSeparatorSymbol
].remove(); // group (thousands) separator
228 fSymbols
[kPatternSeparatorSymbol
] = (UChar
)0x3b; // ';' pattern separator
229 fSymbols
[kPercentSymbol
] = (UChar
)0x25; // '%' percent sign
230 fSymbols
[kZeroDigitSymbol
] = (UChar
)0x30; // '0' native 0 digit
231 fSymbols
[kDigitSymbol
] = (UChar
)0x23; // '#' pattern digit
232 fSymbols
[kMinusSignSymbol
] = (UChar
)0x2d; // '-' minus sign
233 fSymbols
[kPlusSignSymbol
] = (UChar
)0x002b; // '+' plus sign
234 fSymbols
[kCurrencySymbol
] = (UChar
)0xa4; // 'OX' currency symbol
235 fSymbols
[kIntlCurrencySymbol
] = INTL_CURRENCY_SYMBOL_STR
;
236 fSymbols
[kMonetarySeparatorSymbol
] = (UChar
)0x2e; // '.' monetary decimal separator
237 fSymbols
[kExponentialSymbol
] = (UChar
)0x45; // 'E' exponential
238 fSymbols
[kPerMillSymbol
] = (UChar
)0x2030; // '%o' per mill
239 fSymbols
[kPadEscapeSymbol
] = (UChar
)0x2a; // '*' pad escape symbol
240 fSymbols
[kInfinitySymbol
] = (UChar
)0x221e; // 'oo' infinite
241 fSymbols
[kNaNSymbol
] = (UChar
)0xfffd; // SUB NaN
246 #endif /* #if !UCONFIG_NO_FORMATTING */