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/unumberoptions.h" 13 #include "unicode/uformattednumber.h" 14 15 16 /** 17 * \file 18 * \brief C API: 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 resolve conflicts between maximum fraction digits and maximum 82 * significant digits. 83 * 84 * There are two modes, RELAXED and STRICT: 85 * 86 * - RELAXED: Relax one of the two constraints (fraction digits or significant digits) in order 87 * to round the number to a higher level of precision. 88 * - STRICT: Enforce both constraints, resulting in the number being rounded to a lower 89 * level of precision. 90 * 91 * The default settings for compact notation rounding are Max-Fraction = 0 (round to the nearest 92 * integer), Max-Significant = 2 (round to 2 significant digits), and priority RELAXED (choose 93 * the constraint that results in more digits being displayed). 94 * 95 * Conflicting *minimum* fraction and significant digits are always resolved in the direction that 96 * results in more trailing zeros. 97 * 98 * Example 1: Consider the number 3.141, with various different settings: 99 * 100 * - Max-Fraction = 1: "3.1" 101 * - Max-Significant = 3: "3.14" 102 * 103 * The rounding priority determines how to resolve the conflict when both Max-Fraction and 104 * Max-Significant are set. With RELAXED, the less-strict setting (the one that causes more digits 105 * to be displayed) will be used; Max-Significant wins. With STRICT, the more-strict setting (the 106 * one that causes fewer digits to be displayed) will be used; Max-Fraction wins. 107 * 108 * Example 2: Consider the number 8317, with various different settings: 109 * 110 * - Max-Fraction = 1: "8317" 111 * - Max-Significant = 3: "8320" 112 * 113 * Here, RELAXED favors Max-Fraction and STRICT favors Max-Significant. Note that this larger 114 * number caused the two modes to favor the opposite result. 115 * 116 * @stable ICU 69 117 */ 118 typedef enum UNumberRoundingPriority { 119 /** 120 * Favor greater precision by relaxing one of the rounding constraints. 121 * 122 * @stable ICU 69 123 */ 124 UNUM_ROUNDING_PRIORITY_RELAXED, 125 126 /** 127 * Favor adherence to all rounding constraints by producing lower precision. 128 * 129 * @stable ICU 69 130 */ 131 UNUM_ROUNDING_PRIORITY_STRICT, 132 } UNumberRoundingPriority; 133 134 /** 135 * An enum declaring how to render units, including currencies. Example outputs when formatting 123 USD and 123 136 * meters in <em>en-CA</em>: 137 * 138 * <p> 139 * <ul> 140 * <li>NARROW*: "$123.00" and "123 m" 141 * <li>SHORT: "US$ 123.00" and "123 m" 142 * <li>FULL_NAME: "123.00 US dollars" and "123 meters" 143 * <li>ISO_CODE: "USD 123.00" and undefined behavior 144 * <li>HIDDEN: "123.00" and "123" 145 * </ul> 146 * 147 * <p> 148 * This enum is similar to {@link UMeasureFormatWidth}. 149 * 150 * @stable ICU 60 151 */ 152 typedef enum UNumberUnitWidth { 153 /** 154 * Print an abbreviated version of the unit name. Similar to SHORT, but always use the shortest available 155 * abbreviation or symbol. This option can be used when the context hints at the identity of the unit. For more 156 * information on the difference between NARROW and SHORT, see SHORT. 157 * 158 * <p> 159 * In CLDR, this option corresponds to the "Narrow" format for measure units and the "¤¤¤¤¤" placeholder for 160 * currencies. 161 * 162 * @stable ICU 60 163 */ 164 UNUM_UNIT_WIDTH_NARROW = 0, 165 166 /** 167 * Print an abbreviated version of the unit name. Similar to NARROW, but use a slightly wider abbreviation or 168 * symbol when there may be ambiguity. This is the default behavior. 169 * 170 * <p> 171 * For example, in <em>es-US</em>, the SHORT form for Fahrenheit is "{0} °F", but the NARROW form is "{0}°", 172 * since Fahrenheit is the customary unit for temperature in that locale. 173 * 174 * <p> 175 * In CLDR, this option corresponds to the "Short" format for measure units and the "¤" placeholder for 176 * currencies. 177 * 178 * @stable ICU 60 179 */ 180 UNUM_UNIT_WIDTH_SHORT = 1, 181 182 /** 183 * Print the full name of the unit, without any abbreviations. 184 * 185 * <p> 186 * In CLDR, this option corresponds to the default format for measure units and the "¤¤¤" placeholder for 187 * currencies. 188 * 189 * @stable ICU 60 190 */ 191 UNUM_UNIT_WIDTH_FULL_NAME = 2, 192 193 /** 194 * Use the three-digit ISO XXX code in place of the symbol for displaying currencies. The behavior of this 195 * option is currently undefined for use with measure units. 196 * 197 * <p> 198 * In CLDR, this option corresponds to the "¤¤" placeholder for currencies. 199 * 200 * @stable ICU 60 201 */ 202 UNUM_UNIT_WIDTH_ISO_CODE = 3, 203 204 /** 205 * Use the formal variant of the currency symbol; for example, "NT$" for the New Taiwan 206 * dollar in zh-TW. 207 * 208 * <p> 209 * Behavior of this option with non-currency units is not defined at this time. 210 * 211 * @stable ICU 68 212 */ 213 UNUM_UNIT_WIDTH_FORMAL = 4, 214 215 /** 216 * Use the alternate variant of the currency symbol; for example, "TL" for the Turkish 217 * lira (TRY). 218 * 219 * <p> 220 * Behavior of this option with non-currency units is not defined at this time. 221 * 222 * @stable ICU 68 223 */ 224 UNUM_UNIT_WIDTH_VARIANT = 5, 225 226 /** 227 * Format the number according to the specified unit, but do not display the unit. For currencies, apply 228 * monetary symbols and formats as with SHORT, but omit the currency symbol. For measure units, the behavior is 229 * equivalent to not specifying the unit at all. 230 * 231 * @stable ICU 60 232 */ 233 UNUM_UNIT_WIDTH_HIDDEN = 6, 234 235 // Do not conditionalize the following with #ifndef U_HIDE_INTERNAL_API, 236 // needed for unconditionalized struct MacroProps 237 /** 238 * One more than the highest UNumberUnitWidth value. 239 * 240 * @internal ICU 60: The numeric value may change over time; see ICU ticket #12420. 241 */ 242 UNUM_UNIT_WIDTH_COUNT = 7 243 } UNumberUnitWidth; 244 245 /** 246 * An enum declaring how to denote positive and negative numbers. Example outputs when formatting 247 * 123, 0, and -123 in <em>en-US</em>: 248 * 249 * <ul> 250 * <li>AUTO: "123", "0", and "-123" 251 * <li>ALWAYS: "+123", "+0", and "-123" 252 * <li>NEVER: "123", "0", and "123" 253 * <li>ACCOUNTING: "$123", "$0", and "($123)" 254 * <li>ACCOUNTING_ALWAYS: "+$123", "+$0", and "($123)" 255 * <li>EXCEPT_ZERO: "+123", "0", and "-123" 256 * <li>ACCOUNTING_EXCEPT_ZERO: "+$123", "$0", and "($123)" 257 * </ul> 258 * 259 * <p> 260 * The exact format, including the position and the code point of the sign, differ by locale. 261 * 262 * @stable ICU 60 263 */ 264 typedef enum UNumberSignDisplay { 265 /** 266 * Show the minus sign on negative numbers, and do not show the sign on positive numbers. This is the default 267 * behavior. 268 * 269 * If using this option, a sign will be displayed on negative zero, including negative numbers 270 * that round to zero. To hide the sign on negative zero, use the NEGATIVE option. 271 * 272 * @stable ICU 60 273 */ 274 UNUM_SIGN_AUTO, 275 276 /** 277 * Show the minus sign on negative numbers and the plus sign on positive numbers, including zero. 278 * To hide the sign on zero, see {@link UNUM_SIGN_EXCEPT_ZERO}. 279 * 280 * @stable ICU 60 281 */ 282 UNUM_SIGN_ALWAYS, 283 284 /** 285 * Do not show the sign on positive or negative numbers. 286 * 287 * @stable ICU 60 288 */ 289 UNUM_SIGN_NEVER, 290 291 /** 292 * Use the locale-dependent accounting format on negative numbers, and do not show the sign on positive numbers. 293 * 294 * <p> 295 * The accounting format is defined in CLDR and varies by locale; in many Western locales, the format is a pair 296 * of parentheses around the number. 297 * 298 * <p> 299 * Note: Since CLDR defines the accounting format in the monetary context only, this option falls back to the 300 * AUTO sign display strategy when formatting without a currency unit. This limitation may be lifted in the 301 * future. 302 * 303 * @stable ICU 60 304 */ 305 UNUM_SIGN_ACCOUNTING, 306 307 /** 308 * Use the locale-dependent accounting format on negative numbers, and show the plus sign on 309 * positive numbers, including zero. For more information on the accounting format, see the 310 * ACCOUNTING sign display strategy. To hide the sign on zero, see 311 * {@link UNUM_SIGN_ACCOUNTING_EXCEPT_ZERO}. 312 * 313 * @stable ICU 60 314 */ 315 UNUM_SIGN_ACCOUNTING_ALWAYS, 316 317 /** 318 * Show the minus sign on negative numbers and the plus sign on positive numbers. Do not show a 319 * sign on zero, numbers that round to zero, or NaN. 320 * 321 * @stable ICU 61 322 */ 323 UNUM_SIGN_EXCEPT_ZERO, 324 325 /** 326 * Use the locale-dependent accounting format on negative numbers, and show the plus sign on 327 * positive numbers. Do not show a sign on zero, numbers that round to zero, or NaN. For more 328 * information on the accounting format, see the ACCOUNTING sign display strategy. 329 * 330 * @stable ICU 61 331 */ 332 UNUM_SIGN_ACCOUNTING_EXCEPT_ZERO, 333 334 /** 335 * Same as AUTO, but do not show the sign on negative zero. 336 * 337 * @stable ICU 69 338 */ 339 UNUM_SIGN_NEGATIVE, 340 341 /** 342 * Same as ACCOUNTING, but do not show the sign on negative zero. 343 * 344 * @stable ICU 69 345 */ 346 UNUM_SIGN_ACCOUNTING_NEGATIVE, 347 348 // Do not conditionalize the following with #ifndef U_HIDE_INTERNAL_API, 349 // needed for unconditionalized struct MacroProps 350 /** 351 * One more than the highest UNumberSignDisplay value. 352 * 353 * @internal ICU 60: The numeric value may change over time; see ICU ticket #12420. 354 */ 355 UNUM_SIGN_COUNT = 9, 356 } UNumberSignDisplay; 357 358 /** 359 * An enum declaring how to render the decimal separator. 360 * 361 * <p> 362 * <ul> 363 * <li>UNUM_DECIMAL_SEPARATOR_AUTO: "1", "1.1" 364 * <li>UNUM_DECIMAL_SEPARATOR_ALWAYS: "1.", "1.1" 365 * </ul> 366 * 367 * @stable ICU 60 368 */ 369 typedef enum UNumberDecimalSeparatorDisplay { 370 /** 371 * Show the decimal separator when there are one or more digits to display after the separator, and do not show 372 * it otherwise. This is the default behavior. 373 * 374 * @stable ICU 60 375 */ 376 UNUM_DECIMAL_SEPARATOR_AUTO, 377 378 /** 379 * Always show the decimal separator, even if there are no digits to display after the separator. 380 * 381 * @stable ICU 60 382 */ 383 UNUM_DECIMAL_SEPARATOR_ALWAYS, 384 385 // Do not conditionalize the following with #ifndef U_HIDE_INTERNAL_API, 386 // needed for unconditionalized struct MacroProps 387 /** 388 * One more than the highest UNumberDecimalSeparatorDisplay value. 389 * 390 * @internal ICU 60: The numeric value may change over time; see ICU ticket #12420. 391 */ 392 UNUM_DECIMAL_SEPARATOR_COUNT 393 } UNumberDecimalSeparatorDisplay; 394 395 /** 396 * An enum declaring how to render trailing zeros. 397 * 398 * - UNUM_TRAILING_ZERO_AUTO: 0.90, 1.00, 1.10 399 * - UNUM_TRAILING_ZERO_HIDE_IF_WHOLE: 0.90, 1, 1.10 400 * 401 * @stable ICU 69 402 */ 403 typedef enum UNumberTrailingZeroDisplay { 404 /** 405 * Display trailing zeros according to the settings for minimum fraction and significant digits. 406 * 407 * @stable ICU 69 408 */ 409 UNUM_TRAILING_ZERO_AUTO, 410 411 /** 412 * Same as AUTO, but hide trailing zeros after the decimal separator if they are all zero. 413 * 414 * @stable ICU 69 415 */ 416 UNUM_TRAILING_ZERO_HIDE_IF_WHOLE, 417 } UNumberTrailingZeroDisplay; 418 419 struct UNumberFormatter; 420 /** 421 * C-compatible version of icu::number::LocalizedNumberFormatter. 422 * 423 * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead. 424 * 425 * @stable ICU 62 426 */ 427 typedef struct UNumberFormatter UNumberFormatter; 428 429 430 /** 431 * Creates a new UNumberFormatter for the given skeleton string and locale. This is currently the only 432 * method for creating a new UNumberFormatter. 433 * 434 * Objects of type UNumberFormatter returned by this method are threadsafe. 435 * 436 * For more details on skeleton strings, see the documentation in numberformatter.h. For more details on 437 * the usage of this API, see the documentation at the top of unumberformatter.h. 438 * 439 * For more information on number skeleton strings, see: 440 * https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html 441 * 442 * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead. 443 * 444 * @param skeleton The skeleton string, like u"percent precision-integer" 445 * @param skeletonLen The number of UChars in the skeleton string, or -1 if it is NUL-terminated. 446 * @param locale The NUL-terminated locale ID. 447 * @param ec Set if an error occurs. 448 * @stable ICU 62 449 */ 450 U_CAPI UNumberFormatter* U_EXPORT2 451 unumf_openForSkeletonAndLocale(const UChar* skeleton, int32_t skeletonLen, const char* locale, 452 UErrorCode* ec); 453 454 455 /** 456 * Like unumf_openForSkeletonAndLocale, but accepts a UParseError, which will be populated with the 457 * location of a skeleton syntax error if such a syntax error exists. 458 * 459 * For more information on number skeleton strings, see: 460 * https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html 461 * 462 * @param skeleton The skeleton string, like u"percent precision-integer" 463 * @param skeletonLen The number of UChars in the skeleton string, or -1 if it is NUL-terminated. 464 * @param locale The NUL-terminated locale ID. 465 * @param perror A parse error struct populated if an error occurs when parsing. Can be NULL. 466 * If no error occurs, perror->offset will be set to -1. 467 * @param ec Set if an error occurs. 468 * @stable ICU 64 469 */ 470 U_CAPI UNumberFormatter* U_EXPORT2 471 unumf_openForSkeletonAndLocaleWithError( 472 const UChar* skeleton, int32_t skeletonLen, const char* locale, UParseError* perror, UErrorCode* ec); 473 474 475 476 /** 477 * Uses a UNumberFormatter to format an integer to a UFormattedNumber. A string, field position, and other 478 * information can be retrieved from the UFormattedNumber. 479 * 480 * The UNumberFormatter can be shared between threads. Each thread should have its own local 481 * UFormattedNumber, however, for storing the result of the formatting operation. 482 * 483 * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead. 484 * 485 * @param uformatter A formatter object created by unumf_openForSkeletonAndLocale or similar. 486 * @param value The number to be formatted. 487 * @param uresult The object that will be mutated to store the result; see unumf_openResult. 488 * @param ec Set if an error occurs. 489 * @stable ICU 62 490 */ 491 U_CAPI void U_EXPORT2 492 unumf_formatInt(const UNumberFormatter* uformatter, int64_t value, UFormattedNumber* uresult, 493 UErrorCode* ec); 494 495 496 /** 497 * Uses a UNumberFormatter to format a double to a UFormattedNumber. A string, field position, and other 498 * information can be retrieved from the UFormattedNumber. 499 * 500 * The UNumberFormatter can be shared between threads. Each thread should have its own local 501 * UFormattedNumber, however, for storing the result of the formatting operation. 502 * 503 * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead. 504 * 505 * @param uformatter A formatter object created by unumf_openForSkeletonAndLocale or similar. 506 * @param value The number to be formatted. 507 * @param uresult The object that will be mutated to store the result; see unumf_openResult. 508 * @param ec Set if an error occurs. 509 * @stable ICU 62 510 */ 511 U_CAPI void U_EXPORT2 512 unumf_formatDouble(const UNumberFormatter* uformatter, double value, UFormattedNumber* uresult, 513 UErrorCode* ec); 514 515 516 /** 517 * Uses a UNumberFormatter to format a decimal number to a UFormattedNumber. A string, field position, and 518 * other information can be retrieved from the UFormattedNumber. 519 * 520 * The UNumberFormatter can be shared between threads. Each thread should have its own local 521 * UFormattedNumber, however, for storing the result of the formatting operation. 522 * 523 * The syntax of the unformatted number is a "numeric string" as defined in the Decimal Arithmetic 524 * Specification, available at http://speleotrove.com/decimal 525 * 526 * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead. 527 * 528 * @param uformatter A formatter object created by unumf_openForSkeletonAndLocale or similar. 529 * @param value The numeric string to be formatted. 530 * @param valueLen The length of the numeric string, or -1 if it is NUL-terminated. 531 * @param uresult The object that will be mutated to store the result; see unumf_openResult. 532 * @param ec Set if an error occurs. 533 * @stable ICU 62 534 */ 535 U_CAPI void U_EXPORT2 536 unumf_formatDecimal(const UNumberFormatter* uformatter, const char* value, int32_t valueLen, 537 UFormattedNumber* uresult, UErrorCode* ec); 538 539 540 541 /** 542 * Releases the UNumberFormatter created by unumf_openForSkeletonAndLocale(). 543 * 544 * @param uformatter An object created by unumf_openForSkeletonAndLocale(). 545 * @stable ICU 62 546 */ 547 U_CAPI void U_EXPORT2 548 unumf_close(UNumberFormatter* uformatter); 549 550 551 552 #if U_SHOW_CPLUSPLUS_API 553 U_NAMESPACE_BEGIN 554 555 /** 556 * \class LocalUNumberFormatterPointer 557 * "Smart pointer" class; closes a UNumberFormatter via unumf_close(). 558 * For most methods see the LocalPointerBase base class. 559 * 560 * Usage: 561 * <pre> 562 * LocalUNumberFormatterPointer uformatter(unumf_openForSkeletonAndLocale(...)); 563 * // no need to explicitly call unumf_close() 564 * </pre> 565 * 566 * @see LocalPointerBase 567 * @see LocalPointer 568 * @stable ICU 62 569 */ 570 U_DEFINE_LOCAL_OPEN_POINTER(LocalUNumberFormatterPointer, UNumberFormatter, unumf_close); 571 572 U_NAMESPACE_END 573 #endif // U_SHOW_CPLUSPLUS_API 574 575 #endif /* #if !UCONFIG_NO_FORMATTING */ 576 #endif //__UNUMBERFORMATTER_H__ 577