]>
Commit | Line | Data |
---|---|---|
b75a7d8f | 1 | /* |
73c04bcf | 2 | ******************************************************************************** |
46f4442e | 3 | * Copyright (C) 1997-2009, International Business Machines Corporation and others. |
73c04bcf | 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); | |
46f4442e A |
425 | |
426 | /** | |
427 | * Return whether or not strict parsing is in effect. | |
428 | * | |
429 | * @return <code>TRUE</code> if strict parsing is in effect, | |
430 | * <code>FALSE</code> otherwise. | |
431 | * @internal | |
432 | */ | |
433 | UBool isParseStrict(void) const; | |
434 | ||
435 | /** | |
436 | * Set whether or not strict parsing should be used. | |
437 | * | |
438 | * @param value <code>TRUE</code> if strict parsing should be used, | |
439 | * <code>FALSE</code> otherwise. | |
440 | * @internal | |
441 | */ | |
442 | virtual void setParseStrict(UBool value); | |
443 | ||
b75a7d8f A |
444 | /** |
445 | * Returns the default number format for the current default | |
446 | * locale. The default format is one of the styles provided by | |
447 | * the other factory methods: getNumberInstance, | |
448 | * getCurrencyInstance or getPercentInstance. Exactly which one | |
449 | * is locale dependant. | |
450 | * @stable ICU 2.0 | |
451 | */ | |
374ca955 | 452 | static NumberFormat* U_EXPORT2 createInstance(UErrorCode&); |
b75a7d8f A |
453 | |
454 | /** | |
455 | * Returns the default number format for the specified locale. | |
456 | * The default format is one of the styles provided by the other | |
457 | * factory methods: getNumberInstance, getCurrencyInstance or | |
458 | * getPercentInstance. Exactly which one is locale dependant. | |
459 | * @param inLocale the given locale. | |
460 | * @stable ICU 2.0 | |
461 | */ | |
374ca955 | 462 | static NumberFormat* U_EXPORT2 createInstance(const Locale& inLocale, |
b75a7d8f A |
463 | UErrorCode&); |
464 | ||
465 | /** | |
466 | * Returns a currency format for the current default locale. | |
467 | * @stable ICU 2.0 | |
468 | */ | |
374ca955 | 469 | static NumberFormat* U_EXPORT2 createCurrencyInstance(UErrorCode&); |
b75a7d8f A |
470 | |
471 | /** | |
472 | * Returns a currency format for the specified locale. | |
473 | * @param inLocale the given locale. | |
474 | * @stable ICU 2.0 | |
475 | */ | |
374ca955 | 476 | static NumberFormat* U_EXPORT2 createCurrencyInstance(const Locale& inLocale, |
b75a7d8f A |
477 | UErrorCode&); |
478 | ||
479 | /** | |
480 | * Returns a percentage format for the current default locale. | |
481 | * @stable ICU 2.0 | |
482 | */ | |
374ca955 | 483 | static NumberFormat* U_EXPORT2 createPercentInstance(UErrorCode&); |
b75a7d8f A |
484 | |
485 | /** | |
486 | * Returns a percentage format for the specified locale. | |
487 | * @param inLocale the given locale. | |
488 | * @stable ICU 2.0 | |
489 | */ | |
374ca955 | 490 | static NumberFormat* U_EXPORT2 createPercentInstance(const Locale& inLocale, |
b75a7d8f A |
491 | UErrorCode&); |
492 | ||
493 | /** | |
494 | * Returns a scientific format for the current default locale. | |
495 | * @stable ICU 2.0 | |
496 | */ | |
374ca955 | 497 | static NumberFormat* U_EXPORT2 createScientificInstance(UErrorCode&); |
b75a7d8f A |
498 | |
499 | /** | |
500 | * Returns a scientific format for the specified locale. | |
501 | * @param inLocale the given locale. | |
502 | * @stable ICU 2.0 | |
503 | */ | |
374ca955 | 504 | static NumberFormat* U_EXPORT2 createScientificInstance(const Locale& inLocale, |
b75a7d8f A |
505 | UErrorCode&); |
506 | ||
507 | /** | |
508 | * Get the set of Locales for which NumberFormats are installed. | |
509 | * @param count Output param to receive the size of the locales | |
510 | * @stable ICU 2.0 | |
511 | */ | |
374ca955 | 512 | static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count); |
b75a7d8f | 513 | |
374ca955 | 514 | #if !UCONFIG_NO_SERVICE |
b75a7d8f A |
515 | /** |
516 | * Register a new NumberFormatFactory. The factory will be adopted. | |
517 | * @param toAdopt the NumberFormatFactory instance to be adopted | |
518 | * @param status the in/out status code, no special meanings are assigned | |
519 | * @return a registry key that can be used to unregister this factory | |
374ca955 | 520 | * @stable ICU 2.6 |
b75a7d8f | 521 | */ |
374ca955 | 522 | static URegistryKey U_EXPORT2 registerFactory(NumberFormatFactory* toAdopt, UErrorCode& status); |
b75a7d8f A |
523 | |
524 | /** | |
525 | * Unregister a previously-registered NumberFormatFactory using the key returned from the | |
526 | * register call. Key becomes invalid after a successful call and should not be used again. | |
527 | * The NumberFormatFactory corresponding to the key will be deleted. | |
528 | * @param key the registry key returned by a previous call to registerFactory | |
529 | * @param status the in/out status code, no special meanings are assigned | |
530 | * @return TRUE if the factory for the key was successfully unregistered | |
374ca955 | 531 | * @stable ICU 2.6 |
b75a7d8f | 532 | */ |
374ca955 | 533 | static UBool U_EXPORT2 unregister(URegistryKey key, UErrorCode& status); |
b75a7d8f A |
534 | |
535 | /** | |
374ca955 | 536 | * Return a StringEnumeration over the locales available at the time of the call, |
b75a7d8f A |
537 | * including registered locales. |
538 | * @return a StringEnumeration over the locales available at the time of the call | |
374ca955 | 539 | * @stable ICU 2.6 |
b75a7d8f | 540 | */ |
374ca955 A |
541 | static StringEnumeration* U_EXPORT2 getAvailableLocales(void); |
542 | #endif /* UCONFIG_NO_SERVICE */ | |
b75a7d8f A |
543 | |
544 | /** | |
545 | * Returns true if grouping is used in this format. For example, | |
546 | * in the English locale, with grouping on, the number 1234567 | |
547 | * might be formatted as "1,234,567". The grouping separator as | |
548 | * well as the size of each group is locale dependant and is | |
549 | * determined by sub-classes of NumberFormat. | |
550 | * @see setGroupingUsed | |
551 | * @stable ICU 2.0 | |
552 | */ | |
553 | UBool isGroupingUsed(void) const; | |
554 | ||
555 | /** | |
556 | * Set whether or not grouping will be used in this format. | |
374ca955 | 557 | * @param newValue True, grouping will be used in this format. |
b75a7d8f A |
558 | * @see getGroupingUsed |
559 | * @stable ICU 2.0 | |
560 | */ | |
561 | virtual void setGroupingUsed(UBool newValue); | |
562 | ||
563 | /** | |
564 | * Returns the maximum number of digits allowed in the integer portion of a | |
565 | * number. | |
566 | * @return the maximum number of digits allowed in the integer portion of a | |
567 | * number. | |
568 | * @see setMaximumIntegerDigits | |
569 | * @stable ICU 2.0 | |
570 | */ | |
571 | int32_t getMaximumIntegerDigits(void) const; | |
572 | ||
573 | /** | |
574 | * Sets the maximum number of digits allowed in the integer portion of a | |
575 | * number. maximumIntegerDigits must be >= minimumIntegerDigits. If the | |
576 | * new value for maximumIntegerDigits is less than the current value | |
577 | * of minimumIntegerDigits, then minimumIntegerDigits will also be set to | |
578 | * the new value. | |
579 | * | |
374ca955 | 580 | * @param newValue the new value for the maximum number of digits |
b75a7d8f A |
581 | * allowed in the integer portion of a number. |
582 | * @see getMaximumIntegerDigits | |
583 | * @stable ICU 2.0 | |
584 | */ | |
585 | virtual void setMaximumIntegerDigits(int32_t newValue); | |
586 | ||
587 | /** | |
588 | * Returns the minimum number of digits allowed in the integer portion of a | |
589 | * number. | |
590 | * @return the minimum number of digits allowed in the integer portion of a | |
591 | * number. | |
592 | * @see setMinimumIntegerDigits | |
593 | * @stable ICU 2.0 | |
594 | */ | |
595 | int32_t getMinimumIntegerDigits(void) const; | |
596 | ||
597 | /** | |
598 | * Sets the minimum number of digits allowed in the integer portion of a | |
599 | * number. minimumIntegerDigits must be <= maximumIntegerDigits. If the | |
600 | * new value for minimumIntegerDigits exceeds the current value | |
601 | * of maximumIntegerDigits, then maximumIntegerDigits will also be set to | |
602 | * the new value. | |
603 | * @param newValue the new value to be set. | |
604 | * @see getMinimumIntegerDigits | |
605 | * @stable ICU 2.0 | |
606 | */ | |
607 | virtual void setMinimumIntegerDigits(int32_t newValue); | |
608 | ||
609 | /** | |
610 | * Returns the maximum number of digits allowed in the fraction portion of a | |
611 | * number. | |
612 | * @return the maximum number of digits allowed in the fraction portion of a | |
613 | * number. | |
614 | * @see setMaximumFractionDigits | |
615 | * @stable ICU 2.0 | |
616 | */ | |
617 | int32_t getMaximumFractionDigits(void) const; | |
618 | ||
619 | /** | |
620 | * Sets the maximum number of digits allowed in the fraction portion of a | |
621 | * number. maximumFractionDigits must be >= minimumFractionDigits. If the | |
622 | * new value for maximumFractionDigits is less than the current value | |
623 | * of minimumFractionDigits, then minimumFractionDigits will also be set to | |
624 | * the new value. | |
625 | * @param newValue the new value to be set. | |
626 | * @see getMaximumFractionDigits | |
627 | * @stable ICU 2.0 | |
628 | */ | |
629 | virtual void setMaximumFractionDigits(int32_t newValue); | |
630 | ||
631 | /** | |
632 | * Returns the minimum number of digits allowed in the fraction portion of a | |
633 | * number. | |
634 | * @return the minimum number of digits allowed in the fraction portion of a | |
635 | * number. | |
636 | * @see setMinimumFractionDigits | |
637 | * @stable ICU 2.0 | |
638 | */ | |
639 | int32_t getMinimumFractionDigits(void) const; | |
640 | ||
641 | /** | |
642 | * Sets the minimum number of digits allowed in the fraction portion of a | |
643 | * number. minimumFractionDigits must be <= maximumFractionDigits. If the | |
644 | * new value for minimumFractionDigits exceeds the current value | |
645 | * of maximumFractionDigits, then maximumIntegerDigits will also be set to | |
646 | * the new value | |
647 | * @param newValue the new value to be set. | |
648 | * @see getMinimumFractionDigits | |
649 | * @stable ICU 2.0 | |
650 | */ | |
651 | virtual void setMinimumFractionDigits(int32_t newValue); | |
652 | ||
653 | /** | |
654 | * Sets the currency used to display currency | |
655 | * amounts. This takes effect immediately, if this format is a | |
656 | * currency format. If this format is not a currency format, then | |
657 | * the currency is used if and when this object becomes a | |
658 | * currency format. | |
659 | * @param theCurrency a 3-letter ISO code indicating new currency | |
660 | * to use. It need not be null-terminated. May be the empty | |
661 | * string or NULL to indicate no currency. | |
374ca955 | 662 | * @param ec input-output error code |
73c04bcf | 663 | * @stable ICU 3.0 |
b75a7d8f | 664 | */ |
374ca955 | 665 | virtual void setCurrency(const UChar* theCurrency, UErrorCode& ec); |
b75a7d8f A |
666 | |
667 | /** | |
668 | * Gets the currency used to display currency | |
669 | * amounts. This may be an empty string for some subclasses. | |
670 | * @return a 3-letter null-terminated ISO code indicating | |
671 | * the currency in use, or a pointer to the empty string. | |
374ca955 | 672 | * @stable ICU 2.6 |
b75a7d8f A |
673 | */ |
674 | const UChar* getCurrency() const; | |
675 | ||
676 | public: | |
677 | ||
678 | /** | |
374ca955 A |
679 | * Return the class ID for this class. This is useful for |
680 | * comparing to a return value from getDynamicClassID(). Note that, | |
681 | * because NumberFormat is an abstract base class, no fully constructed object | |
682 | * will have the class ID returned by NumberFormat::getStaticClassID(). | |
b75a7d8f A |
683 | * @return The class ID for all objects of this class. |
684 | * @stable ICU 2.0 | |
685 | */ | |
374ca955 | 686 | static UClassID U_EXPORT2 getStaticClassID(void); |
b75a7d8f A |
687 | |
688 | /** | |
689 | * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. | |
690 | * This method is to implement a simple version of RTTI, since not all | |
691 | * C++ compilers support genuine RTTI. Polymorphic operator==() and | |
692 | * clone() methods call this method. | |
693 | * <P> | |
694 | * @return The class ID for this object. All objects of a | |
695 | * given class have the same class ID. Objects of | |
696 | * other classes have different class IDs. | |
697 | * @stable ICU 2.0 | |
698 | */ | |
699 | virtual UClassID getDynamicClassID(void) const = 0; | |
700 | ||
701 | protected: | |
702 | ||
703 | /** | |
704 | * Default constructor for subclass use only. | |
705 | * @stable ICU 2.0 | |
706 | */ | |
707 | NumberFormat(); | |
708 | ||
709 | /** | |
710 | * Copy constructor. | |
711 | * @stable ICU 2.0 | |
712 | */ | |
713 | NumberFormat(const NumberFormat&); | |
714 | ||
715 | /** | |
716 | * Assignment operator. | |
717 | * @stable ICU 2.0 | |
718 | */ | |
719 | NumberFormat& operator=(const NumberFormat&); | |
720 | ||
374ca955 A |
721 | /** |
722 | * Returns the currency in effect for this formatter. Subclasses | |
723 | * should override this method as needed. Unlike getCurrency(), | |
724 | * this method should never return "". | |
725 | * @result output parameter for null-terminated result, which must | |
726 | * have a capacity of at least 4 | |
727 | * @internal | |
728 | */ | |
729 | virtual void getEffectiveCurrency(UChar* result, UErrorCode& ec) const; | |
730 | ||
b75a7d8f | 731 | private: |
b75a7d8f A |
732 | |
733 | enum EStyles { | |
734 | kNumberStyle, | |
735 | kCurrencyStyle, | |
736 | kPercentStyle, | |
737 | kScientificStyle, | |
738 | kStyleCount // ALWAYS LAST ENUM: number of styles | |
739 | }; | |
374ca955 | 740 | |
b75a7d8f A |
741 | /** |
742 | * Creates the specified decimal format style of the desired locale. | |
374ca955 A |
743 | * Hook for service registration, uses makeInstance directly if no services |
744 | * registered. | |
b75a7d8f A |
745 | * @param desiredLocale the given locale. |
746 | * @param choice the given style. | |
747 | * @param success Output param filled with success/failure status. | |
748 | * @return A new NumberFormat instance. | |
749 | */ | |
374ca955 | 750 | static NumberFormat* U_EXPORT2 createInstance(const Locale& desiredLocale, EStyles choice, UErrorCode& success); |
b75a7d8f A |
751 | |
752 | /** | |
753 | * Creates the specified decimal format style of the desired locale. | |
754 | * @param desiredLocale the given locale. | |
755 | * @param choice the given style. | |
756 | * @param success Output param filled with success/failure status. | |
757 | * @return A new NumberFormat instance. | |
758 | */ | |
374ca955 | 759 | static NumberFormat* makeInstance(const Locale& desiredLocale, EStyles choice, UErrorCode& success); |
b75a7d8f A |
760 | |
761 | UBool fGroupingUsed; | |
46f4442e A |
762 | int32_t fMaxIntegerDigits; |
763 | int32_t fMinIntegerDigits; | |
764 | int32_t fMaxFractionDigits; | |
765 | int32_t fMinFractionDigits; | |
b75a7d8f | 766 | UBool fParseIntegerOnly; |
46f4442e | 767 | UBool fParseStrict; |
b75a7d8f A |
768 | |
769 | // ISO currency code | |
374ca955 | 770 | UChar fCurrency[4]; |
b75a7d8f | 771 | |
374ca955 A |
772 | friend class ICUNumberFormatFactory; // access to makeInstance, EStyles |
773 | friend class ICUNumberFormatService; | |
b75a7d8f A |
774 | }; |
775 | ||
374ca955 | 776 | #if !UCONFIG_NO_SERVICE |
b75a7d8f A |
777 | /** |
778 | * A NumberFormatFactory is used to register new number formats. The factory | |
779 | * should be able to create any of the predefined formats for each locale it | |
780 | * supports. When registered, the locales it supports extend or override the | |
781 | * locale already supported by ICU. | |
782 | * | |
374ca955 | 783 | * @stable ICU 2.6 |
b75a7d8f A |
784 | */ |
785 | class U_I18N_API NumberFormatFactory : public UObject { | |
786 | public: | |
787 | ||
374ca955 A |
788 | /** |
789 | * Destructor | |
73c04bcf | 790 | * @stable ICU 3.0 |
374ca955 A |
791 | */ |
792 | virtual ~NumberFormatFactory(); | |
793 | ||
b75a7d8f A |
794 | /** |
795 | * Return true if this factory will be visible. Default is true. | |
796 | * If not visible, the locales supported by this factory will not | |
797 | * be listed by getAvailableLocales. | |
374ca955 | 798 | * @stable ICU 2.6 |
b75a7d8f A |
799 | */ |
800 | virtual UBool visible(void) const = 0; | |
801 | ||
802 | /** | |
803 | * Return the locale names directly supported by this factory. The number of names | |
804 | * is returned in count; | |
374ca955 | 805 | * @stable ICU 2.6 |
b75a7d8f | 806 | */ |
374ca955 | 807 | virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const = 0; |
b75a7d8f A |
808 | |
809 | /** | |
810 | * Return a number format of the appropriate type. If the locale | |
811 | * is not supported, return null. If the locale is supported, but | |
812 | * the type is not provided by this service, return null. Otherwise | |
813 | * return an appropriate instance of NumberFormat. | |
374ca955 | 814 | * @stable ICU 2.6 |
b75a7d8f A |
815 | */ |
816 | virtual NumberFormat* createFormat(const Locale& loc, UNumberFormatStyle formatType) = 0; | |
817 | }; | |
818 | ||
374ca955 A |
819 | /** |
820 | * A NumberFormatFactory that supports a single locale. It can be visible or invisible. | |
73c04bcf | 821 | * @stable ICU 2.6 |
374ca955 | 822 | */ |
b75a7d8f A |
823 | class U_I18N_API SimpleNumberFormatFactory : public NumberFormatFactory { |
824 | protected: | |
825 | /** | |
826 | * True if the locale supported by this factory is visible. | |
374ca955 | 827 | * @stable ICU 2.6 |
b75a7d8f A |
828 | */ |
829 | const UBool _visible; | |
830 | ||
831 | /** | |
832 | * The locale supported by this factory, as a UnicodeString. | |
374ca955 | 833 | * @stable ICU 2.6 |
b75a7d8f A |
834 | */ |
835 | UnicodeString _id; | |
836 | ||
837 | public: | |
838 | /** | |
374ca955 | 839 | * @stable ICU 2.6 |
b75a7d8f | 840 | */ |
374ca955 | 841 | SimpleNumberFormatFactory(const Locale& locale, UBool visible = TRUE); |
b75a7d8f A |
842 | |
843 | /** | |
73c04bcf | 844 | * @stable ICU 3.0 |
b75a7d8f | 845 | */ |
374ca955 | 846 | virtual ~SimpleNumberFormatFactory(); |
b75a7d8f A |
847 | |
848 | /** | |
374ca955 | 849 | * @stable ICU 2.6 |
b75a7d8f | 850 | */ |
374ca955 | 851 | virtual UBool visible(void) const; |
b75a7d8f | 852 | |
374ca955 A |
853 | /** |
854 | * @stable ICU 2.6 | |
855 | */ | |
856 | virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const; | |
857 | }; | |
858 | #endif /* #if !UCONFIG_NO_SERVICE */ | |
b75a7d8f A |
859 | |
860 | // ------------------------------------- | |
861 | ||
b75a7d8f A |
862 | inline UBool |
863 | NumberFormat::isParseIntegerOnly() const | |
864 | { | |
865 | return fParseIntegerOnly; | |
866 | } | |
867 | ||
46f4442e A |
868 | inline UBool |
869 | NumberFormat::isParseStrict() const | |
870 | { | |
871 | return fParseStrict; | |
872 | } | |
873 | ||
b75a7d8f A |
874 | inline UnicodeString& |
875 | NumberFormat::format(const Formattable& obj, | |
876 | UnicodeString& appendTo, | |
877 | UErrorCode& status) const { | |
878 | return Format::format(obj, appendTo, status); | |
879 | } | |
880 | ||
881 | U_NAMESPACE_END | |
882 | ||
883 | #endif /* #if !UCONFIG_NO_FORMATTING */ | |
884 | ||
885 | #endif // _NUMFMT | |
886 | //eof |