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