]>
Commit | Line | Data |
---|---|---|
b75a7d8f | 1 | /* |
73c04bcf A |
2 | ******************************************************************************** |
3 | * Copyright (C) 1997-2006, International Business Machines Corporation and others. | |
4 | * All Rights Reserved. | |
b75a7d8f A |
5 | ******************************************************************************** |
6 | * | |
7 | * File NUMFMT.H | |
8 | * | |
9 | * Modification History: | |
10 | * | |
11 | * Date Name Description | |
12 | * 02/19/97 aliu Converted from java. | |
13 | * 03/18/97 clhuang Updated per C++ implementation. | |
14 | * 04/17/97 aliu Changed DigitCount to int per code review. | |
15 | * 07/20/98 stephen JDK 1.2 sync up. Added scientific support. | |
16 | * Changed naming conventions to match C++ guidelines | |
17 | * Derecated Java style constants (eg, INTEGER_FIELD) | |
18 | ******************************************************************************** | |
19 | */ | |
20 | ||
21 | #ifndef NUMFMT_H | |
22 | #define NUMFMT_H | |
23 | ||
24 | ||
25 | #include "unicode/utypes.h" | |
26 | ||
73c04bcf A |
27 | /** |
28 | * \file | |
29 | * \brief C++ API: Abstract base class for all number formats. | |
30 | */ | |
31 | ||
b75a7d8f A |
32 | #if !UCONFIG_NO_FORMATTING |
33 | ||
34 | #include "unicode/unistr.h" | |
35 | #include "unicode/format.h" | |
36 | #include "unicode/unum.h" // UNumberFormatStyle | |
37 | #include "unicode/locid.h" | |
38 | ||
39 | U_NAMESPACE_BEGIN | |
40 | ||
374ca955 | 41 | #if !UCONFIG_NO_SERVICE |
b75a7d8f A |
42 | class NumberFormatFactory; |
43 | class StringEnumeration; | |
374ca955 | 44 | #endif |
b75a7d8f A |
45 | |
46 | /** | |
73c04bcf | 47 | * |
b75a7d8f A |
48 | * Abstract base class for all number formats. Provides interface for |
49 | * formatting and parsing a number. Also provides methods for | |
50 | * determining which locales have number formats, and what their names | |
51 | * are. | |
52 | * <P> | |
53 | * NumberFormat helps you to format and parse numbers for any locale. | |
54 | * Your code can be completely independent of the locale conventions | |
55 | * for decimal points, thousands-separators, or even the particular | |
56 | * decimal digits used, or whether the number format is even decimal. | |
57 | * <P> | |
58 | * To format a number for the current Locale, use one of the static | |
59 | * factory methods: | |
60 | * <pre> | |
61 | * \code | |
62 | * double myNumber = 7.0; | |
63 | * UnicodeString myString; | |
64 | * UErrorCode success = U_ZERO_ERROR; | |
65 | * NumberFormat* nf = NumberFormat::createInstance(success) | |
66 | * nf->format(myNumber, myString); | |
67 | * cout << " Example 1: " << myString << endl; | |
68 | * \endcode | |
69 | * </pre> | |
70 | * If you are formatting multiple numbers, it is more efficient to get | |
71 | * the format and use it multiple times so that the system doesn't | |
72 | * have to fetch the information about the local language and country | |
73 | * conventions multiple times. | |
74 | * <pre> | |
75 | * \code | |
76 | * UnicodeString myString; | |
77 | * UErrorCode success = U_ZERO_ERROR; | |
78 | * nf = NumberFormat::createInstance( success ); | |
79 | * int32_t a[] = { 123, 3333, -1234567 }; | |
80 | * const int32_t a_len = sizeof(a) / sizeof(a[0]); | |
81 | * myString.remove(); | |
82 | * for (int32_t i = 0; i < a_len; i++) { | |
83 | * nf->format(a[i], myString); | |
84 | * myString += " ; "; | |
85 | * } | |
86 | * cout << " Example 2: " << myString << endl; | |
374ca955 | 87 | * \endcode |
b75a7d8f A |
88 | * </pre> |
89 | * To format a number for a different Locale, specify it in the | |
90 | * call to createInstance(). | |
91 | * <pre> | |
92 | * \code | |
93 | * nf = NumberFormat::createInstance( Locale::FRENCH, success ); | |
94 | * \endcode | |
95 | * </pre> | |
96 | * You can use a NumberFormat to parse also. | |
97 | * <pre> | |
98 | * \code | |
99 | * UErrorCode success; | |
100 | * Formattable result(-999); // initialized with error code | |
101 | * nf->parse(myString, result, success); | |
102 | * \endcode | |
103 | * </pre> | |
104 | * Use createInstance to get the normal number format for that country. | |
105 | * There are other static factory methods available. Use getCurrency | |
106 | * to get the currency number format for that country. Use getPercent | |
107 | * to get a format for displaying percentages. With this format, a | |
108 | * fraction from 0.53 is displayed as 53%. | |
109 | * <P> | |
110 | * You can also control the display of numbers with such methods as | |
111 | * getMinimumFractionDigits. If you want even more control over the | |
112 | * format or parsing, or want to give your users more control, you can | |
113 | * try casting the NumberFormat you get from the factory methods to a | |
114 | * DecimalNumberFormat. This will work for the vast majority of | |
115 | * countries; just remember to put it in a try block in case you | |
116 | * encounter an unusual one. | |
117 | * <P> | |
118 | * You can also use forms of the parse and format methods with | |
119 | * ParsePosition and FieldPosition to allow you to: | |
120 | * <ul type=round> | |
121 | * <li>(a) progressively parse through pieces of a string. | |
122 | * <li>(b) align the decimal point and other areas. | |
123 | * </ul> | |
124 | * For example, you can align numbers in two ways. | |
125 | * <P> | |
126 | * If you are using a monospaced font with spacing for alignment, you | |
127 | * can pass the FieldPosition in your format call, with field = | |
128 | * INTEGER_FIELD. On output, getEndIndex will be set to the offset | |
129 | * between the last character of the integer and the decimal. Add | |
130 | * (desiredSpaceCount - getEndIndex) spaces at the front of the | |
131 | * string. | |
132 | * <P> | |
133 | * If you are using proportional fonts, instead of padding with | |
134 | * spaces, measure the width of the string in pixels from the start to | |
135 | * getEndIndex. Then move the pen by (desiredPixelWidth - | |
136 | * widthToAlignmentPoint) before drawing the text. It also works | |
137 | * where there is no decimal, but possibly additional characters at | |
138 | * the end, e.g. with parentheses in negative numbers: "(12)" for -12. | |
374ca955 A |
139 | * <p> |
140 | * <em>User subclasses are not supported.</em> While clients may write | |
141 | * subclasses, such code will not necessarily work and will not be | |
142 | * guaranteed to work stably from release to release. | |
143 | * | |
b75a7d8f A |
144 | * @stable ICU 2.0 |
145 | */ | |
146 | class U_I18N_API NumberFormat : public Format { | |
147 | public: | |
148 | ||
149 | /** | |
150 | * Alignment Field constants used to construct a FieldPosition object. | |
151 | * Signifies that the position of the integer part or fraction part of | |
152 | * a formatted number should be returned. | |
153 | * | |
154 | * @see FieldPosition | |
155 | * @stable ICU 2.0 | |
156 | */ | |
157 | enum EAlignmentFields { | |
158 | kIntegerField, | |
159 | kFractionField, | |
160 | ||
161 | ||
162 | /** | |
163 | * These constants are provided for backwards compatibility only. | |
164 | * Please use the C++ style constants defined above. | |
165 | * @stable ICU 2.0 | |
166 | */ | |
167 | INTEGER_FIELD = kIntegerField, | |
168 | FRACTION_FIELD = kFractionField | |
169 | }; | |
170 | ||
171 | /** | |
172 | * Destructor. | |
173 | * @stable ICU 2.0 | |
174 | */ | |
175 | virtual ~NumberFormat(); | |
176 | ||
177 | /** | |
178 | * Return true if the given Format objects are semantically equal. | |
179 | * Objects of different subclasses are considered unequal. | |
180 | * @return true if the given Format objects are semantically equal. | |
181 | * @stable ICU 2.0 | |
182 | */ | |
183 | virtual UBool operator==(const Format& other) const; | |
184 | ||
185 | /** | |
186 | * Format an object to produce a string. This method handles | |
187 | * Formattable objects with numeric types. If the Formattable | |
188 | * object type is not a numeric type, then it returns a failing | |
189 | * UErrorCode. | |
190 | * | |
191 | * @param obj The object to format. | |
192 | * @param appendTo Output parameter to receive result. | |
193 | * Result is appended to existing contents. | |
194 | * @param pos On input: an alignment field, if desired. | |
195 | * On output: the offsets of the alignment field. | |
196 | * @param status Output param filled with success/failure status. | |
197 | * @return Reference to 'appendTo' parameter. | |
198 | * @stable ICU 2.0 | |
199 | */ | |
200 | virtual UnicodeString& format(const Formattable& obj, | |
201 | UnicodeString& appendTo, | |
202 | FieldPosition& pos, | |
203 | UErrorCode& status) const; | |
204 | ||
205 | /** | |
206 | * Parse a string to produce an object. This methods handles | |
207 | * parsing of numeric strings into Formattable objects with numeric | |
208 | * types. | |
209 | * <P> | |
210 | * Before calling, set parse_pos.index to the offset you want to | |
211 | * start parsing at in the source. After calling, parse_pos.index | |
374ca955 A |
212 | * indicates the position after the successfully parsed text. If |
213 | * an error occurs, parse_pos.index is unchanged. | |
b75a7d8f A |
214 | * <P> |
215 | * When parsing, leading whitespace is discarded (with successful | |
216 | * parse), while trailing whitespace is left as is. | |
217 | * <P> | |
218 | * See Format::parseObject() for more. | |
219 | * | |
220 | * @param source The string to be parsed into an object. | |
221 | * @param result Formattable to be set to the parse result. | |
222 | * If parse fails, return contents are undefined. | |
223 | * @param parse_pos The position to start parsing at. Upon return | |
224 | * this param is set to the position after the | |
225 | * last character successfully parsed. If the | |
226 | * source is not parsed successfully, this param | |
227 | * will remain unchanged. | |
228 | * @return A newly created Formattable* object, or NULL | |
229 | * on failure. The caller owns this and should | |
230 | * delete it when done. | |
231 | * @stable ICU 2.0 | |
232 | */ | |
233 | virtual void parseObject(const UnicodeString& source, | |
234 | Formattable& result, | |
235 | ParsePosition& parse_pos) const; | |
236 | ||
237 | /** | |
238 | * Format a double number. These methods call the NumberFormat | |
239 | * pure virtual format() methods with the default FieldPosition. | |
240 | * | |
241 | * @param number The value to be formatted. | |
242 | * @param appendTo Output parameter to receive result. | |
243 | * Result is appended to existing contents. | |
244 | * @return Reference to 'appendTo' parameter. | |
245 | * @stable ICU 2.0 | |
246 | */ | |
247 | UnicodeString& format( double number, | |
248 | UnicodeString& appendTo) const; | |
249 | ||
250 | /** | |
251 | * Format a long number. These methods call the NumberFormat | |
252 | * pure virtual format() methods with the default FieldPosition. | |
253 | * | |
254 | * @param number The value to be formatted. | |
255 | * @param appendTo Output parameter to receive result. | |
256 | * Result is appended to existing contents. | |
257 | * @return Reference to 'appendTo' parameter. | |
258 | * @stable ICU 2.0 | |
259 | */ | |
260 | UnicodeString& format( int32_t number, | |
261 | UnicodeString& appendTo) const; | |
262 | ||
374ca955 A |
263 | /** |
264 | * Format an int64 number. These methods call the NumberFormat | |
265 | * pure virtual format() methods with the default FieldPosition. | |
266 | * | |
267 | * @param number The value to be formatted. | |
268 | * @param appendTo Output parameter to receive result. | |
269 | * Result is appended to existing contents. | |
270 | * @return Reference to 'appendTo' parameter. | |
73c04bcf | 271 | * @stable ICU 2.8 |
374ca955 A |
272 | */ |
273 | UnicodeString& format( int64_t number, | |
274 | UnicodeString& appendTo) const; | |
275 | ||
b75a7d8f A |
276 | /** |
277 | * Format a double number. Concrete subclasses must implement | |
278 | * these pure virtual methods. | |
279 | * | |
280 | * @param number The value to be formatted. | |
281 | * @param appendTo Output parameter to receive result. | |
282 | * Result is appended to existing contents. | |
283 | * @param pos On input: an alignment field, if desired. | |
284 | * On output: the offsets of the alignment field. | |
285 | * @return Reference to 'appendTo' parameter. | |
286 | * @stable ICU 2.0 | |
287 | */ | |
288 | virtual UnicodeString& format(double number, | |
289 | UnicodeString& appendTo, | |
290 | FieldPosition& pos) const = 0; | |
291 | /** | |
292 | * Format a long number. Concrete subclasses must implement | |
293 | * these pure virtual methods. | |
294 | * | |
295 | * @param number The value to be formatted. | |
296 | * @param appendTo Output parameter to receive result. | |
297 | * Result is appended to existing contents. | |
298 | * @param pos On input: an alignment field, if desired. | |
299 | * On output: the offsets of the alignment field. | |
300 | * @return Reference to 'appendTo' parameter. | |
301 | * @stable ICU 2.0 | |
302 | */ | |
303 | virtual UnicodeString& format(int32_t number, | |
304 | UnicodeString& appendTo, | |
305 | FieldPosition& pos) const = 0; | |
306 | ||
374ca955 A |
307 | /** |
308 | * Format an int64 number. (Not abstract to retain compatibility | |
309 | * with earlier releases, however subclasses should override this | |
310 | * method as it just delegates to format(int32_t number...); | |
311 | * | |
312 | * @param number The value to be formatted. | |
313 | * @param appendTo Output parameter to receive result. | |
314 | * Result is appended to existing contents. | |
315 | * @param pos On input: an alignment field, if desired. | |
316 | * On output: the offsets of the alignment field. | |
317 | * @return Reference to 'appendTo' parameter. | |
73c04bcf | 318 | * @stable ICU 2.8 |
374ca955 A |
319 | */ |
320 | virtual UnicodeString& format(int64_t number, | |
321 | UnicodeString& appendTo, | |
322 | FieldPosition& pos) const; | |
b75a7d8f A |
323 | /** |
324 | * Redeclared Format method. | |
325 | * @param obj The object to be formatted. | |
326 | * @param appendTo Output parameter to receive result. | |
327 | * Result is appended to existing contents. | |
328 | * @param status Output parameter set to a failure error code | |
329 | * when a failure occurs. | |
330 | * @return Reference to 'appendTo' parameter. | |
331 | * @stable ICU 2.0 | |
332 | */ | |
333 | UnicodeString& format(const Formattable& obj, | |
334 | UnicodeString& appendTo, | |
335 | UErrorCode& status) const; | |
336 | ||
337 | /** | |
338 | * Return a long if possible (e.g. within range LONG_MAX, | |
339 | * LONG_MAX], and with no decimals), otherwise a double. If | |
340 | * IntegerOnly is set, will stop at a decimal point (or equivalent; | |
341 | * e.g. for rational numbers "1 2/3", will stop after the 1). | |
342 | * <P> | |
343 | * If no object can be parsed, index is unchanged, and NULL is | |
344 | * returned. | |
345 | * <P> | |
346 | * This is a pure virtual which concrete subclasses must implement. | |
347 | * | |
348 | * @param text The text to be parsed. | |
349 | * @param result Formattable to be set to the parse result. | |
350 | * If parse fails, return contents are undefined. | |
351 | * @param parsePosition The position to start parsing at on input. | |
352 | * On output, moved to after the last successfully | |
353 | * parse character. On parse failure, does not change. | |
354 | * @return A Formattable object of numeric type. The caller | |
355 | * owns this an must delete it. NULL on failure. | |
356 | * @stable ICU 2.0 | |
357 | */ | |
358 | virtual void parse(const UnicodeString& text, | |
359 | Formattable& result, | |
360 | ParsePosition& parsePosition) const = 0; | |
361 | ||
362 | /** | |
363 | * Parse a string as a numeric value, and return a Formattable | |
364 | * numeric object. This method parses integers only if IntegerOnly | |
365 | * is set. | |
366 | * | |
367 | * @param text The text to be parsed. | |
368 | * @param result Formattable to be set to the parse result. | |
369 | * If parse fails, return contents are undefined. | |
370 | * @param status Output parameter set to a failure error code | |
371 | * when a failure occurs. | |
372 | * @return A Formattable object of numeric type. The caller | |
373 | * owns this an must delete it. NULL on failure. | |
374 | * @see NumberFormat::isParseIntegerOnly | |
375 | * @stable ICU 2.0 | |
376 | */ | |
377 | virtual void parse( const UnicodeString& text, | |
378 | Formattable& result, | |
379 | UErrorCode& status) const; | |
380 | ||
374ca955 A |
381 | /** |
382 | * Parses text from the given string as a currency amount. Unlike | |
383 | * the parse() method, this method will attempt to parse a generic | |
384 | * currency name, searching for a match of this object's locale's | |
385 | * currency display names, or for a 3-letter ISO currency code. | |
386 | * This method will fail if this format is not a currency format, | |
387 | * that is, if it does not contain the currency pattern symbol | |
388 | * (U+00A4) in its prefix or suffix. | |
389 | * | |
390 | * @param text the string to parse | |
391 | * @param result output parameter to receive result. This will have | |
392 | * its currency set to the parsed ISO currency code. | |
393 | * @param pos input-output position; on input, the position within | |
394 | * text to match; must have 0 <= pos.getIndex() < text.length(); | |
395 | * on output, the position after the last matched character. If | |
396 | * the parse fails, the position in unchanged upon output. | |
397 | * @return a reference to result | |
398 | * @internal | |
399 | */ | |
400 | virtual Formattable& parseCurrency(const UnicodeString& text, | |
401 | Formattable& result, | |
402 | ParsePosition& pos) const; | |
403 | ||
b75a7d8f A |
404 | /** |
405 | * Return true if this format will parse numbers as integers | |
406 | * only. For example in the English locale, with ParseIntegerOnly | |
407 | * true, the string "1234." would be parsed as the integer value | |
408 | * 1234 and parsing would stop at the "." character. Of course, | |
409 | * the exact format accepted by the parse operation is locale | |
410 | * dependant and determined by sub-classes of NumberFormat. | |
411 | * @return true if this format will parse numbers as integers | |
374ca955 | 412 | * only. |
b75a7d8f A |
413 | * @stable ICU 2.0 |
414 | */ | |
415 | UBool isParseIntegerOnly(void) const; | |
416 | ||
417 | /** | |
418 | * Sets whether or not numbers should be parsed as integers only. | |
419 | * @param value set True, this format will parse numbers as integers | |
420 | * only. | |
421 | * @see isParseIntegerOnly | |
422 | * @stable ICU 2.0 | |
423 | */ | |
424 | virtual void setParseIntegerOnly(UBool value); | |
425 | ||
426 | /** | |
427 | * Returns the default number format for the current default | |
428 | * locale. The default format is one of the styles provided by | |
429 | * the other factory methods: getNumberInstance, | |
430 | * getCurrencyInstance or getPercentInstance. Exactly which one | |
431 | * is locale dependant. | |
432 | * @stable ICU 2.0 | |
433 | */ | |
374ca955 | 434 | static NumberFormat* U_EXPORT2 createInstance(UErrorCode&); |
b75a7d8f A |
435 | |
436 | /** | |
437 | * Returns the default number format for the specified locale. | |
438 | * The default format is one of the styles provided by the other | |
439 | * factory methods: getNumberInstance, getCurrencyInstance or | |
440 | * getPercentInstance. Exactly which one is locale dependant. | |
441 | * @param inLocale the given locale. | |
442 | * @stable ICU 2.0 | |
443 | */ | |
374ca955 | 444 | static NumberFormat* U_EXPORT2 createInstance(const Locale& inLocale, |
b75a7d8f A |
445 | UErrorCode&); |
446 | ||
447 | /** | |
448 | * Returns a currency format for the current default locale. | |
449 | * @stable ICU 2.0 | |
450 | */ | |
374ca955 | 451 | static NumberFormat* U_EXPORT2 createCurrencyInstance(UErrorCode&); |
b75a7d8f A |
452 | |
453 | /** | |
454 | * Returns a currency format for the specified locale. | |
455 | * @param inLocale the given locale. | |
456 | * @stable ICU 2.0 | |
457 | */ | |
374ca955 | 458 | static NumberFormat* U_EXPORT2 createCurrencyInstance(const Locale& inLocale, |
b75a7d8f A |
459 | UErrorCode&); |
460 | ||
461 | /** | |
462 | * Returns a percentage format for the current default locale. | |
463 | * @stable ICU 2.0 | |
464 | */ | |
374ca955 | 465 | static NumberFormat* U_EXPORT2 createPercentInstance(UErrorCode&); |
b75a7d8f A |
466 | |
467 | /** | |
468 | * Returns a percentage format for the specified locale. | |
469 | * @param inLocale the given locale. | |
470 | * @stable ICU 2.0 | |
471 | */ | |
374ca955 | 472 | static NumberFormat* U_EXPORT2 createPercentInstance(const Locale& inLocale, |
b75a7d8f A |
473 | UErrorCode&); |
474 | ||
475 | /** | |
476 | * Returns a scientific format for the current default locale. | |
477 | * @stable ICU 2.0 | |
478 | */ | |
374ca955 | 479 | static NumberFormat* U_EXPORT2 createScientificInstance(UErrorCode&); |
b75a7d8f A |
480 | |
481 | /** | |
482 | * Returns a scientific format for the specified locale. | |
483 | * @param inLocale the given locale. | |
484 | * @stable ICU 2.0 | |
485 | */ | |
374ca955 | 486 | static NumberFormat* U_EXPORT2 createScientificInstance(const Locale& inLocale, |
b75a7d8f A |
487 | UErrorCode&); |
488 | ||
489 | /** | |
490 | * Get the set of Locales for which NumberFormats are installed. | |
491 | * @param count Output param to receive the size of the locales | |
492 | * @stable ICU 2.0 | |
493 | */ | |
374ca955 | 494 | static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count); |
b75a7d8f | 495 | |
374ca955 | 496 | #if !UCONFIG_NO_SERVICE |
b75a7d8f A |
497 | /** |
498 | * Register a new NumberFormatFactory. The factory will be adopted. | |
499 | * @param toAdopt the NumberFormatFactory instance to be adopted | |
500 | * @param status the in/out status code, no special meanings are assigned | |
501 | * @return a registry key that can be used to unregister this factory | |
374ca955 | 502 | * @stable ICU 2.6 |
b75a7d8f | 503 | */ |
374ca955 | 504 | static URegistryKey U_EXPORT2 registerFactory(NumberFormatFactory* toAdopt, UErrorCode& status); |
b75a7d8f A |
505 | |
506 | /** | |
507 | * Unregister a previously-registered NumberFormatFactory using the key returned from the | |
508 | * register call. Key becomes invalid after a successful call and should not be used again. | |
509 | * The NumberFormatFactory corresponding to the key will be deleted. | |
510 | * @param key the registry key returned by a previous call to registerFactory | |
511 | * @param status the in/out status code, no special meanings are assigned | |
512 | * @return TRUE if the factory for the key was successfully unregistered | |
374ca955 | 513 | * @stable ICU 2.6 |
b75a7d8f | 514 | */ |
374ca955 | 515 | static UBool U_EXPORT2 unregister(URegistryKey key, UErrorCode& status); |
b75a7d8f A |
516 | |
517 | /** | |
374ca955 | 518 | * Return a StringEnumeration over the locales available at the time of the call, |
b75a7d8f A |
519 | * including registered locales. |
520 | * @return a StringEnumeration over the locales available at the time of the call | |
374ca955 | 521 | * @stable ICU 2.6 |
b75a7d8f | 522 | */ |
374ca955 A |
523 | static StringEnumeration* U_EXPORT2 getAvailableLocales(void); |
524 | #endif /* UCONFIG_NO_SERVICE */ | |
b75a7d8f A |
525 | |
526 | /** | |
527 | * Returns true if grouping is used in this format. For example, | |
528 | * in the English locale, with grouping on, the number 1234567 | |
529 | * might be formatted as "1,234,567". The grouping separator as | |
530 | * well as the size of each group is locale dependant and is | |
531 | * determined by sub-classes of NumberFormat. | |
532 | * @see setGroupingUsed | |
533 | * @stable ICU 2.0 | |
534 | */ | |
535 | UBool isGroupingUsed(void) const; | |
536 | ||
537 | /** | |
538 | * Set whether or not grouping will be used in this format. | |
374ca955 | 539 | * @param newValue True, grouping will be used in this format. |
b75a7d8f A |
540 | * @see getGroupingUsed |
541 | * @stable ICU 2.0 | |
542 | */ | |
543 | virtual void setGroupingUsed(UBool newValue); | |
544 | ||
545 | /** | |
546 | * Returns the maximum number of digits allowed in the integer portion of a | |
547 | * number. | |
548 | * @return the maximum number of digits allowed in the integer portion of a | |
549 | * number. | |
550 | * @see setMaximumIntegerDigits | |
551 | * @stable ICU 2.0 | |
552 | */ | |
553 | int32_t getMaximumIntegerDigits(void) const; | |
554 | ||
555 | /** | |
556 | * Sets the maximum number of digits allowed in the integer portion of a | |
557 | * number. maximumIntegerDigits must be >= minimumIntegerDigits. If the | |
558 | * new value for maximumIntegerDigits is less than the current value | |
559 | * of minimumIntegerDigits, then minimumIntegerDigits will also be set to | |
560 | * the new value. | |
561 | * | |
374ca955 | 562 | * @param newValue the new value for the maximum number of digits |
b75a7d8f A |
563 | * allowed in the integer portion of a number. |
564 | * @see getMaximumIntegerDigits | |
565 | * @stable ICU 2.0 | |
566 | */ | |
567 | virtual void setMaximumIntegerDigits(int32_t newValue); | |
568 | ||
569 | /** | |
570 | * Returns the minimum number of digits allowed in the integer portion of a | |
571 | * number. | |
572 | * @return the minimum number of digits allowed in the integer portion of a | |
573 | * number. | |
574 | * @see setMinimumIntegerDigits | |
575 | * @stable ICU 2.0 | |
576 | */ | |
577 | int32_t getMinimumIntegerDigits(void) const; | |
578 | ||
579 | /** | |
580 | * Sets the minimum number of digits allowed in the integer portion of a | |
581 | * number. minimumIntegerDigits must be <= maximumIntegerDigits. If the | |
582 | * new value for minimumIntegerDigits exceeds the current value | |
583 | * of maximumIntegerDigits, then maximumIntegerDigits will also be set to | |
584 | * the new value. | |
585 | * @param newValue the new value to be set. | |
586 | * @see getMinimumIntegerDigits | |
587 | * @stable ICU 2.0 | |
588 | */ | |
589 | virtual void setMinimumIntegerDigits(int32_t newValue); | |
590 | ||
591 | /** | |
592 | * Returns the maximum number of digits allowed in the fraction portion of a | |
593 | * number. | |
594 | * @return the maximum number of digits allowed in the fraction portion of a | |
595 | * number. | |
596 | * @see setMaximumFractionDigits | |
597 | * @stable ICU 2.0 | |
598 | */ | |
599 | int32_t getMaximumFractionDigits(void) const; | |
600 | ||
601 | /** | |
602 | * Sets the maximum number of digits allowed in the fraction portion of a | |
603 | * number. maximumFractionDigits must be >= minimumFractionDigits. If the | |
604 | * new value for maximumFractionDigits is less than the current value | |
605 | * of minimumFractionDigits, then minimumFractionDigits will also be set to | |
606 | * the new value. | |
607 | * @param newValue the new value to be set. | |
608 | * @see getMaximumFractionDigits | |
609 | * @stable ICU 2.0 | |
610 | */ | |
611 | virtual void setMaximumFractionDigits(int32_t newValue); | |
612 | ||
613 | /** | |
614 | * Returns the minimum number of digits allowed in the fraction portion of a | |
615 | * number. | |
616 | * @return the minimum number of digits allowed in the fraction portion of a | |
617 | * number. | |
618 | * @see setMinimumFractionDigits | |
619 | * @stable ICU 2.0 | |
620 | */ | |
621 | int32_t getMinimumFractionDigits(void) const; | |
622 | ||
623 | /** | |
624 | * Sets the minimum number of digits allowed in the fraction portion of a | |
625 | * number. minimumFractionDigits must be <= maximumFractionDigits. If the | |
626 | * new value for minimumFractionDigits exceeds the current value | |
627 | * of maximumFractionDigits, then maximumIntegerDigits will also be set to | |
628 | * the new value | |
629 | * @param newValue the new value to be set. | |
630 | * @see getMinimumFractionDigits | |
631 | * @stable ICU 2.0 | |
632 | */ | |
633 | virtual void setMinimumFractionDigits(int32_t newValue); | |
634 | ||
635 | /** | |
636 | * Sets the currency used to display currency | |
637 | * amounts. This takes effect immediately, if this format is a | |
638 | * currency format. If this format is not a currency format, then | |
639 | * the currency is used if and when this object becomes a | |
640 | * currency format. | |
641 | * @param theCurrency a 3-letter ISO code indicating new currency | |
642 | * to use. It need not be null-terminated. May be the empty | |
643 | * string or NULL to indicate no currency. | |
374ca955 | 644 | * @param ec input-output error code |
73c04bcf | 645 | * @stable ICU 3.0 |
b75a7d8f | 646 | */ |
374ca955 | 647 | virtual void setCurrency(const UChar* theCurrency, UErrorCode& ec); |
b75a7d8f A |
648 | |
649 | /** | |
650 | * Gets the currency used to display currency | |
651 | * amounts. This may be an empty string for some subclasses. | |
652 | * @return a 3-letter null-terminated ISO code indicating | |
653 | * the currency in use, or a pointer to the empty string. | |
374ca955 | 654 | * @stable ICU 2.6 |
b75a7d8f A |
655 | */ |
656 | const UChar* getCurrency() const; | |
657 | ||
658 | public: | |
659 | ||
660 | /** | |
374ca955 A |
661 | * Return the class ID for this class. This is useful for |
662 | * comparing to a return value from getDynamicClassID(). Note that, | |
663 | * because NumberFormat is an abstract base class, no fully constructed object | |
664 | * will have the class ID returned by NumberFormat::getStaticClassID(). | |
b75a7d8f A |
665 | * @return The class ID for all objects of this class. |
666 | * @stable ICU 2.0 | |
667 | */ | |
374ca955 | 668 | static UClassID U_EXPORT2 getStaticClassID(void); |
b75a7d8f A |
669 | |
670 | /** | |
671 | * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. | |
672 | * This method is to implement a simple version of RTTI, since not all | |
673 | * C++ compilers support genuine RTTI. Polymorphic operator==() and | |
674 | * clone() methods call this method. | |
675 | * <P> | |
676 | * @return The class ID for this object. All objects of a | |
677 | * given class have the same class ID. Objects of | |
678 | * other classes have different class IDs. | |
679 | * @stable ICU 2.0 | |
680 | */ | |
681 | virtual UClassID getDynamicClassID(void) const = 0; | |
682 | ||
683 | protected: | |
684 | ||
685 | /** | |
686 | * Default constructor for subclass use only. | |
687 | * @stable ICU 2.0 | |
688 | */ | |
689 | NumberFormat(); | |
690 | ||
691 | /** | |
692 | * Copy constructor. | |
693 | * @stable ICU 2.0 | |
694 | */ | |
695 | NumberFormat(const NumberFormat&); | |
696 | ||
697 | /** | |
698 | * Assignment operator. | |
699 | * @stable ICU 2.0 | |
700 | */ | |
701 | NumberFormat& operator=(const NumberFormat&); | |
702 | ||
374ca955 A |
703 | /** |
704 | * Returns the currency in effect for this formatter. Subclasses | |
705 | * should override this method as needed. Unlike getCurrency(), | |
706 | * this method should never return "". | |
707 | * @result output parameter for null-terminated result, which must | |
708 | * have a capacity of at least 4 | |
709 | * @internal | |
710 | */ | |
711 | virtual void getEffectiveCurrency(UChar* result, UErrorCode& ec) const; | |
712 | ||
b75a7d8f | 713 | private: |
b75a7d8f A |
714 | |
715 | enum EStyles { | |
716 | kNumberStyle, | |
717 | kCurrencyStyle, | |
718 | kPercentStyle, | |
719 | kScientificStyle, | |
720 | kStyleCount // ALWAYS LAST ENUM: number of styles | |
721 | }; | |
374ca955 | 722 | |
b75a7d8f A |
723 | /** |
724 | * Creates the specified decimal format style of the desired locale. | |
374ca955 A |
725 | * Hook for service registration, uses makeInstance directly if no services |
726 | * registered. | |
b75a7d8f A |
727 | * @param desiredLocale the given locale. |
728 | * @param choice the given style. | |
729 | * @param success Output param filled with success/failure status. | |
730 | * @return A new NumberFormat instance. | |
731 | */ | |
374ca955 | 732 | static NumberFormat* U_EXPORT2 createInstance(const Locale& desiredLocale, EStyles choice, UErrorCode& success); |
b75a7d8f A |
733 | |
734 | /** | |
735 | * Creates the specified decimal format style of the desired locale. | |
736 | * @param desiredLocale the given locale. | |
737 | * @param choice the given style. | |
738 | * @param success Output param filled with success/failure status. | |
739 | * @return A new NumberFormat instance. | |
740 | */ | |
374ca955 | 741 | static NumberFormat* makeInstance(const Locale& desiredLocale, EStyles choice, UErrorCode& success); |
b75a7d8f A |
742 | |
743 | UBool fGroupingUsed; | |
744 | int32_t fMaxIntegerDigits; | |
745 | int32_t fMinIntegerDigits; | |
746 | int32_t fMaxFractionDigits; | |
747 | int32_t fMinFractionDigits; | |
748 | UBool fParseIntegerOnly; | |
749 | ||
750 | // ISO currency code | |
374ca955 | 751 | UChar fCurrency[4]; |
b75a7d8f | 752 | |
374ca955 A |
753 | friend class ICUNumberFormatFactory; // access to makeInstance, EStyles |
754 | friend class ICUNumberFormatService; | |
b75a7d8f A |
755 | }; |
756 | ||
374ca955 | 757 | #if !UCONFIG_NO_SERVICE |
b75a7d8f A |
758 | /** |
759 | * A NumberFormatFactory is used to register new number formats. The factory | |
760 | * should be able to create any of the predefined formats for each locale it | |
761 | * supports. When registered, the locales it supports extend or override the | |
762 | * locale already supported by ICU. | |
763 | * | |
374ca955 | 764 | * @stable ICU 2.6 |
b75a7d8f A |
765 | */ |
766 | class U_I18N_API NumberFormatFactory : public UObject { | |
767 | public: | |
768 | ||
374ca955 A |
769 | /** |
770 | * Destructor | |
73c04bcf | 771 | * @stable ICU 3.0 |
374ca955 A |
772 | */ |
773 | virtual ~NumberFormatFactory(); | |
774 | ||
b75a7d8f A |
775 | /** |
776 | * Return true if this factory will be visible. Default is true. | |
777 | * If not visible, the locales supported by this factory will not | |
778 | * be listed by getAvailableLocales. | |
374ca955 | 779 | * @stable ICU 2.6 |
b75a7d8f A |
780 | */ |
781 | virtual UBool visible(void) const = 0; | |
782 | ||
783 | /** | |
784 | * Return the locale names directly supported by this factory. The number of names | |
785 | * is returned in count; | |
374ca955 | 786 | * @stable ICU 2.6 |
b75a7d8f | 787 | */ |
374ca955 | 788 | virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const = 0; |
b75a7d8f A |
789 | |
790 | /** | |
791 | * Return a number format of the appropriate type. If the locale | |
792 | * is not supported, return null. If the locale is supported, but | |
793 | * the type is not provided by this service, return null. Otherwise | |
794 | * return an appropriate instance of NumberFormat. | |
374ca955 | 795 | * @stable ICU 2.6 |
b75a7d8f A |
796 | */ |
797 | virtual NumberFormat* createFormat(const Locale& loc, UNumberFormatStyle formatType) = 0; | |
798 | }; | |
799 | ||
374ca955 A |
800 | /** |
801 | * A NumberFormatFactory that supports a single locale. It can be visible or invisible. | |
73c04bcf | 802 | * @stable ICU 2.6 |
374ca955 | 803 | */ |
b75a7d8f A |
804 | class U_I18N_API SimpleNumberFormatFactory : public NumberFormatFactory { |
805 | protected: | |
806 | /** | |
807 | * True if the locale supported by this factory is visible. | |
374ca955 | 808 | * @stable ICU 2.6 |
b75a7d8f A |
809 | */ |
810 | const UBool _visible; | |
811 | ||
812 | /** | |
813 | * The locale supported by this factory, as a UnicodeString. | |
374ca955 | 814 | * @stable ICU 2.6 |
b75a7d8f A |
815 | */ |
816 | UnicodeString _id; | |
817 | ||
818 | public: | |
819 | /** | |
374ca955 | 820 | * @stable ICU 2.6 |
b75a7d8f | 821 | */ |
374ca955 | 822 | SimpleNumberFormatFactory(const Locale& locale, UBool visible = TRUE); |
b75a7d8f A |
823 | |
824 | /** | |
73c04bcf | 825 | * @stable ICU 3.0 |
b75a7d8f | 826 | */ |
374ca955 | 827 | virtual ~SimpleNumberFormatFactory(); |
b75a7d8f A |
828 | |
829 | /** | |
374ca955 | 830 | * @stable ICU 2.6 |
b75a7d8f | 831 | */ |
374ca955 | 832 | virtual UBool visible(void) const; |
b75a7d8f | 833 | |
374ca955 A |
834 | /** |
835 | * @stable ICU 2.6 | |
836 | */ | |
837 | virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const; | |
838 | }; | |
839 | #endif /* #if !UCONFIG_NO_SERVICE */ | |
b75a7d8f A |
840 | |
841 | // ------------------------------------- | |
842 | ||
b75a7d8f A |
843 | inline UBool |
844 | NumberFormat::isParseIntegerOnly() const | |
845 | { | |
846 | return fParseIntegerOnly; | |
847 | } | |
848 | ||
849 | inline UnicodeString& | |
850 | NumberFormat::format(const Formattable& obj, | |
851 | UnicodeString& appendTo, | |
852 | UErrorCode& status) const { | |
853 | return Format::format(obj, appendTo, status); | |
854 | } | |
855 | ||
856 | U_NAMESPACE_END | |
857 | ||
858 | #endif /* #if !UCONFIG_NO_FORMATTING */ | |
859 | ||
860 | #endif // _NUMFMT | |
861 | //eof |