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