Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | ******************************************************************************** |
michael@0 | 3 | * Copyright (C) 1997-2013, International Business Machines Corporation and others. |
michael@0 | 4 | * All Rights Reserved. |
michael@0 | 5 | ******************************************************************************** |
michael@0 | 6 | * |
michael@0 | 7 | * File NUMFMT.H |
michael@0 | 8 | * |
michael@0 | 9 | * Modification History: |
michael@0 | 10 | * |
michael@0 | 11 | * Date Name Description |
michael@0 | 12 | * 02/19/97 aliu Converted from java. |
michael@0 | 13 | * 03/18/97 clhuang Updated per C++ implementation. |
michael@0 | 14 | * 04/17/97 aliu Changed DigitCount to int per code review. |
michael@0 | 15 | * 07/20/98 stephen JDK 1.2 sync up. Added scientific support. |
michael@0 | 16 | * Changed naming conventions to match C++ guidelines |
michael@0 | 17 | * Derecated Java style constants (eg, INTEGER_FIELD) |
michael@0 | 18 | ******************************************************************************** |
michael@0 | 19 | */ |
michael@0 | 20 | |
michael@0 | 21 | #ifndef NUMFMT_H |
michael@0 | 22 | #define NUMFMT_H |
michael@0 | 23 | |
michael@0 | 24 | |
michael@0 | 25 | #include "unicode/utypes.h" |
michael@0 | 26 | |
michael@0 | 27 | /** |
michael@0 | 28 | * \file |
michael@0 | 29 | * \brief C++ API: Abstract base class for all number formats. |
michael@0 | 30 | */ |
michael@0 | 31 | |
michael@0 | 32 | #if !UCONFIG_NO_FORMATTING |
michael@0 | 33 | |
michael@0 | 34 | #include "unicode/unistr.h" |
michael@0 | 35 | #include "unicode/format.h" |
michael@0 | 36 | #include "unicode/unum.h" // UNumberFormatStyle |
michael@0 | 37 | #include "unicode/locid.h" |
michael@0 | 38 | #include "unicode/stringpiece.h" |
michael@0 | 39 | #include "unicode/curramt.h" |
michael@0 | 40 | |
michael@0 | 41 | class NumberFormatTest; |
michael@0 | 42 | |
michael@0 | 43 | U_NAMESPACE_BEGIN |
michael@0 | 44 | |
michael@0 | 45 | #if !UCONFIG_NO_SERVICE |
michael@0 | 46 | class NumberFormatFactory; |
michael@0 | 47 | class StringEnumeration; |
michael@0 | 48 | #endif |
michael@0 | 49 | |
michael@0 | 50 | /** |
michael@0 | 51 | * |
michael@0 | 52 | * Abstract base class for all number formats. Provides interface for |
michael@0 | 53 | * formatting and parsing a number. Also provides methods for |
michael@0 | 54 | * determining which locales have number formats, and what their names |
michael@0 | 55 | * are. |
michael@0 | 56 | * <P> |
michael@0 | 57 | * NumberFormat helps you to format and parse numbers for any locale. |
michael@0 | 58 | * Your code can be completely independent of the locale conventions |
michael@0 | 59 | * for decimal points, thousands-separators, or even the particular |
michael@0 | 60 | * decimal digits used, or whether the number format is even decimal. |
michael@0 | 61 | * <P> |
michael@0 | 62 | * To format a number for the current Locale, use one of the static |
michael@0 | 63 | * factory methods: |
michael@0 | 64 | * <pre> |
michael@0 | 65 | * \code |
michael@0 | 66 | * double myNumber = 7.0; |
michael@0 | 67 | * UnicodeString myString; |
michael@0 | 68 | * UErrorCode success = U_ZERO_ERROR; |
michael@0 | 69 | * NumberFormat* nf = NumberFormat::createInstance(success) |
michael@0 | 70 | * nf->format(myNumber, myString); |
michael@0 | 71 | * cout << " Example 1: " << myString << endl; |
michael@0 | 72 | * \endcode |
michael@0 | 73 | * </pre> |
michael@0 | 74 | * Note that there are additional factory methods within subclasses of |
michael@0 | 75 | * NumberFormat. |
michael@0 | 76 | * <P> |
michael@0 | 77 | * If you are formatting multiple numbers, it is more efficient to get |
michael@0 | 78 | * the format and use it multiple times so that the system doesn't |
michael@0 | 79 | * have to fetch the information about the local language and country |
michael@0 | 80 | * conventions multiple times. |
michael@0 | 81 | * <pre> |
michael@0 | 82 | * \code |
michael@0 | 83 | * UnicodeString myString; |
michael@0 | 84 | * UErrorCode success = U_ZERO_ERROR; |
michael@0 | 85 | * nf = NumberFormat::createInstance( success ); |
michael@0 | 86 | * int32_t a[] = { 123, 3333, -1234567 }; |
michael@0 | 87 | * const int32_t a_len = sizeof(a) / sizeof(a[0]); |
michael@0 | 88 | * myString.remove(); |
michael@0 | 89 | * for (int32_t i = 0; i < a_len; i++) { |
michael@0 | 90 | * nf->format(a[i], myString); |
michael@0 | 91 | * myString += " ; "; |
michael@0 | 92 | * } |
michael@0 | 93 | * cout << " Example 2: " << myString << endl; |
michael@0 | 94 | * \endcode |
michael@0 | 95 | * </pre> |
michael@0 | 96 | * To format a number for a different Locale, specify it in the |
michael@0 | 97 | * call to createInstance(). |
michael@0 | 98 | * <pre> |
michael@0 | 99 | * \code |
michael@0 | 100 | * nf = NumberFormat::createInstance( Locale::FRENCH, success ); |
michael@0 | 101 | * \endcode |
michael@0 | 102 | * </pre> |
michael@0 | 103 | * You can use a NumberFormat to parse also. |
michael@0 | 104 | * <pre> |
michael@0 | 105 | * \code |
michael@0 | 106 | * UErrorCode success; |
michael@0 | 107 | * Formattable result(-999); // initialized with error code |
michael@0 | 108 | * nf->parse(myString, result, success); |
michael@0 | 109 | * \endcode |
michael@0 | 110 | * </pre> |
michael@0 | 111 | * Use createInstance to get the normal number format for that country. |
michael@0 | 112 | * There are other static factory methods available. Use getCurrency |
michael@0 | 113 | * to get the currency number format for that country. Use getPercent |
michael@0 | 114 | * to get a format for displaying percentages. With this format, a |
michael@0 | 115 | * fraction from 0.53 is displayed as 53%. |
michael@0 | 116 | * <P> |
michael@0 | 117 | * Starting from ICU 4.2, you can use createInstance() by passing in a 'style' |
michael@0 | 118 | * as parameter to get the correct instance. |
michael@0 | 119 | * For example, |
michael@0 | 120 | * use createInstance(...kNumberStyle...) to get the normal number format, |
michael@0 | 121 | * createInstance(...kPercentStyle...) to get a format for displaying |
michael@0 | 122 | * percentage, |
michael@0 | 123 | * createInstance(...kScientificStyle...) to get a format for displaying |
michael@0 | 124 | * scientific number, |
michael@0 | 125 | * createInstance(...kCurrencyStyle...) to get the currency number format, |
michael@0 | 126 | * in which the currency is represented by its symbol, for example, "$3.00". |
michael@0 | 127 | * createInstance(...kIsoCurrencyStyle...) to get the currency number format, |
michael@0 | 128 | * in which the currency is represented by its ISO code, for example "USD3.00". |
michael@0 | 129 | * createInstance(...kPluralCurrencyStyle...) to get the currency number format, |
michael@0 | 130 | * in which the currency is represented by its full name in plural format, |
michael@0 | 131 | * for example, "3.00 US dollars" or "1.00 US dollar". |
michael@0 | 132 | * <P> |
michael@0 | 133 | * You can also control the display of numbers with such methods as |
michael@0 | 134 | * getMinimumFractionDigits. If you want even more control over the |
michael@0 | 135 | * format or parsing, or want to give your users more control, you can |
michael@0 | 136 | * try casting the NumberFormat you get from the factory methods to a |
michael@0 | 137 | * DecimalNumberFormat. This will work for the vast majority of |
michael@0 | 138 | * countries; just remember to put it in a try block in case you |
michael@0 | 139 | * encounter an unusual one. |
michael@0 | 140 | * <P> |
michael@0 | 141 | * You can also use forms of the parse and format methods with |
michael@0 | 142 | * ParsePosition and FieldPosition to allow you to: |
michael@0 | 143 | * <ul type=round> |
michael@0 | 144 | * <li>(a) progressively parse through pieces of a string. |
michael@0 | 145 | * <li>(b) align the decimal point and other areas. |
michael@0 | 146 | * </ul> |
michael@0 | 147 | * For example, you can align numbers in two ways. |
michael@0 | 148 | * <P> |
michael@0 | 149 | * If you are using a monospaced font with spacing for alignment, you |
michael@0 | 150 | * can pass the FieldPosition in your format call, with field = |
michael@0 | 151 | * INTEGER_FIELD. On output, getEndIndex will be set to the offset |
michael@0 | 152 | * between the last character of the integer and the decimal. Add |
michael@0 | 153 | * (desiredSpaceCount - getEndIndex) spaces at the front of the |
michael@0 | 154 | * string. |
michael@0 | 155 | * <P> |
michael@0 | 156 | * If you are using proportional fonts, instead of padding with |
michael@0 | 157 | * spaces, measure the width of the string in pixels from the start to |
michael@0 | 158 | * getEndIndex. Then move the pen by (desiredPixelWidth - |
michael@0 | 159 | * widthToAlignmentPoint) before drawing the text. It also works |
michael@0 | 160 | * where there is no decimal, but possibly additional characters at |
michael@0 | 161 | * the end, e.g. with parentheses in negative numbers: "(12)" for -12. |
michael@0 | 162 | * <p> |
michael@0 | 163 | * <em>User subclasses are not supported.</em> While clients may write |
michael@0 | 164 | * subclasses, such code will not necessarily work and will not be |
michael@0 | 165 | * guaranteed to work stably from release to release. |
michael@0 | 166 | * |
michael@0 | 167 | * @stable ICU 2.0 |
michael@0 | 168 | */ |
michael@0 | 169 | class U_I18N_API NumberFormat : public Format { |
michael@0 | 170 | public: |
michael@0 | 171 | /** |
michael@0 | 172 | * Alignment Field constants used to construct a FieldPosition object. |
michael@0 | 173 | * Signifies that the position of the integer part or fraction part of |
michael@0 | 174 | * a formatted number should be returned. |
michael@0 | 175 | * |
michael@0 | 176 | * Note: as of ICU 4.4, the values in this enum have been extended to |
michael@0 | 177 | * support identification of all number format fields, not just those |
michael@0 | 178 | * pertaining to alignment. |
michael@0 | 179 | * |
michael@0 | 180 | * These constants are provided for backwards compatibility only. |
michael@0 | 181 | * Please use the C style constants defined in the header file unum.h. |
michael@0 | 182 | * |
michael@0 | 183 | * @see FieldPosition |
michael@0 | 184 | * @stable ICU 2.0 |
michael@0 | 185 | */ |
michael@0 | 186 | enum EAlignmentFields { |
michael@0 | 187 | /** @stable ICU 2.0 */ |
michael@0 | 188 | kIntegerField = UNUM_INTEGER_FIELD, |
michael@0 | 189 | /** @stable ICU 2.0 */ |
michael@0 | 190 | kFractionField = UNUM_FRACTION_FIELD, |
michael@0 | 191 | /** @stable ICU 2.0 */ |
michael@0 | 192 | kDecimalSeparatorField = UNUM_DECIMAL_SEPARATOR_FIELD, |
michael@0 | 193 | /** @stable ICU 2.0 */ |
michael@0 | 194 | kExponentSymbolField = UNUM_EXPONENT_SYMBOL_FIELD, |
michael@0 | 195 | /** @stable ICU 2.0 */ |
michael@0 | 196 | kExponentSignField = UNUM_EXPONENT_SIGN_FIELD, |
michael@0 | 197 | /** @stable ICU 2.0 */ |
michael@0 | 198 | kExponentField = UNUM_EXPONENT_FIELD, |
michael@0 | 199 | /** @stable ICU 2.0 */ |
michael@0 | 200 | kGroupingSeparatorField = UNUM_GROUPING_SEPARATOR_FIELD, |
michael@0 | 201 | /** @stable ICU 2.0 */ |
michael@0 | 202 | kCurrencyField = UNUM_CURRENCY_FIELD, |
michael@0 | 203 | /** @stable ICU 2.0 */ |
michael@0 | 204 | kPercentField = UNUM_PERCENT_FIELD, |
michael@0 | 205 | /** @stable ICU 2.0 */ |
michael@0 | 206 | kPermillField = UNUM_PERMILL_FIELD, |
michael@0 | 207 | /** @stable ICU 2.0 */ |
michael@0 | 208 | kSignField = UNUM_SIGN_FIELD, |
michael@0 | 209 | |
michael@0 | 210 | /** |
michael@0 | 211 | * These constants are provided for backwards compatibility only. |
michael@0 | 212 | * Please use the constants defined in the header file unum.h. |
michael@0 | 213 | */ |
michael@0 | 214 | /** @stable ICU 2.0 */ |
michael@0 | 215 | INTEGER_FIELD = UNUM_INTEGER_FIELD, |
michael@0 | 216 | /** @stable ICU 2.0 */ |
michael@0 | 217 | FRACTION_FIELD = UNUM_FRACTION_FIELD |
michael@0 | 218 | }; |
michael@0 | 219 | |
michael@0 | 220 | /** |
michael@0 | 221 | * Destructor. |
michael@0 | 222 | * @stable ICU 2.0 |
michael@0 | 223 | */ |
michael@0 | 224 | virtual ~NumberFormat(); |
michael@0 | 225 | |
michael@0 | 226 | /** |
michael@0 | 227 | * Return true if the given Format objects are semantically equal. |
michael@0 | 228 | * Objects of different subclasses are considered unequal. |
michael@0 | 229 | * @return true if the given Format objects are semantically equal. |
michael@0 | 230 | * @stable ICU 2.0 |
michael@0 | 231 | */ |
michael@0 | 232 | virtual UBool operator==(const Format& other) const; |
michael@0 | 233 | |
michael@0 | 234 | |
michael@0 | 235 | using Format::format; |
michael@0 | 236 | |
michael@0 | 237 | /** |
michael@0 | 238 | * Format an object to produce a string. This method handles |
michael@0 | 239 | * Formattable objects with numeric types. If the Formattable |
michael@0 | 240 | * object type is not a numeric type, then it returns a failing |
michael@0 | 241 | * UErrorCode. |
michael@0 | 242 | * |
michael@0 | 243 | * @param obj The object to format. |
michael@0 | 244 | * @param appendTo Output parameter to receive result. |
michael@0 | 245 | * Result is appended to existing contents. |
michael@0 | 246 | * @param pos On input: an alignment field, if desired. |
michael@0 | 247 | * On output: the offsets of the alignment field. |
michael@0 | 248 | * @param status Output param filled with success/failure status. |
michael@0 | 249 | * @return Reference to 'appendTo' parameter. |
michael@0 | 250 | * @stable ICU 2.0 |
michael@0 | 251 | */ |
michael@0 | 252 | virtual UnicodeString& format(const Formattable& obj, |
michael@0 | 253 | UnicodeString& appendTo, |
michael@0 | 254 | FieldPosition& pos, |
michael@0 | 255 | UErrorCode& status) const; |
michael@0 | 256 | |
michael@0 | 257 | /** |
michael@0 | 258 | * Format an object to produce a string. This method handles |
michael@0 | 259 | * Formattable objects with numeric types. If the Formattable |
michael@0 | 260 | * object type is not a numeric type, then it returns a failing |
michael@0 | 261 | * UErrorCode. |
michael@0 | 262 | * |
michael@0 | 263 | * @param obj The object to format. |
michael@0 | 264 | * @param appendTo Output parameter to receive result. |
michael@0 | 265 | * Result is appended to existing contents. |
michael@0 | 266 | * @param posIter On return, can be used to iterate over positions |
michael@0 | 267 | * of fields generated by this format call. Can be |
michael@0 | 268 | * NULL. |
michael@0 | 269 | * @param status Output param filled with success/failure status. |
michael@0 | 270 | * @return Reference to 'appendTo' parameter. |
michael@0 | 271 | * @stable 4.4 |
michael@0 | 272 | */ |
michael@0 | 273 | virtual UnicodeString& format(const Formattable& obj, |
michael@0 | 274 | UnicodeString& appendTo, |
michael@0 | 275 | FieldPositionIterator* posIter, |
michael@0 | 276 | UErrorCode& status) const; |
michael@0 | 277 | |
michael@0 | 278 | /** |
michael@0 | 279 | * Parse a string to produce an object. This methods handles |
michael@0 | 280 | * parsing of numeric strings into Formattable objects with numeric |
michael@0 | 281 | * types. |
michael@0 | 282 | * <P> |
michael@0 | 283 | * Before calling, set parse_pos.index to the offset you want to |
michael@0 | 284 | * start parsing at in the source. After calling, parse_pos.index |
michael@0 | 285 | * indicates the position after the successfully parsed text. If |
michael@0 | 286 | * an error occurs, parse_pos.index is unchanged. |
michael@0 | 287 | * <P> |
michael@0 | 288 | * When parsing, leading whitespace is discarded (with successful |
michael@0 | 289 | * parse), while trailing whitespace is left as is. |
michael@0 | 290 | * <P> |
michael@0 | 291 | * See Format::parseObject() for more. |
michael@0 | 292 | * |
michael@0 | 293 | * @param source The string to be parsed into an object. |
michael@0 | 294 | * @param result Formattable to be set to the parse result. |
michael@0 | 295 | * If parse fails, return contents are undefined. |
michael@0 | 296 | * @param parse_pos The position to start parsing at. Upon return |
michael@0 | 297 | * this param is set to the position after the |
michael@0 | 298 | * last character successfully parsed. If the |
michael@0 | 299 | * source is not parsed successfully, this param |
michael@0 | 300 | * will remain unchanged. |
michael@0 | 301 | * @return A newly created Formattable* object, or NULL |
michael@0 | 302 | * on failure. The caller owns this and should |
michael@0 | 303 | * delete it when done. |
michael@0 | 304 | * @stable ICU 2.0 |
michael@0 | 305 | */ |
michael@0 | 306 | virtual void parseObject(const UnicodeString& source, |
michael@0 | 307 | Formattable& result, |
michael@0 | 308 | ParsePosition& parse_pos) const; |
michael@0 | 309 | |
michael@0 | 310 | /** |
michael@0 | 311 | * Format a double number. These methods call the NumberFormat |
michael@0 | 312 | * pure virtual format() methods with the default FieldPosition. |
michael@0 | 313 | * |
michael@0 | 314 | * @param number The value to be formatted. |
michael@0 | 315 | * @param appendTo Output parameter to receive result. |
michael@0 | 316 | * Result is appended to existing contents. |
michael@0 | 317 | * @return Reference to 'appendTo' parameter. |
michael@0 | 318 | * @stable ICU 2.0 |
michael@0 | 319 | */ |
michael@0 | 320 | UnicodeString& format( double number, |
michael@0 | 321 | UnicodeString& appendTo) const; |
michael@0 | 322 | |
michael@0 | 323 | /** |
michael@0 | 324 | * Format a long number. These methods call the NumberFormat |
michael@0 | 325 | * pure virtual format() methods with the default FieldPosition. |
michael@0 | 326 | * |
michael@0 | 327 | * @param number The value to be formatted. |
michael@0 | 328 | * @param appendTo Output parameter to receive result. |
michael@0 | 329 | * Result is appended to existing contents. |
michael@0 | 330 | * @return Reference to 'appendTo' parameter. |
michael@0 | 331 | * @stable ICU 2.0 |
michael@0 | 332 | */ |
michael@0 | 333 | UnicodeString& format( int32_t number, |
michael@0 | 334 | UnicodeString& appendTo) const; |
michael@0 | 335 | |
michael@0 | 336 | /** |
michael@0 | 337 | * Format an int64 number. These methods call the NumberFormat |
michael@0 | 338 | * pure virtual format() methods with the default FieldPosition. |
michael@0 | 339 | * |
michael@0 | 340 | * @param number The value to be formatted. |
michael@0 | 341 | * @param appendTo Output parameter to receive result. |
michael@0 | 342 | * Result is appended to existing contents. |
michael@0 | 343 | * @return Reference to 'appendTo' parameter. |
michael@0 | 344 | * @stable ICU 2.8 |
michael@0 | 345 | */ |
michael@0 | 346 | UnicodeString& format( int64_t number, |
michael@0 | 347 | UnicodeString& appendTo) const; |
michael@0 | 348 | |
michael@0 | 349 | /** |
michael@0 | 350 | * Format a double number. Concrete subclasses must implement |
michael@0 | 351 | * these pure virtual methods. |
michael@0 | 352 | * |
michael@0 | 353 | * @param number The value to be formatted. |
michael@0 | 354 | * @param appendTo Output parameter to receive result. |
michael@0 | 355 | * Result is appended to existing contents. |
michael@0 | 356 | * @param pos On input: an alignment field, if desired. |
michael@0 | 357 | * On output: the offsets of the alignment field. |
michael@0 | 358 | * @return Reference to 'appendTo' parameter. |
michael@0 | 359 | * @stable ICU 2.0 |
michael@0 | 360 | */ |
michael@0 | 361 | virtual UnicodeString& format(double number, |
michael@0 | 362 | UnicodeString& appendTo, |
michael@0 | 363 | FieldPosition& pos) const = 0; |
michael@0 | 364 | /** |
michael@0 | 365 | * Format a double number. By default, the parent function simply |
michael@0 | 366 | * calls the base class and does not return an error status. |
michael@0 | 367 | * Therefore, the status may be ignored in some subclasses. |
michael@0 | 368 | * |
michael@0 | 369 | * @param number The value to be formatted. |
michael@0 | 370 | * @param appendTo Output parameter to receive result. |
michael@0 | 371 | * Result is appended to existing contents. |
michael@0 | 372 | * @param pos On input: an alignment field, if desired. |
michael@0 | 373 | * On output: the offsets of the alignment field. |
michael@0 | 374 | * @param status error status |
michael@0 | 375 | * @return Reference to 'appendTo' parameter. |
michael@0 | 376 | * @internal |
michael@0 | 377 | */ |
michael@0 | 378 | virtual UnicodeString& format(double number, |
michael@0 | 379 | UnicodeString& appendTo, |
michael@0 | 380 | FieldPosition& pos, |
michael@0 | 381 | UErrorCode &status) const; |
michael@0 | 382 | /** |
michael@0 | 383 | * Format a double number. Subclasses must implement |
michael@0 | 384 | * this method. |
michael@0 | 385 | * |
michael@0 | 386 | * @param number The value to be formatted. |
michael@0 | 387 | * @param appendTo Output parameter to receive result. |
michael@0 | 388 | * Result is appended to existing contents. |
michael@0 | 389 | * @param posIter On return, can be used to iterate over positions |
michael@0 | 390 | * of fields generated by this format call. |
michael@0 | 391 | * Can be NULL. |
michael@0 | 392 | * @param status Output param filled with success/failure status. |
michael@0 | 393 | * @return Reference to 'appendTo' parameter. |
michael@0 | 394 | * @stable 4.4 |
michael@0 | 395 | */ |
michael@0 | 396 | virtual UnicodeString& format(double number, |
michael@0 | 397 | UnicodeString& appendTo, |
michael@0 | 398 | FieldPositionIterator* posIter, |
michael@0 | 399 | UErrorCode& status) const; |
michael@0 | 400 | /** |
michael@0 | 401 | * Format a long number. Concrete subclasses must implement |
michael@0 | 402 | * these pure virtual methods. |
michael@0 | 403 | * |
michael@0 | 404 | * @param number The value to be formatted. |
michael@0 | 405 | * @param appendTo Output parameter to receive result. |
michael@0 | 406 | * Result is appended to existing contents. |
michael@0 | 407 | * @param pos On input: an alignment field, if desired. |
michael@0 | 408 | * On output: the offsets of the alignment field. |
michael@0 | 409 | * @return Reference to 'appendTo' parameter. |
michael@0 | 410 | * @stable ICU 2.0 |
michael@0 | 411 | */ |
michael@0 | 412 | virtual UnicodeString& format(int32_t number, |
michael@0 | 413 | UnicodeString& appendTo, |
michael@0 | 414 | FieldPosition& pos) const = 0; |
michael@0 | 415 | |
michael@0 | 416 | /** |
michael@0 | 417 | * Format a long number. Concrete subclasses may override |
michael@0 | 418 | * this function to provide status return. |
michael@0 | 419 | * |
michael@0 | 420 | * @param number The value to be formatted. |
michael@0 | 421 | * @param appendTo Output parameter to receive result. |
michael@0 | 422 | * Result is appended to existing contents. |
michael@0 | 423 | * @param pos On input: an alignment field, if desired. |
michael@0 | 424 | * On output: the offsets of the alignment field. |
michael@0 | 425 | * @param status the output status. |
michael@0 | 426 | * @return Reference to 'appendTo' parameter. |
michael@0 | 427 | * @internal |
michael@0 | 428 | */ |
michael@0 | 429 | virtual UnicodeString& format(int32_t number, |
michael@0 | 430 | UnicodeString& appendTo, |
michael@0 | 431 | FieldPosition& pos, |
michael@0 | 432 | UErrorCode &status) const; |
michael@0 | 433 | |
michael@0 | 434 | /** |
michael@0 | 435 | * Format an int32 number. Subclasses must implement |
michael@0 | 436 | * this method. |
michael@0 | 437 | * |
michael@0 | 438 | * @param number The value to be formatted. |
michael@0 | 439 | * @param appendTo Output parameter to receive result. |
michael@0 | 440 | * Result is appended to existing contents. |
michael@0 | 441 | * @param posIter On return, can be used to iterate over positions |
michael@0 | 442 | * of fields generated by this format call. |
michael@0 | 443 | * Can be NULL. |
michael@0 | 444 | * @param status Output param filled with success/failure status. |
michael@0 | 445 | * @return Reference to 'appendTo' parameter. |
michael@0 | 446 | * @stable 4.4 |
michael@0 | 447 | */ |
michael@0 | 448 | virtual UnicodeString& format(int32_t number, |
michael@0 | 449 | UnicodeString& appendTo, |
michael@0 | 450 | FieldPositionIterator* posIter, |
michael@0 | 451 | UErrorCode& status) const; |
michael@0 | 452 | /** |
michael@0 | 453 | * Format an int64 number. (Not abstract to retain compatibility |
michael@0 | 454 | * with earlier releases, however subclasses should override this |
michael@0 | 455 | * method as it just delegates to format(int32_t number...); |
michael@0 | 456 | * |
michael@0 | 457 | * @param number The value to be formatted. |
michael@0 | 458 | * @param appendTo Output parameter to receive result. |
michael@0 | 459 | * Result is appended to existing contents. |
michael@0 | 460 | * @param pos On input: an alignment field, if desired. |
michael@0 | 461 | * On output: the offsets of the alignment field. |
michael@0 | 462 | * @return Reference to 'appendTo' parameter. |
michael@0 | 463 | * @stable ICU 2.8 |
michael@0 | 464 | */ |
michael@0 | 465 | virtual UnicodeString& format(int64_t number, |
michael@0 | 466 | UnicodeString& appendTo, |
michael@0 | 467 | FieldPosition& pos) const; |
michael@0 | 468 | |
michael@0 | 469 | /** |
michael@0 | 470 | * Format an int64 number. (Not abstract to retain compatibility |
michael@0 | 471 | * with earlier releases, however subclasses should override this |
michael@0 | 472 | * method as it just delegates to format(int32_t number...); |
michael@0 | 473 | * |
michael@0 | 474 | * @param number The value to be formatted. |
michael@0 | 475 | * @param appendTo Output parameter to receive result. |
michael@0 | 476 | * Result is appended to existing contents. |
michael@0 | 477 | * @param pos On input: an alignment field, if desired. |
michael@0 | 478 | * On output: the offsets of the alignment field. |
michael@0 | 479 | * @return Reference to 'appendTo' parameter. |
michael@0 | 480 | * @internal |
michael@0 | 481 | */ |
michael@0 | 482 | virtual UnicodeString& format(int64_t number, |
michael@0 | 483 | UnicodeString& appendTo, |
michael@0 | 484 | FieldPosition& pos, |
michael@0 | 485 | UErrorCode& status) const; |
michael@0 | 486 | /** |
michael@0 | 487 | * Format an int64 number. Subclasses must implement |
michael@0 | 488 | * this method. |
michael@0 | 489 | * |
michael@0 | 490 | * @param number The value to be formatted. |
michael@0 | 491 | * @param appendTo Output parameter to receive result. |
michael@0 | 492 | * Result is appended to existing contents. |
michael@0 | 493 | * @param posIter On return, can be used to iterate over positions |
michael@0 | 494 | * of fields generated by this format call. |
michael@0 | 495 | * Can be NULL. |
michael@0 | 496 | * @param status Output param filled with success/failure status. |
michael@0 | 497 | * @return Reference to 'appendTo' parameter. |
michael@0 | 498 | * @stable 4.4 |
michael@0 | 499 | */ |
michael@0 | 500 | virtual UnicodeString& format(int64_t number, |
michael@0 | 501 | UnicodeString& appendTo, |
michael@0 | 502 | FieldPositionIterator* posIter, |
michael@0 | 503 | UErrorCode& status) const; |
michael@0 | 504 | |
michael@0 | 505 | /** |
michael@0 | 506 | * Format a decimal number. Subclasses must implement |
michael@0 | 507 | * this method. The syntax of the unformatted number is a "numeric string" |
michael@0 | 508 | * as defined in the Decimal Arithmetic Specification, available at |
michael@0 | 509 | * http://speleotrove.com/decimal |
michael@0 | 510 | * |
michael@0 | 511 | * @param number The unformatted number, as a string, to be formatted. |
michael@0 | 512 | * @param appendTo Output parameter to receive result. |
michael@0 | 513 | * Result is appended to existing contents. |
michael@0 | 514 | * @param posIter On return, can be used to iterate over positions |
michael@0 | 515 | * of fields generated by this format call. |
michael@0 | 516 | * Can be NULL. |
michael@0 | 517 | * @param status Output param filled with success/failure status. |
michael@0 | 518 | * @return Reference to 'appendTo' parameter. |
michael@0 | 519 | * @stable 4.4 |
michael@0 | 520 | */ |
michael@0 | 521 | virtual UnicodeString& format(const StringPiece &number, |
michael@0 | 522 | UnicodeString& appendTo, |
michael@0 | 523 | FieldPositionIterator* posIter, |
michael@0 | 524 | UErrorCode& status) const; |
michael@0 | 525 | public: |
michael@0 | 526 | /** |
michael@0 | 527 | * Format a decimal number. |
michael@0 | 528 | * The number is a DigitList wrapper onto a floating point decimal number. |
michael@0 | 529 | * The default implementation in NumberFormat converts the decimal number |
michael@0 | 530 | * to a double and formats that. Subclasses of NumberFormat that want |
michael@0 | 531 | * to specifically handle big decimal numbers must override this method. |
michael@0 | 532 | * class DecimalFormat does so. |
michael@0 | 533 | * |
michael@0 | 534 | * @param number The number, a DigitList format Decimal Floating Point. |
michael@0 | 535 | * @param appendTo Output parameter to receive result. |
michael@0 | 536 | * Result is appended to existing contents. |
michael@0 | 537 | * @param posIter On return, can be used to iterate over positions |
michael@0 | 538 | * of fields generated by this format call. |
michael@0 | 539 | * @param status Output param filled with success/failure status. |
michael@0 | 540 | * @return Reference to 'appendTo' parameter. |
michael@0 | 541 | * @internal |
michael@0 | 542 | */ |
michael@0 | 543 | virtual UnicodeString& format(const DigitList &number, |
michael@0 | 544 | UnicodeString& appendTo, |
michael@0 | 545 | FieldPositionIterator* posIter, |
michael@0 | 546 | UErrorCode& status) const; |
michael@0 | 547 | |
michael@0 | 548 | /** |
michael@0 | 549 | * Format a decimal number. |
michael@0 | 550 | * The number is a DigitList wrapper onto a floating point decimal number. |
michael@0 | 551 | * The default implementation in NumberFormat converts the decimal number |
michael@0 | 552 | * to a double and formats that. Subclasses of NumberFormat that want |
michael@0 | 553 | * to specifically handle big decimal numbers must override this method. |
michael@0 | 554 | * class DecimalFormat does so. |
michael@0 | 555 | * |
michael@0 | 556 | * @param number The number, a DigitList format Decimal Floating Point. |
michael@0 | 557 | * @param appendTo Output parameter to receive result. |
michael@0 | 558 | * Result is appended to existing contents. |
michael@0 | 559 | * @param pos On input: an alignment field, if desired. |
michael@0 | 560 | * On output: the offsets of the alignment field. |
michael@0 | 561 | * @param status Output param filled with success/failure status. |
michael@0 | 562 | * @return Reference to 'appendTo' parameter. |
michael@0 | 563 | * @internal |
michael@0 | 564 | */ |
michael@0 | 565 | virtual UnicodeString& format(const DigitList &number, |
michael@0 | 566 | UnicodeString& appendTo, |
michael@0 | 567 | FieldPosition& pos, |
michael@0 | 568 | UErrorCode& status) const; |
michael@0 | 569 | |
michael@0 | 570 | public: |
michael@0 | 571 | |
michael@0 | 572 | /** |
michael@0 | 573 | * Return a long if possible (e.g. within range LONG_MAX, |
michael@0 | 574 | * LONG_MAX], and with no decimals), otherwise a double. If |
michael@0 | 575 | * IntegerOnly is set, will stop at a decimal point (or equivalent; |
michael@0 | 576 | * e.g. for rational numbers "1 2/3", will stop after the 1). |
michael@0 | 577 | * <P> |
michael@0 | 578 | * If no object can be parsed, index is unchanged, and NULL is |
michael@0 | 579 | * returned. |
michael@0 | 580 | * <P> |
michael@0 | 581 | * This is a pure virtual which concrete subclasses must implement. |
michael@0 | 582 | * |
michael@0 | 583 | * @param text The text to be parsed. |
michael@0 | 584 | * @param result Formattable to be set to the parse result. |
michael@0 | 585 | * If parse fails, return contents are undefined. |
michael@0 | 586 | * @param parsePosition The position to start parsing at on input. |
michael@0 | 587 | * On output, moved to after the last successfully |
michael@0 | 588 | * parse character. On parse failure, does not change. |
michael@0 | 589 | * @stable ICU 2.0 |
michael@0 | 590 | */ |
michael@0 | 591 | virtual void parse(const UnicodeString& text, |
michael@0 | 592 | Formattable& result, |
michael@0 | 593 | ParsePosition& parsePosition) const = 0; |
michael@0 | 594 | |
michael@0 | 595 | /** |
michael@0 | 596 | * Parse a string as a numeric value, and return a Formattable |
michael@0 | 597 | * numeric object. This method parses integers only if IntegerOnly |
michael@0 | 598 | * is set. |
michael@0 | 599 | * |
michael@0 | 600 | * @param text The text to be parsed. |
michael@0 | 601 | * @param result Formattable to be set to the parse result. |
michael@0 | 602 | * If parse fails, return contents are undefined. |
michael@0 | 603 | * @param status Output parameter set to a failure error code |
michael@0 | 604 | * when a failure occurs. |
michael@0 | 605 | * @see NumberFormat::isParseIntegerOnly |
michael@0 | 606 | * @stable ICU 2.0 |
michael@0 | 607 | */ |
michael@0 | 608 | virtual void parse(const UnicodeString& text, |
michael@0 | 609 | Formattable& result, |
michael@0 | 610 | UErrorCode& status) const; |
michael@0 | 611 | |
michael@0 | 612 | /** |
michael@0 | 613 | * Parses text from the given string as a currency amount. Unlike |
michael@0 | 614 | * the parse() method, this method will attempt to parse a generic |
michael@0 | 615 | * currency name, searching for a match of this object's locale's |
michael@0 | 616 | * currency display names, or for a 3-letter ISO currency code. |
michael@0 | 617 | * This method will fail if this format is not a currency format, |
michael@0 | 618 | * that is, if it does not contain the currency pattern symbol |
michael@0 | 619 | * (U+00A4) in its prefix or suffix. |
michael@0 | 620 | * |
michael@0 | 621 | * @param text the string to parse |
michael@0 | 622 | * @param pos input-output position; on input, the position within text |
michael@0 | 623 | * to match; must have 0 <= pos.getIndex() < text.length(); |
michael@0 | 624 | * on output, the position after the last matched character. |
michael@0 | 625 | * If the parse fails, the position in unchanged upon output. |
michael@0 | 626 | * @return if parse succeeds, a pointer to a newly-created CurrencyAmount |
michael@0 | 627 | * object (owned by the caller) containing information about |
michael@0 | 628 | * the parsed currency; if parse fails, this is NULL. |
michael@0 | 629 | * @stable ICU 49 |
michael@0 | 630 | */ |
michael@0 | 631 | virtual CurrencyAmount* parseCurrency(const UnicodeString& text, |
michael@0 | 632 | ParsePosition& pos) const; |
michael@0 | 633 | |
michael@0 | 634 | /** |
michael@0 | 635 | * Return true if this format will parse numbers as integers |
michael@0 | 636 | * only. For example in the English locale, with ParseIntegerOnly |
michael@0 | 637 | * true, the string "1234." would be parsed as the integer value |
michael@0 | 638 | * 1234 and parsing would stop at the "." character. Of course, |
michael@0 | 639 | * the exact format accepted by the parse operation is locale |
michael@0 | 640 | * dependant and determined by sub-classes of NumberFormat. |
michael@0 | 641 | * @return true if this format will parse numbers as integers |
michael@0 | 642 | * only. |
michael@0 | 643 | * @stable ICU 2.0 |
michael@0 | 644 | */ |
michael@0 | 645 | UBool isParseIntegerOnly(void) const; |
michael@0 | 646 | |
michael@0 | 647 | /** |
michael@0 | 648 | * Sets whether or not numbers should be parsed as integers only. |
michael@0 | 649 | * @param value set True, this format will parse numbers as integers |
michael@0 | 650 | * only. |
michael@0 | 651 | * @see isParseIntegerOnly |
michael@0 | 652 | * @stable ICU 2.0 |
michael@0 | 653 | */ |
michael@0 | 654 | virtual void setParseIntegerOnly(UBool value); |
michael@0 | 655 | |
michael@0 | 656 | /** |
michael@0 | 657 | * Sets whether lenient parsing should be enabled (it is off by default). |
michael@0 | 658 | * |
michael@0 | 659 | * @param enable <code>TRUE</code> if lenient parsing should be used, |
michael@0 | 660 | * <code>FALSE</code> otherwise. |
michael@0 | 661 | * @stable ICU 4.8 |
michael@0 | 662 | */ |
michael@0 | 663 | virtual void setLenient(UBool enable); |
michael@0 | 664 | |
michael@0 | 665 | /** |
michael@0 | 666 | * Returns whether lenient parsing is enabled (it is off by default). |
michael@0 | 667 | * |
michael@0 | 668 | * @return <code>TRUE</code> if lenient parsing is enabled, |
michael@0 | 669 | * <code>FALSE</code> otherwise. |
michael@0 | 670 | * @see #setLenient |
michael@0 | 671 | * @stable ICU 4.8 |
michael@0 | 672 | */ |
michael@0 | 673 | virtual UBool isLenient(void) const; |
michael@0 | 674 | |
michael@0 | 675 | /** |
michael@0 | 676 | * Returns the default number format for the current default |
michael@0 | 677 | * locale. The default format is one of the styles provided by |
michael@0 | 678 | * the other factory methods: getNumberInstance, |
michael@0 | 679 | * getCurrencyInstance or getPercentInstance. Exactly which one |
michael@0 | 680 | * is locale dependant. |
michael@0 | 681 | * @stable ICU 2.0 |
michael@0 | 682 | */ |
michael@0 | 683 | static NumberFormat* U_EXPORT2 createInstance(UErrorCode&); |
michael@0 | 684 | |
michael@0 | 685 | /** |
michael@0 | 686 | * Returns the default number format for the specified locale. |
michael@0 | 687 | * The default format is one of the styles provided by the other |
michael@0 | 688 | * factory methods: getNumberInstance, getCurrencyInstance or |
michael@0 | 689 | * getPercentInstance. Exactly which one is locale dependant. |
michael@0 | 690 | * @param inLocale the given locale. |
michael@0 | 691 | * @stable ICU 2.0 |
michael@0 | 692 | */ |
michael@0 | 693 | static NumberFormat* U_EXPORT2 createInstance(const Locale& inLocale, |
michael@0 | 694 | UErrorCode&); |
michael@0 | 695 | |
michael@0 | 696 | /** |
michael@0 | 697 | * Creates the specified decimal format style of the desired locale. |
michael@0 | 698 | * @param desiredLocale the given locale. |
michael@0 | 699 | * @param style the given style. |
michael@0 | 700 | * @param errorCode Output param filled with success/failure status. |
michael@0 | 701 | * @return A new NumberFormat instance. |
michael@0 | 702 | * @stable ICU 4.8 |
michael@0 | 703 | */ |
michael@0 | 704 | static NumberFormat* U_EXPORT2 createInstance(const Locale& desiredLocale, |
michael@0 | 705 | UNumberFormatStyle style, |
michael@0 | 706 | UErrorCode& errorCode); |
michael@0 | 707 | |
michael@0 | 708 | /** |
michael@0 | 709 | * Returns a currency format for the current default locale. |
michael@0 | 710 | * @stable ICU 2.0 |
michael@0 | 711 | */ |
michael@0 | 712 | static NumberFormat* U_EXPORT2 createCurrencyInstance(UErrorCode&); |
michael@0 | 713 | |
michael@0 | 714 | /** |
michael@0 | 715 | * Returns a currency format for the specified locale. |
michael@0 | 716 | * @param inLocale the given locale. |
michael@0 | 717 | * @stable ICU 2.0 |
michael@0 | 718 | */ |
michael@0 | 719 | static NumberFormat* U_EXPORT2 createCurrencyInstance(const Locale& inLocale, |
michael@0 | 720 | UErrorCode&); |
michael@0 | 721 | |
michael@0 | 722 | /** |
michael@0 | 723 | * Returns a percentage format for the current default locale. |
michael@0 | 724 | * @stable ICU 2.0 |
michael@0 | 725 | */ |
michael@0 | 726 | static NumberFormat* U_EXPORT2 createPercentInstance(UErrorCode&); |
michael@0 | 727 | |
michael@0 | 728 | /** |
michael@0 | 729 | * Returns a percentage format for the specified locale. |
michael@0 | 730 | * @param inLocale the given locale. |
michael@0 | 731 | * @stable ICU 2.0 |
michael@0 | 732 | */ |
michael@0 | 733 | static NumberFormat* U_EXPORT2 createPercentInstance(const Locale& inLocale, |
michael@0 | 734 | UErrorCode&); |
michael@0 | 735 | |
michael@0 | 736 | /** |
michael@0 | 737 | * Returns a scientific format for the current default locale. |
michael@0 | 738 | * @stable ICU 2.0 |
michael@0 | 739 | */ |
michael@0 | 740 | static NumberFormat* U_EXPORT2 createScientificInstance(UErrorCode&); |
michael@0 | 741 | |
michael@0 | 742 | /** |
michael@0 | 743 | * Returns a scientific format for the specified locale. |
michael@0 | 744 | * @param inLocale the given locale. |
michael@0 | 745 | * @stable ICU 2.0 |
michael@0 | 746 | */ |
michael@0 | 747 | static NumberFormat* U_EXPORT2 createScientificInstance(const Locale& inLocale, |
michael@0 | 748 | UErrorCode&); |
michael@0 | 749 | |
michael@0 | 750 | /** |
michael@0 | 751 | * Get the set of Locales for which NumberFormats are installed. |
michael@0 | 752 | * @param count Output param to receive the size of the locales |
michael@0 | 753 | * @stable ICU 2.0 |
michael@0 | 754 | */ |
michael@0 | 755 | static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count); |
michael@0 | 756 | |
michael@0 | 757 | #if !UCONFIG_NO_SERVICE |
michael@0 | 758 | /** |
michael@0 | 759 | * Register a new NumberFormatFactory. The factory will be adopted. |
michael@0 | 760 | * @param toAdopt the NumberFormatFactory instance to be adopted |
michael@0 | 761 | * @param status the in/out status code, no special meanings are assigned |
michael@0 | 762 | * @return a registry key that can be used to unregister this factory |
michael@0 | 763 | * @stable ICU 2.6 |
michael@0 | 764 | */ |
michael@0 | 765 | static URegistryKey U_EXPORT2 registerFactory(NumberFormatFactory* toAdopt, UErrorCode& status); |
michael@0 | 766 | |
michael@0 | 767 | /** |
michael@0 | 768 | * Unregister a previously-registered NumberFormatFactory using the key returned from the |
michael@0 | 769 | * register call. Key becomes invalid after a successful call and should not be used again. |
michael@0 | 770 | * The NumberFormatFactory corresponding to the key will be deleted. |
michael@0 | 771 | * @param key the registry key returned by a previous call to registerFactory |
michael@0 | 772 | * @param status the in/out status code, no special meanings are assigned |
michael@0 | 773 | * @return TRUE if the factory for the key was successfully unregistered |
michael@0 | 774 | * @stable ICU 2.6 |
michael@0 | 775 | */ |
michael@0 | 776 | static UBool U_EXPORT2 unregister(URegistryKey key, UErrorCode& status); |
michael@0 | 777 | |
michael@0 | 778 | /** |
michael@0 | 779 | * Return a StringEnumeration over the locales available at the time of the call, |
michael@0 | 780 | * including registered locales. |
michael@0 | 781 | * @return a StringEnumeration over the locales available at the time of the call |
michael@0 | 782 | * @stable ICU 2.6 |
michael@0 | 783 | */ |
michael@0 | 784 | static StringEnumeration* U_EXPORT2 getAvailableLocales(void); |
michael@0 | 785 | #endif /* UCONFIG_NO_SERVICE */ |
michael@0 | 786 | |
michael@0 | 787 | /** |
michael@0 | 788 | * Returns true if grouping is used in this format. For example, |
michael@0 | 789 | * in the English locale, with grouping on, the number 1234567 |
michael@0 | 790 | * might be formatted as "1,234,567". The grouping separator as |
michael@0 | 791 | * well as the size of each group is locale dependant and is |
michael@0 | 792 | * determined by sub-classes of NumberFormat. |
michael@0 | 793 | * @see setGroupingUsed |
michael@0 | 794 | * @stable ICU 2.0 |
michael@0 | 795 | */ |
michael@0 | 796 | UBool isGroupingUsed(void) const; |
michael@0 | 797 | |
michael@0 | 798 | /** |
michael@0 | 799 | * Set whether or not grouping will be used in this format. |
michael@0 | 800 | * @param newValue True, grouping will be used in this format. |
michael@0 | 801 | * @see getGroupingUsed |
michael@0 | 802 | * @stable ICU 2.0 |
michael@0 | 803 | */ |
michael@0 | 804 | virtual void setGroupingUsed(UBool newValue); |
michael@0 | 805 | |
michael@0 | 806 | /** |
michael@0 | 807 | * Returns the maximum number of digits allowed in the integer portion of a |
michael@0 | 808 | * number. |
michael@0 | 809 | * @return the maximum number of digits allowed in the integer portion of a |
michael@0 | 810 | * number. |
michael@0 | 811 | * @see setMaximumIntegerDigits |
michael@0 | 812 | * @stable ICU 2.0 |
michael@0 | 813 | */ |
michael@0 | 814 | int32_t getMaximumIntegerDigits(void) const; |
michael@0 | 815 | |
michael@0 | 816 | /** |
michael@0 | 817 | * Sets the maximum number of digits allowed in the integer portion of a |
michael@0 | 818 | * number. maximumIntegerDigits must be >= minimumIntegerDigits. If the |
michael@0 | 819 | * new value for maximumIntegerDigits is less than the current value |
michael@0 | 820 | * of minimumIntegerDigits, then minimumIntegerDigits will also be set to |
michael@0 | 821 | * the new value. |
michael@0 | 822 | * |
michael@0 | 823 | * @param newValue the new value for the maximum number of digits |
michael@0 | 824 | * allowed in the integer portion of a number. |
michael@0 | 825 | * @see getMaximumIntegerDigits |
michael@0 | 826 | * @stable ICU 2.0 |
michael@0 | 827 | */ |
michael@0 | 828 | virtual void setMaximumIntegerDigits(int32_t newValue); |
michael@0 | 829 | |
michael@0 | 830 | /** |
michael@0 | 831 | * Returns the minimum number of digits allowed in the integer portion of a |
michael@0 | 832 | * number. |
michael@0 | 833 | * @return the minimum number of digits allowed in the integer portion of a |
michael@0 | 834 | * number. |
michael@0 | 835 | * @see setMinimumIntegerDigits |
michael@0 | 836 | * @stable ICU 2.0 |
michael@0 | 837 | */ |
michael@0 | 838 | int32_t getMinimumIntegerDigits(void) const; |
michael@0 | 839 | |
michael@0 | 840 | /** |
michael@0 | 841 | * Sets the minimum number of digits allowed in the integer portion of a |
michael@0 | 842 | * number. minimumIntegerDigits must be <= maximumIntegerDigits. If the |
michael@0 | 843 | * new value for minimumIntegerDigits exceeds the current value |
michael@0 | 844 | * of maximumIntegerDigits, then maximumIntegerDigits will also be set to |
michael@0 | 845 | * the new value. |
michael@0 | 846 | * @param newValue the new value to be set. |
michael@0 | 847 | * @see getMinimumIntegerDigits |
michael@0 | 848 | * @stable ICU 2.0 |
michael@0 | 849 | */ |
michael@0 | 850 | virtual void setMinimumIntegerDigits(int32_t newValue); |
michael@0 | 851 | |
michael@0 | 852 | /** |
michael@0 | 853 | * Returns the maximum number of digits allowed in the fraction portion of a |
michael@0 | 854 | * number. |
michael@0 | 855 | * @return the maximum number of digits allowed in the fraction portion of a |
michael@0 | 856 | * number. |
michael@0 | 857 | * @see setMaximumFractionDigits |
michael@0 | 858 | * @stable ICU 2.0 |
michael@0 | 859 | */ |
michael@0 | 860 | int32_t getMaximumFractionDigits(void) const; |
michael@0 | 861 | |
michael@0 | 862 | /** |
michael@0 | 863 | * Sets the maximum number of digits allowed in the fraction portion of a |
michael@0 | 864 | * number. maximumFractionDigits must be >= minimumFractionDigits. If the |
michael@0 | 865 | * new value for maximumFractionDigits is less than the current value |
michael@0 | 866 | * of minimumFractionDigits, then minimumFractionDigits will also be set to |
michael@0 | 867 | * the new value. |
michael@0 | 868 | * @param newValue the new value to be set. |
michael@0 | 869 | * @see getMaximumFractionDigits |
michael@0 | 870 | * @stable ICU 2.0 |
michael@0 | 871 | */ |
michael@0 | 872 | virtual void setMaximumFractionDigits(int32_t newValue); |
michael@0 | 873 | |
michael@0 | 874 | /** |
michael@0 | 875 | * Returns the minimum number of digits allowed in the fraction portion of a |
michael@0 | 876 | * number. |
michael@0 | 877 | * @return the minimum number of digits allowed in the fraction portion of a |
michael@0 | 878 | * number. |
michael@0 | 879 | * @see setMinimumFractionDigits |
michael@0 | 880 | * @stable ICU 2.0 |
michael@0 | 881 | */ |
michael@0 | 882 | int32_t getMinimumFractionDigits(void) const; |
michael@0 | 883 | |
michael@0 | 884 | /** |
michael@0 | 885 | * Sets the minimum number of digits allowed in the fraction portion of a |
michael@0 | 886 | * number. minimumFractionDigits must be <= maximumFractionDigits. If the |
michael@0 | 887 | * new value for minimumFractionDigits exceeds the current value |
michael@0 | 888 | * of maximumFractionDigits, then maximumIntegerDigits will also be set to |
michael@0 | 889 | * the new value |
michael@0 | 890 | * @param newValue the new value to be set. |
michael@0 | 891 | * @see getMinimumFractionDigits |
michael@0 | 892 | * @stable ICU 2.0 |
michael@0 | 893 | */ |
michael@0 | 894 | virtual void setMinimumFractionDigits(int32_t newValue); |
michael@0 | 895 | |
michael@0 | 896 | /** |
michael@0 | 897 | * Sets the currency used to display currency |
michael@0 | 898 | * amounts. This takes effect immediately, if this format is a |
michael@0 | 899 | * currency format. If this format is not a currency format, then |
michael@0 | 900 | * the currency is used if and when this object becomes a |
michael@0 | 901 | * currency format. |
michael@0 | 902 | * @param theCurrency a 3-letter ISO code indicating new currency |
michael@0 | 903 | * to use. It need not be null-terminated. May be the empty |
michael@0 | 904 | * string or NULL to indicate no currency. |
michael@0 | 905 | * @param ec input-output error code |
michael@0 | 906 | * @stable ICU 3.0 |
michael@0 | 907 | */ |
michael@0 | 908 | virtual void setCurrency(const UChar* theCurrency, UErrorCode& ec); |
michael@0 | 909 | |
michael@0 | 910 | /** |
michael@0 | 911 | * Gets the currency used to display currency |
michael@0 | 912 | * amounts. This may be an empty string for some subclasses. |
michael@0 | 913 | * @return a 3-letter null-terminated ISO code indicating |
michael@0 | 914 | * the currency in use, or a pointer to the empty string. |
michael@0 | 915 | * @stable ICU 2.6 |
michael@0 | 916 | */ |
michael@0 | 917 | const UChar* getCurrency() const; |
michael@0 | 918 | |
michael@0 | 919 | public: |
michael@0 | 920 | |
michael@0 | 921 | /** |
michael@0 | 922 | * Return the class ID for this class. This is useful for |
michael@0 | 923 | * comparing to a return value from getDynamicClassID(). Note that, |
michael@0 | 924 | * because NumberFormat is an abstract base class, no fully constructed object |
michael@0 | 925 | * will have the class ID returned by NumberFormat::getStaticClassID(). |
michael@0 | 926 | * @return The class ID for all objects of this class. |
michael@0 | 927 | * @stable ICU 2.0 |
michael@0 | 928 | */ |
michael@0 | 929 | static UClassID U_EXPORT2 getStaticClassID(void); |
michael@0 | 930 | |
michael@0 | 931 | /** |
michael@0 | 932 | * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. |
michael@0 | 933 | * This method is to implement a simple version of RTTI, since not all |
michael@0 | 934 | * C++ compilers support genuine RTTI. Polymorphic operator==() and |
michael@0 | 935 | * clone() methods call this method. |
michael@0 | 936 | * <P> |
michael@0 | 937 | * @return The class ID for this object. All objects of a |
michael@0 | 938 | * given class have the same class ID. Objects of |
michael@0 | 939 | * other classes have different class IDs. |
michael@0 | 940 | * @stable ICU 2.0 |
michael@0 | 941 | */ |
michael@0 | 942 | virtual UClassID getDynamicClassID(void) const = 0; |
michael@0 | 943 | |
michael@0 | 944 | protected: |
michael@0 | 945 | |
michael@0 | 946 | /** |
michael@0 | 947 | * Default constructor for subclass use only. |
michael@0 | 948 | * @stable ICU 2.0 |
michael@0 | 949 | */ |
michael@0 | 950 | NumberFormat(); |
michael@0 | 951 | |
michael@0 | 952 | /** |
michael@0 | 953 | * Copy constructor. |
michael@0 | 954 | * @stable ICU 2.0 |
michael@0 | 955 | */ |
michael@0 | 956 | NumberFormat(const NumberFormat&); |
michael@0 | 957 | |
michael@0 | 958 | /** |
michael@0 | 959 | * Assignment operator. |
michael@0 | 960 | * @stable ICU 2.0 |
michael@0 | 961 | */ |
michael@0 | 962 | NumberFormat& operator=(const NumberFormat&); |
michael@0 | 963 | |
michael@0 | 964 | /** |
michael@0 | 965 | * Returns the currency in effect for this formatter. Subclasses |
michael@0 | 966 | * should override this method as needed. Unlike getCurrency(), |
michael@0 | 967 | * this method should never return "". |
michael@0 | 968 | * @result output parameter for null-terminated result, which must |
michael@0 | 969 | * have a capacity of at least 4 |
michael@0 | 970 | * @internal |
michael@0 | 971 | */ |
michael@0 | 972 | virtual void getEffectiveCurrency(UChar* result, UErrorCode& ec) const; |
michael@0 | 973 | |
michael@0 | 974 | #ifndef U_HIDE_INTERNAL_API |
michael@0 | 975 | /** |
michael@0 | 976 | * Creates the specified number format style of the desired locale. |
michael@0 | 977 | * If mustBeDecimalFormat is TRUE, then the returned pointer is |
michael@0 | 978 | * either a DecimalFormat or it is NULL. |
michael@0 | 979 | * @internal |
michael@0 | 980 | */ |
michael@0 | 981 | static NumberFormat* makeInstance(const Locale& desiredLocale, |
michael@0 | 982 | UNumberFormatStyle style, |
michael@0 | 983 | UBool mustBeDecimalFormat, |
michael@0 | 984 | UErrorCode& errorCode); |
michael@0 | 985 | #endif /* U_HIDE_INTERNAL_API */ |
michael@0 | 986 | |
michael@0 | 987 | private: |
michael@0 | 988 | |
michael@0 | 989 | static UBool isStyleSupported(UNumberFormatStyle style); |
michael@0 | 990 | |
michael@0 | 991 | /** |
michael@0 | 992 | * Creates the specified decimal format style of the desired locale. |
michael@0 | 993 | * @param desiredLocale the given locale. |
michael@0 | 994 | * @param style the given style. |
michael@0 | 995 | * @param errorCode Output param filled with success/failure status. |
michael@0 | 996 | * @return A new NumberFormat instance. |
michael@0 | 997 | */ |
michael@0 | 998 | static NumberFormat* makeInstance(const Locale& desiredLocale, |
michael@0 | 999 | UNumberFormatStyle style, |
michael@0 | 1000 | UErrorCode& errorCode); |
michael@0 | 1001 | |
michael@0 | 1002 | UBool fGroupingUsed; |
michael@0 | 1003 | int32_t fMaxIntegerDigits; |
michael@0 | 1004 | int32_t fMinIntegerDigits; |
michael@0 | 1005 | int32_t fMaxFractionDigits; |
michael@0 | 1006 | int32_t fMinFractionDigits; |
michael@0 | 1007 | |
michael@0 | 1008 | protected: |
michael@0 | 1009 | static const int32_t gDefaultMaxIntegerDigits; |
michael@0 | 1010 | static const int32_t gDefaultMinIntegerDigits; |
michael@0 | 1011 | |
michael@0 | 1012 | private: |
michael@0 | 1013 | UBool fParseIntegerOnly; |
michael@0 | 1014 | UBool fLenient; // TRUE => lenient parse is enabled |
michael@0 | 1015 | |
michael@0 | 1016 | // ISO currency code |
michael@0 | 1017 | UChar fCurrency[4]; |
michael@0 | 1018 | |
michael@0 | 1019 | friend class ICUNumberFormatFactory; // access to makeInstance |
michael@0 | 1020 | friend class ICUNumberFormatService; |
michael@0 | 1021 | friend class ::NumberFormatTest; // access to isStyleSupported() |
michael@0 | 1022 | }; |
michael@0 | 1023 | |
michael@0 | 1024 | #if !UCONFIG_NO_SERVICE |
michael@0 | 1025 | /** |
michael@0 | 1026 | * A NumberFormatFactory is used to register new number formats. The factory |
michael@0 | 1027 | * should be able to create any of the predefined formats for each locale it |
michael@0 | 1028 | * supports. When registered, the locales it supports extend or override the |
michael@0 | 1029 | * locale already supported by ICU. |
michael@0 | 1030 | * |
michael@0 | 1031 | * @stable ICU 2.6 |
michael@0 | 1032 | */ |
michael@0 | 1033 | class U_I18N_API NumberFormatFactory : public UObject { |
michael@0 | 1034 | public: |
michael@0 | 1035 | |
michael@0 | 1036 | /** |
michael@0 | 1037 | * Destructor |
michael@0 | 1038 | * @stable ICU 3.0 |
michael@0 | 1039 | */ |
michael@0 | 1040 | virtual ~NumberFormatFactory(); |
michael@0 | 1041 | |
michael@0 | 1042 | /** |
michael@0 | 1043 | * Return true if this factory will be visible. Default is true. |
michael@0 | 1044 | * If not visible, the locales supported by this factory will not |
michael@0 | 1045 | * be listed by getAvailableLocales. |
michael@0 | 1046 | * @stable ICU 2.6 |
michael@0 | 1047 | */ |
michael@0 | 1048 | virtual UBool visible(void) const = 0; |
michael@0 | 1049 | |
michael@0 | 1050 | /** |
michael@0 | 1051 | * Return the locale names directly supported by this factory. The number of names |
michael@0 | 1052 | * is returned in count; |
michael@0 | 1053 | * @stable ICU 2.6 |
michael@0 | 1054 | */ |
michael@0 | 1055 | virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const = 0; |
michael@0 | 1056 | |
michael@0 | 1057 | /** |
michael@0 | 1058 | * Return a number format of the appropriate type. If the locale |
michael@0 | 1059 | * is not supported, return null. If the locale is supported, but |
michael@0 | 1060 | * the type is not provided by this service, return null. Otherwise |
michael@0 | 1061 | * return an appropriate instance of NumberFormat. |
michael@0 | 1062 | * @stable ICU 2.6 |
michael@0 | 1063 | */ |
michael@0 | 1064 | virtual NumberFormat* createFormat(const Locale& loc, UNumberFormatStyle formatType) = 0; |
michael@0 | 1065 | }; |
michael@0 | 1066 | |
michael@0 | 1067 | /** |
michael@0 | 1068 | * A NumberFormatFactory that supports a single locale. It can be visible or invisible. |
michael@0 | 1069 | * @stable ICU 2.6 |
michael@0 | 1070 | */ |
michael@0 | 1071 | class U_I18N_API SimpleNumberFormatFactory : public NumberFormatFactory { |
michael@0 | 1072 | protected: |
michael@0 | 1073 | /** |
michael@0 | 1074 | * True if the locale supported by this factory is visible. |
michael@0 | 1075 | * @stable ICU 2.6 |
michael@0 | 1076 | */ |
michael@0 | 1077 | const UBool _visible; |
michael@0 | 1078 | |
michael@0 | 1079 | /** |
michael@0 | 1080 | * The locale supported by this factory, as a UnicodeString. |
michael@0 | 1081 | * @stable ICU 2.6 |
michael@0 | 1082 | */ |
michael@0 | 1083 | UnicodeString _id; |
michael@0 | 1084 | |
michael@0 | 1085 | public: |
michael@0 | 1086 | /** |
michael@0 | 1087 | * @stable ICU 2.6 |
michael@0 | 1088 | */ |
michael@0 | 1089 | SimpleNumberFormatFactory(const Locale& locale, UBool visible = TRUE); |
michael@0 | 1090 | |
michael@0 | 1091 | /** |
michael@0 | 1092 | * @stable ICU 3.0 |
michael@0 | 1093 | */ |
michael@0 | 1094 | virtual ~SimpleNumberFormatFactory(); |
michael@0 | 1095 | |
michael@0 | 1096 | /** |
michael@0 | 1097 | * @stable ICU 2.6 |
michael@0 | 1098 | */ |
michael@0 | 1099 | virtual UBool visible(void) const; |
michael@0 | 1100 | |
michael@0 | 1101 | /** |
michael@0 | 1102 | * @stable ICU 2.6 |
michael@0 | 1103 | */ |
michael@0 | 1104 | virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const; |
michael@0 | 1105 | }; |
michael@0 | 1106 | #endif /* #if !UCONFIG_NO_SERVICE */ |
michael@0 | 1107 | |
michael@0 | 1108 | // ------------------------------------- |
michael@0 | 1109 | |
michael@0 | 1110 | inline UBool |
michael@0 | 1111 | NumberFormat::isParseIntegerOnly() const |
michael@0 | 1112 | { |
michael@0 | 1113 | return fParseIntegerOnly; |
michael@0 | 1114 | } |
michael@0 | 1115 | |
michael@0 | 1116 | inline UBool |
michael@0 | 1117 | NumberFormat::isLenient() const |
michael@0 | 1118 | { |
michael@0 | 1119 | return fLenient; |
michael@0 | 1120 | } |
michael@0 | 1121 | |
michael@0 | 1122 | U_NAMESPACE_END |
michael@0 | 1123 | |
michael@0 | 1124 | #endif /* #if !UCONFIG_NO_FORMATTING */ |
michael@0 | 1125 | |
michael@0 | 1126 | #endif // _NUMFMT |
michael@0 | 1127 | //eof |