| 1 | // © 2018 and later: Unicode, Inc. and others. |
| 2 | // License & terms of use: http://www.unicode.org/copyright.html |
| 3 | |
| 4 | #include "unicode/utypes.h" |
| 5 | |
| 6 | #if !UCONFIG_NO_FORMATTING |
| 7 | #ifndef __UNUMBERFORMATTER_H__ |
| 8 | #define __UNUMBERFORMATTER_H__ |
| 9 | |
| 10 | #include "unicode/parseerr.h" |
| 11 | #include "unicode/ufieldpositer.h" |
| 12 | #include "unicode/umisc.h" |
| 13 | #include "unicode/uformattedvalue.h" |
| 14 | |
| 15 | |
| 16 | /** |
| 17 | * \file |
| 18 | * \brief C-compatible API for localized number formatting; not recommended for C++. |
| 19 | * |
| 20 | * This is the C-compatible version of the NumberFormatter API introduced in ICU 60. C++ users should |
| 21 | * include unicode/numberformatter.h and use the proper C++ APIs. |
| 22 | * |
| 23 | * The C API accepts a number skeleton string for specifying the settings for formatting, which covers a |
| 24 | * very large subset of all possible number formatting features. For more information on number skeleton |
| 25 | * strings, see unicode/numberformatter.h. |
| 26 | * |
| 27 | * When using UNumberFormatter, which is treated as immutable, the results are exported to a mutable |
| 28 | * UFormattedNumber object, which you subsequently use for populating your string buffer or iterating over |
| 29 | * the fields. |
| 30 | * |
| 31 | * Example code: |
| 32 | * <pre> |
| 33 | * // Setup: |
| 34 | * UErrorCode ec = U_ZERO_ERROR; |
| 35 | * UNumberFormatter* uformatter = unumf_openForSkeletonAndLocale(u"precision-integer", -1, "en", &ec); |
| 36 | * UFormattedNumber* uresult = unumf_openResult(&ec); |
| 37 | * if (U_FAILURE(ec)) { return; } |
| 38 | * |
| 39 | * // Format a double: |
| 40 | * unumf_formatDouble(uformatter, 5142.3, uresult, &ec); |
| 41 | * if (U_FAILURE(ec)) { return; } |
| 42 | * |
| 43 | * // Export the string to a malloc'd buffer: |
| 44 | * int32_t len = unumf_resultToString(uresult, NULL, 0, &ec); |
| 45 | * // at this point, ec == U_BUFFER_OVERFLOW_ERROR |
| 46 | * ec = U_ZERO_ERROR; |
| 47 | * UChar* buffer = (UChar*) malloc((len+1)*sizeof(UChar)); |
| 48 | * unumf_resultToString(uresult, buffer, len+1, &ec); |
| 49 | * if (U_FAILURE(ec)) { return; } |
| 50 | * // buffer should equal "5,142" |
| 51 | * |
| 52 | * // Cleanup: |
| 53 | * unumf_close(uformatter); |
| 54 | * unumf_closeResult(uresult); |
| 55 | * free(buffer); |
| 56 | * </pre> |
| 57 | * |
| 58 | * If you are a C++ user linking against the C libraries, you can use the LocalPointer versions of these |
| 59 | * APIs. The following example uses LocalPointer with the decimal number and field position APIs: |
| 60 | * |
| 61 | * <pre> |
| 62 | * // Setup: |
| 63 | * LocalUNumberFormatterPointer uformatter(unumf_openForSkeletonAndLocale(u"percent", -1, "en", &ec)); |
| 64 | * LocalUFormattedNumberPointer uresult(unumf_openResult(&ec)); |
| 65 | * if (U_FAILURE(ec)) { return; } |
| 66 | * |
| 67 | * // Format a decimal number: |
| 68 | * unumf_formatDecimal(uformatter.getAlias(), "9.87E-3", -1, uresult.getAlias(), &ec); |
| 69 | * if (U_FAILURE(ec)) { return; } |
| 70 | * |
| 71 | * // Get the location of the percent sign: |
| 72 | * UFieldPosition ufpos = {UNUM_PERCENT_FIELD, 0, 0}; |
| 73 | * unumf_resultNextFieldPosition(uresult.getAlias(), &ufpos, &ec); |
| 74 | * // ufpos should contain beginIndex=7 and endIndex=8 since the string is "0.00987%" |
| 75 | * |
| 76 | * // No need to do any cleanup since we are using LocalPointer. |
| 77 | * </pre> |
| 78 | */ |
| 79 | |
| 80 | /** |
| 81 | * An enum declaring how to render units, including currencies. Example outputs when formatting 123 USD and 123 |
| 82 | * meters in <em>en-CA</em>: |
| 83 | * |
| 84 | * <p> |
| 85 | * <ul> |
| 86 | * <li>NARROW*: "$123.00" and "123 m" |
| 87 | * <li>SHORT: "US$ 123.00" and "123 m" |
| 88 | * <li>FULL_NAME: "123.00 US dollars" and "123 meters" |
| 89 | * <li>ISO_CODE: "USD 123.00" and undefined behavior |
| 90 | * <li>HIDDEN: "123.00" and "123" |
| 91 | * </ul> |
| 92 | * |
| 93 | * <p> |
| 94 | * This enum is similar to {@link UMeasureFormatWidth}. |
| 95 | * |
| 96 | * @stable ICU 60 |
| 97 | */ |
| 98 | typedef enum UNumberUnitWidth { |
| 99 | /** |
| 100 | * Print an abbreviated version of the unit name. Similar to SHORT, but always use the shortest available |
| 101 | * abbreviation or symbol. This option can be used when the context hints at the identity of the unit. For more |
| 102 | * information on the difference between NARROW and SHORT, see SHORT. |
| 103 | * |
| 104 | * <p> |
| 105 | * In CLDR, this option corresponds to the "Narrow" format for measure units and the "¤¤¤¤¤" placeholder for |
| 106 | * currencies. |
| 107 | * |
| 108 | * @stable ICU 60 |
| 109 | */ |
| 110 | UNUM_UNIT_WIDTH_NARROW, |
| 111 | |
| 112 | /** |
| 113 | * Print an abbreviated version of the unit name. Similar to NARROW, but use a slightly wider abbreviation or |
| 114 | * symbol when there may be ambiguity. This is the default behavior. |
| 115 | * |
| 116 | * <p> |
| 117 | * For example, in <em>es-US</em>, the SHORT form for Fahrenheit is "{0} °F", but the NARROW form is "{0}°", |
| 118 | * since Fahrenheit is the customary unit for temperature in that locale. |
| 119 | * |
| 120 | * <p> |
| 121 | * In CLDR, this option corresponds to the "Short" format for measure units and the "¤" placeholder for |
| 122 | * currencies. |
| 123 | * |
| 124 | * @stable ICU 60 |
| 125 | */ |
| 126 | UNUM_UNIT_WIDTH_SHORT, |
| 127 | |
| 128 | /** |
| 129 | * Print the full name of the unit, without any abbreviations. |
| 130 | * |
| 131 | * <p> |
| 132 | * In CLDR, this option corresponds to the default format for measure units and the "¤¤¤" placeholder for |
| 133 | * currencies. |
| 134 | * |
| 135 | * @stable ICU 60 |
| 136 | */ |
| 137 | UNUM_UNIT_WIDTH_FULL_NAME, |
| 138 | |
| 139 | /** |
| 140 | * Use the three-digit ISO XXX code in place of the symbol for displaying currencies. The behavior of this |
| 141 | * option is currently undefined for use with measure units. |
| 142 | * |
| 143 | * <p> |
| 144 | * In CLDR, this option corresponds to the "¤¤" placeholder for currencies. |
| 145 | * |
| 146 | * @stable ICU 60 |
| 147 | */ |
| 148 | UNUM_UNIT_WIDTH_ISO_CODE, |
| 149 | |
| 150 | /** |
| 151 | * Format the number according to the specified unit, but do not display the unit. For currencies, apply |
| 152 | * monetary symbols and formats as with SHORT, but omit the currency symbol. For measure units, the behavior is |
| 153 | * equivalent to not specifying the unit at all. |
| 154 | * |
| 155 | * @stable ICU 60 |
| 156 | */ |
| 157 | UNUM_UNIT_WIDTH_HIDDEN, |
| 158 | |
| 159 | /** |
| 160 | * One more than the highest UNumberUnitWidth value. |
| 161 | * |
| 162 | * @internal ICU 60: The numeric value may change over time; see ICU ticket #12420. |
| 163 | */ |
| 164 | UNUM_UNIT_WIDTH_COUNT |
| 165 | } UNumberUnitWidth; |
| 166 | |
| 167 | /** |
| 168 | * An enum declaring the strategy for when and how to display grouping separators (i.e., the |
| 169 | * separator, often a comma or period, after every 2-3 powers of ten). The choices are several |
| 170 | * pre-built strategies for different use cases that employ locale data whenever possible. Example |
| 171 | * outputs for 1234 and 1234567 in <em>en-IN</em>: |
| 172 | * |
| 173 | * <ul> |
| 174 | * <li>OFF: 1234 and 12345 |
| 175 | * <li>MIN2: 1234 and 12,34,567 |
| 176 | * <li>AUTO: 1,234 and 12,34,567 |
| 177 | * <li>ON_ALIGNED: 1,234 and 12,34,567 |
| 178 | * <li>THOUSANDS: 1,234 and 1,234,567 |
| 179 | * </ul> |
| 180 | * |
| 181 | * <p> |
| 182 | * The default is AUTO, which displays grouping separators unless the locale data says that grouping |
| 183 | * is not customary. To force grouping for all numbers greater than 1000 consistently across locales, |
| 184 | * use ON_ALIGNED. On the other hand, to display grouping less frequently than the default, use MIN2 |
| 185 | * or OFF. See the docs of each option for details. |
| 186 | * |
| 187 | * <p> |
| 188 | * Note: This enum specifies the strategy for grouping sizes. To set which character to use as the |
| 189 | * grouping separator, use the "symbols" setter. |
| 190 | * |
| 191 | * @stable ICU 63 |
| 192 | */ |
| 193 | typedef enum UNumberGroupingStrategy { |
| 194 | /** |
| 195 | * Do not display grouping separators in any locale. |
| 196 | * |
| 197 | * @stable ICU 61 |
| 198 | */ |
| 199 | UNUM_GROUPING_OFF, |
| 200 | |
| 201 | /** |
| 202 | * Display grouping using locale defaults, except do not show grouping on values smaller than |
| 203 | * 10000 (such that there is a <em>minimum of two digits</em> before the first separator). |
| 204 | * |
| 205 | * <p> |
| 206 | * Note that locales may restrict grouping separators to be displayed only on 1 million or |
| 207 | * greater (for example, ee and hu) or disable grouping altogether (for example, bg currency). |
| 208 | * |
| 209 | * <p> |
| 210 | * Locale data is used to determine whether to separate larger numbers into groups of 2 |
| 211 | * (customary in South Asia) or groups of 3 (customary in Europe and the Americas). |
| 212 | * |
| 213 | * @stable ICU 61 |
| 214 | */ |
| 215 | UNUM_GROUPING_MIN2, |
| 216 | |
| 217 | /** |
| 218 | * Display grouping using the default strategy for all locales. This is the default behavior. |
| 219 | * |
| 220 | * <p> |
| 221 | * Note that locales may restrict grouping separators to be displayed only on 1 million or |
| 222 | * greater (for example, ee and hu) or disable grouping altogether (for example, bg currency). |
| 223 | * |
| 224 | * <p> |
| 225 | * Locale data is used to determine whether to separate larger numbers into groups of 2 |
| 226 | * (customary in South Asia) or groups of 3 (customary in Europe and the Americas). |
| 227 | * |
| 228 | * @stable ICU 61 |
| 229 | */ |
| 230 | UNUM_GROUPING_AUTO, |
| 231 | |
| 232 | /** |
| 233 | * Always display the grouping separator on values of at least 1000. |
| 234 | * |
| 235 | * <p> |
| 236 | * This option ignores the locale data that restricts or disables grouping, described in MIN2 and |
| 237 | * AUTO. This option may be useful to normalize the alignment of numbers, such as in a |
| 238 | * spreadsheet. |
| 239 | * |
| 240 | * <p> |
| 241 | * Locale data is used to determine whether to separate larger numbers into groups of 2 |
| 242 | * (customary in South Asia) or groups of 3 (customary in Europe and the Americas). |
| 243 | * |
| 244 | * @stable ICU 61 |
| 245 | */ |
| 246 | UNUM_GROUPING_ON_ALIGNED, |
| 247 | |
| 248 | /** |
| 249 | * Use the Western defaults: groups of 3 and enabled for all numbers 1000 or greater. Do not use |
| 250 | * locale data for determining the grouping strategy. |
| 251 | * |
| 252 | * @stable ICU 61 |
| 253 | */ |
| 254 | UNUM_GROUPING_THOUSANDS |
| 255 | |
| 256 | #ifndef U_HIDE_INTERNAL_API |
| 257 | , |
| 258 | /** |
| 259 | * One more than the highest UNumberGroupingStrategy value. |
| 260 | * |
| 261 | * @internal ICU 62: The numeric value may change over time; see ICU ticket #12420. |
| 262 | */ |
| 263 | UNUM_GROUPING_COUNT |
| 264 | #endif /* U_HIDE_INTERNAL_API */ |
| 265 | |
| 266 | } UNumberGroupingStrategy; |
| 267 | |
| 268 | /** |
| 269 | * An enum declaring how to denote positive and negative numbers. Example outputs when formatting |
| 270 | * 123, 0, and -123 in <em>en-US</em>: |
| 271 | * |
| 272 | * <ul> |
| 273 | * <li>AUTO: "123", "0", and "-123" |
| 274 | * <li>ALWAYS: "+123", "+0", and "-123" |
| 275 | * <li>NEVER: "123", "0", and "123" |
| 276 | * <li>ACCOUNTING: "$123", "$0", and "($123)" |
| 277 | * <li>ACCOUNTING_ALWAYS: "+$123", "+$0", and "($123)" |
| 278 | * <li>EXCEPT_ZERO: "+123", "0", and "-123" |
| 279 | * <li>ACCOUNTING_EXCEPT_ZERO: "+$123", "$0", and "($123)" |
| 280 | * </ul> |
| 281 | * |
| 282 | * <p> |
| 283 | * The exact format, including the position and the code point of the sign, differ by locale. |
| 284 | * |
| 285 | * @stable ICU 60 |
| 286 | */ |
| 287 | typedef enum UNumberSignDisplay { |
| 288 | /** |
| 289 | * Show the minus sign on negative numbers, and do not show the sign on positive numbers. This is the default |
| 290 | * behavior. |
| 291 | * |
| 292 | * @stable ICU 60 |
| 293 | */ |
| 294 | UNUM_SIGN_AUTO, |
| 295 | |
| 296 | /** |
| 297 | * Show the minus sign on negative numbers and the plus sign on positive numbers, including zero. |
| 298 | * To hide the sign on zero, see {@link UNUM_SIGN_EXCEPT_ZERO}. |
| 299 | * |
| 300 | * @stable ICU 60 |
| 301 | */ |
| 302 | UNUM_SIGN_ALWAYS, |
| 303 | |
| 304 | /** |
| 305 | * Do not show the sign on positive or negative numbers. |
| 306 | * |
| 307 | * @stable ICU 60 |
| 308 | */ |
| 309 | UNUM_SIGN_NEVER, |
| 310 | |
| 311 | /** |
| 312 | * Use the locale-dependent accounting format on negative numbers, and do not show the sign on positive numbers. |
| 313 | * |
| 314 | * <p> |
| 315 | * The accounting format is defined in CLDR and varies by locale; in many Western locales, the format is a pair |
| 316 | * of parentheses around the number. |
| 317 | * |
| 318 | * <p> |
| 319 | * Note: Since CLDR defines the accounting format in the monetary context only, this option falls back to the |
| 320 | * AUTO sign display strategy when formatting without a currency unit. This limitation may be lifted in the |
| 321 | * future. |
| 322 | * |
| 323 | * @stable ICU 60 |
| 324 | */ |
| 325 | UNUM_SIGN_ACCOUNTING, |
| 326 | |
| 327 | /** |
| 328 | * Use the locale-dependent accounting format on negative numbers, and show the plus sign on |
| 329 | * positive numbers, including zero. For more information on the accounting format, see the |
| 330 | * ACCOUNTING sign display strategy. To hide the sign on zero, see |
| 331 | * {@link UNUM_SIGN_ACCOUNTING_EXCEPT_ZERO}. |
| 332 | * |
| 333 | * @stable ICU 60 |
| 334 | */ |
| 335 | UNUM_SIGN_ACCOUNTING_ALWAYS, |
| 336 | |
| 337 | /** |
| 338 | * Show the minus sign on negative numbers and the plus sign on positive numbers. Do not show a |
| 339 | * sign on zero or NaN, unless the sign bit is set (-0.0 gets a sign). |
| 340 | * |
| 341 | * @stable ICU 61 |
| 342 | */ |
| 343 | UNUM_SIGN_EXCEPT_ZERO, |
| 344 | |
| 345 | /** |
| 346 | * Use the locale-dependent accounting format on negative numbers, and show the plus sign on |
| 347 | * positive numbers. Do not show a sign on zero or NaN, unless the sign bit is set (-0.0 gets a |
| 348 | * sign). For more information on the accounting format, see the ACCOUNTING sign display |
| 349 | * strategy. |
| 350 | * |
| 351 | * @stable ICU 61 |
| 352 | */ |
| 353 | UNUM_SIGN_ACCOUNTING_EXCEPT_ZERO, |
| 354 | |
| 355 | /** |
| 356 | * One more than the highest UNumberSignDisplay value. |
| 357 | * |
| 358 | * @internal ICU 60: The numeric value may change over time; see ICU ticket #12420. |
| 359 | */ |
| 360 | UNUM_SIGN_COUNT |
| 361 | } UNumberSignDisplay; |
| 362 | |
| 363 | /** |
| 364 | * An enum declaring how to render the decimal separator. |
| 365 | * |
| 366 | * <p> |
| 367 | * <ul> |
| 368 | * <li>UNUM_DECIMAL_SEPARATOR_AUTO: "1", "1.1" |
| 369 | * <li>UNUM_DECIMAL_SEPARATOR_ALWAYS: "1.", "1.1" |
| 370 | * </ul> |
| 371 | * |
| 372 | * @stable ICU 60 |
| 373 | */ |
| 374 | typedef enum UNumberDecimalSeparatorDisplay { |
| 375 | /** |
| 376 | * Show the decimal separator when there are one or more digits to display after the separator, and do not show |
| 377 | * it otherwise. This is the default behavior. |
| 378 | * |
| 379 | * @stable ICU 60 |
| 380 | */ |
| 381 | UNUM_DECIMAL_SEPARATOR_AUTO, |
| 382 | |
| 383 | /** |
| 384 | * Always show the decimal separator, even if there are no digits to display after the separator. |
| 385 | * |
| 386 | * @stable ICU 60 |
| 387 | */ |
| 388 | UNUM_DECIMAL_SEPARATOR_ALWAYS, |
| 389 | |
| 390 | /** |
| 391 | * One more than the highest UNumberDecimalSeparatorDisplay value. |
| 392 | * |
| 393 | * @internal ICU 60: The numeric value may change over time; see ICU ticket #12420. |
| 394 | */ |
| 395 | UNUM_DECIMAL_SEPARATOR_COUNT |
| 396 | } UNumberDecimalSeparatorDisplay; |
| 397 | |
| 398 | struct UNumberFormatter; |
| 399 | /** |
| 400 | * C-compatible version of icu::number::LocalizedNumberFormatter. |
| 401 | * |
| 402 | * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead. |
| 403 | * |
| 404 | * @stable ICU 62 |
| 405 | */ |
| 406 | typedef struct UNumberFormatter UNumberFormatter; |
| 407 | |
| 408 | struct UFormattedNumber; |
| 409 | /** |
| 410 | * C-compatible version of icu::number::FormattedNumber. |
| 411 | * |
| 412 | * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead. |
| 413 | * |
| 414 | * @stable ICU 62 |
| 415 | */ |
| 416 | typedef struct UFormattedNumber UFormattedNumber; |
| 417 | |
| 418 | |
| 419 | /** |
| 420 | * Creates a new UNumberFormatter for the given skeleton string and locale. This is currently the only |
| 421 | * method for creating a new UNumberFormatter. |
| 422 | * |
| 423 | * Objects of type UNumberFormatter returned by this method are threadsafe. |
| 424 | * |
| 425 | * For more details on skeleton strings, see the documentation in numberformatter.h. For more details on |
| 426 | * the usage of this API, see the documentation at the top of unumberformatter.h. |
| 427 | * |
| 428 | * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead. |
| 429 | * |
| 430 | * @param skeleton The skeleton string, like u"percent precision-integer" |
| 431 | * @param skeletonLen The number of UChars in the skeleton string, or -1 it it is NUL-terminated. |
| 432 | * @param locale The NUL-terminated locale ID. |
| 433 | * @param ec Set if an error occurs. |
| 434 | * @stable ICU 62 |
| 435 | */ |
| 436 | U_STABLE UNumberFormatter* U_EXPORT2 |
| 437 | unumf_openForSkeletonAndLocale(const UChar* skeleton, int32_t skeletonLen, const char* locale, |
| 438 | UErrorCode* ec); |
| 439 | |
| 440 | |
| 441 | #ifndef U_HIDE_DRAFT_API |
| 442 | /** |
| 443 | * Like unumf_openForSkeletonAndLocale, but accepts a UParseError, which will be populated with the |
| 444 | * location of a skeleton syntax error if such a syntax error exists. |
| 445 | * |
| 446 | * @param skeleton The skeleton string, like u"percent precision-integer" |
| 447 | * @param skeletonLen The number of UChars in the skeleton string, or -1 it it is NUL-terminated. |
| 448 | * @param locale The NUL-terminated locale ID. |
| 449 | * @param perror A parse error struct populated if an error occurs when parsing. Can be NULL. |
| 450 | * If no error occurs, perror->offset will be set to -1. |
| 451 | * @param ec Set if an error occurs. |
| 452 | * @draft ICU 64 |
| 453 | */ |
| 454 | U_DRAFT UNumberFormatter* U_EXPORT2 |
| 455 | unumf_openForSkeletonAndLocaleWithError( |
| 456 | const UChar* skeleton, int32_t skeletonLen, const char* locale, UParseError* perror, UErrorCode* ec); |
| 457 | #endif // U_HIDE_DRAFT_API |
| 458 | |
| 459 | |
| 460 | /** |
| 461 | * Creates an object to hold the result of a UNumberFormatter |
| 462 | * operation. The object can be used repeatedly; it is cleared whenever |
| 463 | * passed to a format function. |
| 464 | * |
| 465 | * @param ec Set if an error occurs. |
| 466 | * @stable ICU 62 |
| 467 | */ |
| 468 | U_STABLE UFormattedNumber* U_EXPORT2 |
| 469 | unumf_openResult(UErrorCode* ec); |
| 470 | |
| 471 | |
| 472 | /** |
| 473 | * Uses a UNumberFormatter to format an integer to a UFormattedNumber. A string, field position, and other |
| 474 | * information can be retrieved from the UFormattedNumber. |
| 475 | * |
| 476 | * The UNumberFormatter can be shared between threads. Each thread should have its own local |
| 477 | * UFormattedNumber, however, for storing the result of the formatting operation. |
| 478 | * |
| 479 | * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead. |
| 480 | * |
| 481 | * @param uformatter A formatter object created by unumf_openForSkeletonAndLocale or similar. |
| 482 | * @param value The number to be formatted. |
| 483 | * @param uresult The object that will be mutated to store the result; see unumf_openResult. |
| 484 | * @param ec Set if an error occurs. |
| 485 | * @stable ICU 62 |
| 486 | */ |
| 487 | U_STABLE void U_EXPORT2 |
| 488 | unumf_formatInt(const UNumberFormatter* uformatter, int64_t value, UFormattedNumber* uresult, |
| 489 | UErrorCode* ec); |
| 490 | |
| 491 | |
| 492 | /** |
| 493 | * Uses a UNumberFormatter to format a double to a UFormattedNumber. A string, field position, and other |
| 494 | * information can be retrieved from the UFormattedNumber. |
| 495 | * |
| 496 | * The UNumberFormatter can be shared between threads. Each thread should have its own local |
| 497 | * UFormattedNumber, however, for storing the result of the formatting operation. |
| 498 | * |
| 499 | * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead. |
| 500 | * |
| 501 | * @param uformatter A formatter object created by unumf_openForSkeletonAndLocale or similar. |
| 502 | * @param value The number to be formatted. |
| 503 | * @param uresult The object that will be mutated to store the result; see unumf_openResult. |
| 504 | * @param ec Set if an error occurs. |
| 505 | * @stable ICU 62 |
| 506 | */ |
| 507 | U_STABLE void U_EXPORT2 |
| 508 | unumf_formatDouble(const UNumberFormatter* uformatter, double value, UFormattedNumber* uresult, |
| 509 | UErrorCode* ec); |
| 510 | |
| 511 | |
| 512 | /** |
| 513 | * Uses a UNumberFormatter to format a decimal number to a UFormattedNumber. A string, field position, and |
| 514 | * other information can be retrieved from the UFormattedNumber. |
| 515 | * |
| 516 | * The UNumberFormatter can be shared between threads. Each thread should have its own local |
| 517 | * UFormattedNumber, however, for storing the result of the formatting operation. |
| 518 | * |
| 519 | * The syntax of the unformatted number is a "numeric string" as defined in the Decimal Arithmetic |
| 520 | * Specification, available at http://speleotrove.com/decimal |
| 521 | * |
| 522 | * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead. |
| 523 | * |
| 524 | * @param uformatter A formatter object created by unumf_openForSkeletonAndLocale or similar. |
| 525 | * @param value The numeric string to be formatted. |
| 526 | * @param valueLen The length of the numeric string, or -1 if it is NUL-terminated. |
| 527 | * @param uresult The object that will be mutated to store the result; see unumf_openResult. |
| 528 | * @param ec Set if an error occurs. |
| 529 | * @stable ICU 62 |
| 530 | */ |
| 531 | U_STABLE void U_EXPORT2 |
| 532 | unumf_formatDecimal(const UNumberFormatter* uformatter, const char* value, int32_t valueLen, |
| 533 | UFormattedNumber* uresult, UErrorCode* ec); |
| 534 | |
| 535 | #ifndef U_HIDE_DRAFT_API |
| 536 | /** |
| 537 | * Returns a representation of a UFormattedNumber as a UFormattedValue, |
| 538 | * which can be subsequently passed to any API requiring that type. |
| 539 | * |
| 540 | * The returned object is owned by the UFormattedNumber and is valid |
| 541 | * only as long as the UFormattedNumber is present and unchanged in memory. |
| 542 | * |
| 543 | * You can think of this method as a cast between types. |
| 544 | * |
| 545 | * @param uresult The object containing the formatted string. |
| 546 | * @param ec Set if an error occurs. |
| 547 | * @return A UFormattedValue owned by the input object. |
| 548 | * @draft ICU 64 |
| 549 | */ |
| 550 | U_DRAFT const UFormattedValue* U_EXPORT2 |
| 551 | unumf_resultAsValue(const UFormattedNumber* uresult, UErrorCode* ec); |
| 552 | #endif /* U_HIDE_DRAFT_API */ |
| 553 | |
| 554 | |
| 555 | /** |
| 556 | * Extracts the result number string out of a UFormattedNumber to a UChar buffer if possible. |
| 557 | * If bufferCapacity is greater than the required length, a terminating NUL is written. |
| 558 | * If bufferCapacity is less than the required length, an error code is set. |
| 559 | * |
| 560 | * Also see ufmtval_getString, which returns a NUL-terminated string: |
| 561 | * |
| 562 | * int32_t len; |
| 563 | * const UChar* str = ufmtval_getString(unumf_resultAsValue(uresult, &ec), &len, &ec); |
| 564 | * |
| 565 | * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead. |
| 566 | * |
| 567 | * @param uresult The object containing the formatted number. |
| 568 | * @param buffer Where to save the string output. |
| 569 | * @param bufferCapacity The number of UChars available in the buffer. |
| 570 | * @param ec Set if an error occurs. |
| 571 | * @return The required length. |
| 572 | * @stable ICU 62 |
| 573 | */ |
| 574 | U_STABLE int32_t U_EXPORT2 |
| 575 | unumf_resultToString(const UFormattedNumber* uresult, UChar* buffer, int32_t bufferCapacity, |
| 576 | UErrorCode* ec); |
| 577 | |
| 578 | |
| 579 | /** |
| 580 | * Determines the start and end indices of the next occurrence of the given <em>field</em> in the |
| 581 | * output string. This allows you to determine the locations of, for example, the integer part, |
| 582 | * fraction part, or symbols. |
| 583 | * |
| 584 | * This is a simpler but less powerful alternative to {@link ufmtval_nextPosition}. |
| 585 | * |
| 586 | * If a field occurs just once, calling this method will find that occurrence and return it. If a |
| 587 | * field occurs multiple times, this method may be called repeatedly with the following pattern: |
| 588 | * |
| 589 | * <pre> |
| 590 | * UFieldPosition ufpos = {UNUM_GROUPING_SEPARATOR_FIELD, 0, 0}; |
| 591 | * while (unumf_resultNextFieldPosition(uresult, ufpos, &ec)) { |
| 592 | * // do something with ufpos. |
| 593 | * } |
| 594 | * </pre> |
| 595 | * |
| 596 | * This method is useful if you know which field to query. If you want all available field position |
| 597 | * information, use unumf_resultGetAllFieldPositions(). |
| 598 | * |
| 599 | * NOTE: All fields of the UFieldPosition must be initialized before calling this method. |
| 600 | * |
| 601 | * @param uresult The object containing the formatted number. |
| 602 | * @param ufpos |
| 603 | * Input+output variable. On input, the "field" property determines which field to look up, |
| 604 | * and the "endIndex" property determines where to begin the search. On output, the |
| 605 | * "beginIndex" field is set to the beginning of the first occurrence of the field after the |
| 606 | * input "endIndex", and "endIndex" is set to the end of that occurrence of the field |
| 607 | * (exclusive index). If a field position is not found, the FieldPosition is not changed and |
| 608 | * the method returns FALSE. |
| 609 | * @param ec Set if an error occurs. |
| 610 | * @stable ICU 62 |
| 611 | */ |
| 612 | U_STABLE UBool U_EXPORT2 |
| 613 | unumf_resultNextFieldPosition(const UFormattedNumber* uresult, UFieldPosition* ufpos, UErrorCode* ec); |
| 614 | |
| 615 | |
| 616 | /** |
| 617 | * Populates the given iterator with all fields in the formatted output string. This allows you to |
| 618 | * determine the locations of the integer part, fraction part, and sign. |
| 619 | * |
| 620 | * This is an alternative to the more powerful {@link ufmtval_nextPosition} API. |
| 621 | * |
| 622 | * If you need information on only one field, use {@link ufmtval_nextPosition} or |
| 623 | * {@link unumf_resultNextFieldPosition}. |
| 624 | * |
| 625 | * @param uresult The object containing the formatted number. |
| 626 | * @param ufpositer |
| 627 | * A pointer to a UFieldPositionIterator created by {@link #ufieldpositer_open}. Iteration |
| 628 | * information already present in the UFieldPositionIterator is deleted, and the iterator is reset |
| 629 | * to apply to the fields in the formatted string created by this function call. The field values |
| 630 | * and indexes returned by {@link #ufieldpositer_next} represent fields denoted by |
| 631 | * the UNumberFormatFields enum. Fields are not returned in a guaranteed order. Fields cannot |
| 632 | * overlap, but they may nest. For example, 1234 could format as "1,234" which might consist of a |
| 633 | * grouping separator field for ',' and an integer field encompassing the entire string. |
| 634 | * @param ec Set if an error occurs. |
| 635 | * @stable ICU 62 |
| 636 | */ |
| 637 | U_STABLE void U_EXPORT2 |
| 638 | unumf_resultGetAllFieldPositions(const UFormattedNumber* uresult, UFieldPositionIterator* ufpositer, |
| 639 | UErrorCode* ec); |
| 640 | |
| 641 | |
| 642 | // TODO(ICU-20775): Propose this as API. |
| 643 | // NOTE: This is not currently implemented. |
| 644 | // U_DRAFT int32_t U_EXPORT2 |
| 645 | // unumf_resultToDecimalNumber(const UFormattedNumber* uresult, char* buffer, int32_t bufferCapacity, |
| 646 | // UErrorCode* ec); |
| 647 | |
| 648 | |
| 649 | /** |
| 650 | * Releases the UNumberFormatter created by unumf_openForSkeletonAndLocale(). |
| 651 | * |
| 652 | * @param uformatter An object created by unumf_openForSkeletonAndLocale(). |
| 653 | * @stable ICU 62 |
| 654 | */ |
| 655 | U_STABLE void U_EXPORT2 |
| 656 | unumf_close(UNumberFormatter* uformatter); |
| 657 | |
| 658 | |
| 659 | /** |
| 660 | * Releases the UFormattedNumber created by unumf_openResult(). |
| 661 | * |
| 662 | * @param uresult An object created by unumf_openResult(). |
| 663 | * @stable ICU 62 |
| 664 | */ |
| 665 | U_STABLE void U_EXPORT2 |
| 666 | unumf_closeResult(UFormattedNumber* uresult); |
| 667 | |
| 668 | |
| 669 | #if U_SHOW_CPLUSPLUS_API |
| 670 | U_NAMESPACE_BEGIN |
| 671 | |
| 672 | /** |
| 673 | * \class LocalUNumberFormatterPointer |
| 674 | * "Smart pointer" class; closes a UNumberFormatter via unumf_close(). |
| 675 | * For most methods see the LocalPointerBase base class. |
| 676 | * |
| 677 | * Usage: |
| 678 | * <pre> |
| 679 | * LocalUNumberFormatterPointer uformatter(unumf_openForSkeletonAndLocale(...)); |
| 680 | * // no need to explicitly call unumf_close() |
| 681 | * </pre> |
| 682 | * |
| 683 | * @see LocalPointerBase |
| 684 | * @see LocalPointer |
| 685 | * @stable ICU 62 |
| 686 | */ |
| 687 | U_DEFINE_LOCAL_OPEN_POINTER(LocalUNumberFormatterPointer, UNumberFormatter, unumf_close); |
| 688 | |
| 689 | /** |
| 690 | * \class LocalUFormattedNumberPointer |
| 691 | * "Smart pointer" class; closes a UFormattedNumber via unumf_closeResult(). |
| 692 | * For most methods see the LocalPointerBase base class. |
| 693 | * |
| 694 | * Usage: |
| 695 | * <pre> |
| 696 | * LocalUFormattedNumberPointer uformatter(unumf_openResult(...)); |
| 697 | * // no need to explicitly call unumf_closeResult() |
| 698 | * </pre> |
| 699 | * |
| 700 | * @see LocalPointerBase |
| 701 | * @see LocalPointer |
| 702 | * @stable ICU 62 |
| 703 | */ |
| 704 | U_DEFINE_LOCAL_OPEN_POINTER(LocalUFormattedNumberPointer, UFormattedNumber, unumf_closeResult); |
| 705 | |
| 706 | U_NAMESPACE_END |
| 707 | #endif // U_SHOW_CPLUSPLUS_API |
| 708 | |
| 709 | #endif //__UNUMBERFORMATTER_H__ |
| 710 | #endif /* #if !UCONFIG_NO_FORMATTING */ |
| 711 | |