|
||||
File indexing completed on 2025-01-18 10:13:13
0001 // © 2018 and later: Unicode, Inc. and others. 0002 // License & terms of use: http://www.unicode.org/copyright.html 0003 0004 #ifndef __UNUMBERFORMATTER_H__ 0005 #define __UNUMBERFORMATTER_H__ 0006 0007 #include "unicode/utypes.h" 0008 0009 #if !UCONFIG_NO_FORMATTING 0010 0011 #include "unicode/parseerr.h" 0012 #include "unicode/unumberoptions.h" 0013 #include "unicode/uformattednumber.h" 0014 0015 0016 /** 0017 * \file 0018 * \brief C API: Localized number formatting; not recommended for C++. 0019 * 0020 * This is the C-compatible version of the NumberFormatter API introduced in ICU 60. C++ users should 0021 * include unicode/numberformatter.h and use the proper C++ APIs. 0022 * 0023 * The C API accepts a number skeleton string for specifying the settings for formatting, which covers a 0024 * very large subset of all possible number formatting features. For more information on number skeleton 0025 * strings, see unicode/numberformatter.h. 0026 * 0027 * When using UNumberFormatter, which is treated as immutable, the results are exported to a mutable 0028 * UFormattedNumber object, which you subsequently use for populating your string buffer or iterating over 0029 * the fields. 0030 * 0031 * Example code: 0032 * <pre> 0033 * // Setup: 0034 * UErrorCode ec = U_ZERO_ERROR; 0035 * UNumberFormatter* uformatter = unumf_openForSkeletonAndLocale(u"precision-integer", -1, "en", &ec); 0036 * UFormattedNumber* uresult = unumf_openResult(&ec); 0037 * if (U_FAILURE(ec)) { return; } 0038 * 0039 * // Format a double: 0040 * unumf_formatDouble(uformatter, 5142.3, uresult, &ec); 0041 * if (U_FAILURE(ec)) { return; } 0042 * 0043 * // Export the string to a malloc'd buffer: 0044 * int32_t len = unumf_resultToString(uresult, NULL, 0, &ec); 0045 * // at this point, ec == U_BUFFER_OVERFLOW_ERROR 0046 * ec = U_ZERO_ERROR; 0047 * UChar* buffer = (UChar*) malloc((len+1)*sizeof(UChar)); 0048 * unumf_resultToString(uresult, buffer, len+1, &ec); 0049 * if (U_FAILURE(ec)) { return; } 0050 * // buffer should equal "5,142" 0051 * 0052 * // Cleanup: 0053 * unumf_close(uformatter); 0054 * unumf_closeResult(uresult); 0055 * free(buffer); 0056 * </pre> 0057 * 0058 * If you are a C++ user linking against the C libraries, you can use the LocalPointer versions of these 0059 * APIs. The following example uses LocalPointer with the decimal number and field position APIs: 0060 * 0061 * <pre> 0062 * // Setup: 0063 * LocalUNumberFormatterPointer uformatter(unumf_openForSkeletonAndLocale(u"percent", -1, "en", &ec)); 0064 * LocalUFormattedNumberPointer uresult(unumf_openResult(&ec)); 0065 * if (U_FAILURE(ec)) { return; } 0066 * 0067 * // Format a decimal number: 0068 * unumf_formatDecimal(uformatter.getAlias(), "9.87E-3", -1, uresult.getAlias(), &ec); 0069 * if (U_FAILURE(ec)) { return; } 0070 * 0071 * // Get the location of the percent sign: 0072 * UFieldPosition ufpos = {UNUM_PERCENT_FIELD, 0, 0}; 0073 * unumf_resultNextFieldPosition(uresult.getAlias(), &ufpos, &ec); 0074 * // ufpos should contain beginIndex=7 and endIndex=8 since the string is "0.00987%" 0075 * 0076 * // No need to do any cleanup since we are using LocalPointer. 0077 * </pre> 0078 */ 0079 0080 /** 0081 * An enum declaring how to resolve conflicts between maximum fraction digits and maximum 0082 * significant digits. 0083 * 0084 * There are two modes, RELAXED and STRICT: 0085 * 0086 * - RELAXED: Relax one of the two constraints (fraction digits or significant digits) in order 0087 * to round the number to a higher level of precision. 0088 * - STRICT: Enforce both constraints, resulting in the number being rounded to a lower 0089 * level of precision. 0090 * 0091 * The default settings for compact notation rounding are Max-Fraction = 0 (round to the nearest 0092 * integer), Max-Significant = 2 (round to 2 significant digits), and priority RELAXED (choose 0093 * the constraint that results in more digits being displayed). 0094 * 0095 * Conflicting *minimum* fraction and significant digits are always resolved in the direction that 0096 * results in more trailing zeros. 0097 * 0098 * Example 1: Consider the number 3.141, with various different settings: 0099 * 0100 * - Max-Fraction = 1: "3.1" 0101 * - Max-Significant = 3: "3.14" 0102 * 0103 * The rounding priority determines how to resolve the conflict when both Max-Fraction and 0104 * Max-Significant are set. With RELAXED, the less-strict setting (the one that causes more digits 0105 * to be displayed) will be used; Max-Significant wins. With STRICT, the more-strict setting (the 0106 * one that causes fewer digits to be displayed) will be used; Max-Fraction wins. 0107 * 0108 * Example 2: Consider the number 8317, with various different settings: 0109 * 0110 * - Max-Fraction = 1: "8317" 0111 * - Max-Significant = 3: "8320" 0112 * 0113 * Here, RELAXED favors Max-Fraction and STRICT favors Max-Significant. Note that this larger 0114 * number caused the two modes to favor the opposite result. 0115 * 0116 * @stable ICU 69 0117 */ 0118 typedef enum UNumberRoundingPriority { 0119 /** 0120 * Favor greater precision by relaxing one of the rounding constraints. 0121 * 0122 * @stable ICU 69 0123 */ 0124 UNUM_ROUNDING_PRIORITY_RELAXED, 0125 0126 /** 0127 * Favor adherence to all rounding constraints by producing lower precision. 0128 * 0129 * @stable ICU 69 0130 */ 0131 UNUM_ROUNDING_PRIORITY_STRICT, 0132 } UNumberRoundingPriority; 0133 0134 /** 0135 * An enum declaring how to render units, including currencies. Example outputs when formatting 123 USD and 123 0136 * meters in <em>en-CA</em>: 0137 * 0138 * <p> 0139 * <ul> 0140 * <li>NARROW*: "$123.00" and "123 m" 0141 * <li>SHORT: "US$ 123.00" and "123 m" 0142 * <li>FULL_NAME: "123.00 US dollars" and "123 meters" 0143 * <li>ISO_CODE: "USD 123.00" and undefined behavior 0144 * <li>HIDDEN: "123.00" and "123" 0145 * </ul> 0146 * 0147 * <p> 0148 * This enum is similar to {@link UMeasureFormatWidth}. 0149 * 0150 * @stable ICU 60 0151 */ 0152 typedef enum UNumberUnitWidth { 0153 /** 0154 * Print an abbreviated version of the unit name. Similar to SHORT, but always use the shortest available 0155 * abbreviation or symbol. This option can be used when the context hints at the identity of the unit. For more 0156 * information on the difference between NARROW and SHORT, see SHORT. 0157 * 0158 * <p> 0159 * In CLDR, this option corresponds to the "Narrow" format for measure units and the "¤¤¤¤¤" placeholder for 0160 * currencies. 0161 * 0162 * @stable ICU 60 0163 */ 0164 UNUM_UNIT_WIDTH_NARROW = 0, 0165 0166 /** 0167 * Print an abbreviated version of the unit name. Similar to NARROW, but use a slightly wider abbreviation or 0168 * symbol when there may be ambiguity. This is the default behavior. 0169 * 0170 * <p> 0171 * For example, in <em>es-US</em>, the SHORT form for Fahrenheit is "{0} °F", but the NARROW form is "{0}°", 0172 * since Fahrenheit is the customary unit for temperature in that locale. 0173 * 0174 * <p> 0175 * In CLDR, this option corresponds to the "Short" format for measure units and the "¤" placeholder for 0176 * currencies. 0177 * 0178 * @stable ICU 60 0179 */ 0180 UNUM_UNIT_WIDTH_SHORT = 1, 0181 0182 /** 0183 * Print the full name of the unit, without any abbreviations. 0184 * 0185 * <p> 0186 * In CLDR, this option corresponds to the default format for measure units and the "¤¤¤" placeholder for 0187 * currencies. 0188 * 0189 * @stable ICU 60 0190 */ 0191 UNUM_UNIT_WIDTH_FULL_NAME = 2, 0192 0193 /** 0194 * Use the three-digit ISO XXX code in place of the symbol for displaying currencies. The behavior of this 0195 * option is currently undefined for use with measure units. 0196 * 0197 * <p> 0198 * In CLDR, this option corresponds to the "¤¤" placeholder for currencies. 0199 * 0200 * @stable ICU 60 0201 */ 0202 UNUM_UNIT_WIDTH_ISO_CODE = 3, 0203 0204 /** 0205 * Use the formal variant of the currency symbol; for example, "NT$" for the New Taiwan 0206 * dollar in zh-TW. 0207 * 0208 * <p> 0209 * Behavior of this option with non-currency units is not defined at this time. 0210 * 0211 * @stable ICU 68 0212 */ 0213 UNUM_UNIT_WIDTH_FORMAL = 4, 0214 0215 /** 0216 * Use the alternate variant of the currency symbol; for example, "TL" for the Turkish 0217 * lira (TRY). 0218 * 0219 * <p> 0220 * Behavior of this option with non-currency units is not defined at this time. 0221 * 0222 * @stable ICU 68 0223 */ 0224 UNUM_UNIT_WIDTH_VARIANT = 5, 0225 0226 /** 0227 * Format the number according to the specified unit, but do not display the unit. For currencies, apply 0228 * monetary symbols and formats as with SHORT, but omit the currency symbol. For measure units, the behavior is 0229 * equivalent to not specifying the unit at all. 0230 * 0231 * @stable ICU 60 0232 */ 0233 UNUM_UNIT_WIDTH_HIDDEN = 6, 0234 0235 // Do not conditionalize the following with #ifndef U_HIDE_INTERNAL_API, 0236 // needed for unconditionalized struct MacroProps 0237 /** 0238 * One more than the highest UNumberUnitWidth value. 0239 * 0240 * @internal ICU 60: The numeric value may change over time; see ICU ticket #12420. 0241 */ 0242 UNUM_UNIT_WIDTH_COUNT = 7 0243 } UNumberUnitWidth; 0244 0245 /** 0246 * An enum declaring how to denote positive and negative numbers. Example outputs when formatting 0247 * 123, 0, and -123 in <em>en-US</em>: 0248 * 0249 * <ul> 0250 * <li>AUTO: "123", "0", and "-123" 0251 * <li>ALWAYS: "+123", "+0", and "-123" 0252 * <li>NEVER: "123", "0", and "123" 0253 * <li>ACCOUNTING: "$123", "$0", and "($123)" 0254 * <li>ACCOUNTING_ALWAYS: "+$123", "+$0", and "($123)" 0255 * <li>EXCEPT_ZERO: "+123", "0", and "-123" 0256 * <li>ACCOUNTING_EXCEPT_ZERO: "+$123", "$0", and "($123)" 0257 * </ul> 0258 * 0259 * <p> 0260 * The exact format, including the position and the code point of the sign, differ by locale. 0261 * 0262 * @stable ICU 60 0263 */ 0264 typedef enum UNumberSignDisplay { 0265 /** 0266 * Show the minus sign on negative numbers, and do not show the sign on positive numbers. This is the default 0267 * behavior. 0268 * 0269 * If using this option, a sign will be displayed on negative zero, including negative numbers 0270 * that round to zero. To hide the sign on negative zero, use the NEGATIVE option. 0271 * 0272 * @stable ICU 60 0273 */ 0274 UNUM_SIGN_AUTO, 0275 0276 /** 0277 * Show the minus sign on negative numbers and the plus sign on positive numbers, including zero. 0278 * To hide the sign on zero, see {@link UNUM_SIGN_EXCEPT_ZERO}. 0279 * 0280 * @stable ICU 60 0281 */ 0282 UNUM_SIGN_ALWAYS, 0283 0284 /** 0285 * Do not show the sign on positive or negative numbers. 0286 * 0287 * @stable ICU 60 0288 */ 0289 UNUM_SIGN_NEVER, 0290 0291 /** 0292 * Use the locale-dependent accounting format on negative numbers, and do not show the sign on positive numbers. 0293 * 0294 * <p> 0295 * The accounting format is defined in CLDR and varies by locale; in many Western locales, the format is a pair 0296 * of parentheses around the number. 0297 * 0298 * <p> 0299 * Note: Since CLDR defines the accounting format in the monetary context only, this option falls back to the 0300 * AUTO sign display strategy when formatting without a currency unit. This limitation may be lifted in the 0301 * future. 0302 * 0303 * @stable ICU 60 0304 */ 0305 UNUM_SIGN_ACCOUNTING, 0306 0307 /** 0308 * Use the locale-dependent accounting format on negative numbers, and show the plus sign on 0309 * positive numbers, including zero. For more information on the accounting format, see the 0310 * ACCOUNTING sign display strategy. To hide the sign on zero, see 0311 * {@link UNUM_SIGN_ACCOUNTING_EXCEPT_ZERO}. 0312 * 0313 * @stable ICU 60 0314 */ 0315 UNUM_SIGN_ACCOUNTING_ALWAYS, 0316 0317 /** 0318 * Show the minus sign on negative numbers and the plus sign on positive numbers. Do not show a 0319 * sign on zero, numbers that round to zero, or NaN. 0320 * 0321 * @stable ICU 61 0322 */ 0323 UNUM_SIGN_EXCEPT_ZERO, 0324 0325 /** 0326 * Use the locale-dependent accounting format on negative numbers, and show the plus sign on 0327 * positive numbers. Do not show a sign on zero, numbers that round to zero, or NaN. For more 0328 * information on the accounting format, see the ACCOUNTING sign display strategy. 0329 * 0330 * @stable ICU 61 0331 */ 0332 UNUM_SIGN_ACCOUNTING_EXCEPT_ZERO, 0333 0334 /** 0335 * Same as AUTO, but do not show the sign on negative zero. 0336 * 0337 * @stable ICU 69 0338 */ 0339 UNUM_SIGN_NEGATIVE, 0340 0341 /** 0342 * Same as ACCOUNTING, but do not show the sign on negative zero. 0343 * 0344 * @stable ICU 69 0345 */ 0346 UNUM_SIGN_ACCOUNTING_NEGATIVE, 0347 0348 // Do not conditionalize the following with #ifndef U_HIDE_INTERNAL_API, 0349 // needed for unconditionalized struct MacroProps 0350 /** 0351 * One more than the highest UNumberSignDisplay value. 0352 * 0353 * @internal ICU 60: The numeric value may change over time; see ICU ticket #12420. 0354 */ 0355 UNUM_SIGN_COUNT = 9, 0356 } UNumberSignDisplay; 0357 0358 /** 0359 * An enum declaring how to render the decimal separator. 0360 * 0361 * <p> 0362 * <ul> 0363 * <li>UNUM_DECIMAL_SEPARATOR_AUTO: "1", "1.1" 0364 * <li>UNUM_DECIMAL_SEPARATOR_ALWAYS: "1.", "1.1" 0365 * </ul> 0366 * 0367 * @stable ICU 60 0368 */ 0369 typedef enum UNumberDecimalSeparatorDisplay { 0370 /** 0371 * Show the decimal separator when there are one or more digits to display after the separator, and do not show 0372 * it otherwise. This is the default behavior. 0373 * 0374 * @stable ICU 60 0375 */ 0376 UNUM_DECIMAL_SEPARATOR_AUTO, 0377 0378 /** 0379 * Always show the decimal separator, even if there are no digits to display after the separator. 0380 * 0381 * @stable ICU 60 0382 */ 0383 UNUM_DECIMAL_SEPARATOR_ALWAYS, 0384 0385 // Do not conditionalize the following with #ifndef U_HIDE_INTERNAL_API, 0386 // needed for unconditionalized struct MacroProps 0387 /** 0388 * One more than the highest UNumberDecimalSeparatorDisplay value. 0389 * 0390 * @internal ICU 60: The numeric value may change over time; see ICU ticket #12420. 0391 */ 0392 UNUM_DECIMAL_SEPARATOR_COUNT 0393 } UNumberDecimalSeparatorDisplay; 0394 0395 /** 0396 * An enum declaring how to render trailing zeros. 0397 * 0398 * - UNUM_TRAILING_ZERO_AUTO: 0.90, 1.00, 1.10 0399 * - UNUM_TRAILING_ZERO_HIDE_IF_WHOLE: 0.90, 1, 1.10 0400 * 0401 * @stable ICU 69 0402 */ 0403 typedef enum UNumberTrailingZeroDisplay { 0404 /** 0405 * Display trailing zeros according to the settings for minimum fraction and significant digits. 0406 * 0407 * @stable ICU 69 0408 */ 0409 UNUM_TRAILING_ZERO_AUTO, 0410 0411 /** 0412 * Same as AUTO, but hide trailing zeros after the decimal separator if they are all zero. 0413 * 0414 * @stable ICU 69 0415 */ 0416 UNUM_TRAILING_ZERO_HIDE_IF_WHOLE, 0417 } UNumberTrailingZeroDisplay; 0418 0419 struct UNumberFormatter; 0420 /** 0421 * C-compatible version of icu::number::LocalizedNumberFormatter. 0422 * 0423 * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead. 0424 * 0425 * @stable ICU 62 0426 */ 0427 typedef struct UNumberFormatter UNumberFormatter; 0428 0429 0430 /** 0431 * Creates a new UNumberFormatter for the given skeleton string and locale. This is currently the only 0432 * method for creating a new UNumberFormatter. 0433 * 0434 * Objects of type UNumberFormatter returned by this method are threadsafe. 0435 * 0436 * For more details on skeleton strings, see the documentation in numberformatter.h. For more details on 0437 * the usage of this API, see the documentation at the top of unumberformatter.h. 0438 * 0439 * For more information on number skeleton strings, see: 0440 * https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html 0441 * 0442 * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead. 0443 * 0444 * @param skeleton The skeleton string, like u"percent precision-integer" 0445 * @param skeletonLen The number of UChars in the skeleton string, or -1 if it is NUL-terminated. 0446 * @param locale The NUL-terminated locale ID. 0447 * @param ec Set if an error occurs. 0448 * @stable ICU 62 0449 */ 0450 U_CAPI UNumberFormatter* U_EXPORT2 0451 unumf_openForSkeletonAndLocale(const UChar* skeleton, int32_t skeletonLen, const char* locale, 0452 UErrorCode* ec); 0453 0454 0455 /** 0456 * Like unumf_openForSkeletonAndLocale, but accepts a UParseError, which will be populated with the 0457 * location of a skeleton syntax error if such a syntax error exists. 0458 * 0459 * For more information on number skeleton strings, see: 0460 * https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html 0461 * 0462 * @param skeleton The skeleton string, like u"percent precision-integer" 0463 * @param skeletonLen The number of UChars in the skeleton string, or -1 if it is NUL-terminated. 0464 * @param locale The NUL-terminated locale ID. 0465 * @param perror A parse error struct populated if an error occurs when parsing. Can be NULL. 0466 * If no error occurs, perror->offset will be set to -1. 0467 * @param ec Set if an error occurs. 0468 * @stable ICU 64 0469 */ 0470 U_CAPI UNumberFormatter* U_EXPORT2 0471 unumf_openForSkeletonAndLocaleWithError( 0472 const UChar* skeleton, int32_t skeletonLen, const char* locale, UParseError* perror, UErrorCode* ec); 0473 0474 0475 0476 /** 0477 * Uses a UNumberFormatter to format an integer to a UFormattedNumber. A string, field position, and other 0478 * information can be retrieved from the UFormattedNumber. 0479 * 0480 * The UNumberFormatter can be shared between threads. Each thread should have its own local 0481 * UFormattedNumber, however, for storing the result of the formatting operation. 0482 * 0483 * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead. 0484 * 0485 * @param uformatter A formatter object created by unumf_openForSkeletonAndLocale or similar. 0486 * @param value The number to be formatted. 0487 * @param uresult The object that will be mutated to store the result; see unumf_openResult. 0488 * @param ec Set if an error occurs. 0489 * @stable ICU 62 0490 */ 0491 U_CAPI void U_EXPORT2 0492 unumf_formatInt(const UNumberFormatter* uformatter, int64_t value, UFormattedNumber* uresult, 0493 UErrorCode* ec); 0494 0495 0496 /** 0497 * Uses a UNumberFormatter to format a double to a UFormattedNumber. A string, field position, and other 0498 * information can be retrieved from the UFormattedNumber. 0499 * 0500 * The UNumberFormatter can be shared between threads. Each thread should have its own local 0501 * UFormattedNumber, however, for storing the result of the formatting operation. 0502 * 0503 * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead. 0504 * 0505 * @param uformatter A formatter object created by unumf_openForSkeletonAndLocale or similar. 0506 * @param value The number to be formatted. 0507 * @param uresult The object that will be mutated to store the result; see unumf_openResult. 0508 * @param ec Set if an error occurs. 0509 * @stable ICU 62 0510 */ 0511 U_CAPI void U_EXPORT2 0512 unumf_formatDouble(const UNumberFormatter* uformatter, double value, UFormattedNumber* uresult, 0513 UErrorCode* ec); 0514 0515 0516 /** 0517 * Uses a UNumberFormatter to format a decimal number to a UFormattedNumber. A string, field position, and 0518 * other information can be retrieved from the UFormattedNumber. 0519 * 0520 * The UNumberFormatter can be shared between threads. Each thread should have its own local 0521 * UFormattedNumber, however, for storing the result of the formatting operation. 0522 * 0523 * The syntax of the unformatted number is a "numeric string" as defined in the Decimal Arithmetic 0524 * Specification, available at http://speleotrove.com/decimal 0525 * 0526 * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead. 0527 * 0528 * @param uformatter A formatter object created by unumf_openForSkeletonAndLocale or similar. 0529 * @param value The numeric string to be formatted. 0530 * @param valueLen The length of the numeric string, or -1 if it is NUL-terminated. 0531 * @param uresult The object that will be mutated to store the result; see unumf_openResult. 0532 * @param ec Set if an error occurs. 0533 * @stable ICU 62 0534 */ 0535 U_CAPI void U_EXPORT2 0536 unumf_formatDecimal(const UNumberFormatter* uformatter, const char* value, int32_t valueLen, 0537 UFormattedNumber* uresult, UErrorCode* ec); 0538 0539 0540 0541 /** 0542 * Releases the UNumberFormatter created by unumf_openForSkeletonAndLocale(). 0543 * 0544 * @param uformatter An object created by unumf_openForSkeletonAndLocale(). 0545 * @stable ICU 62 0546 */ 0547 U_CAPI void U_EXPORT2 0548 unumf_close(UNumberFormatter* uformatter); 0549 0550 0551 0552 #if U_SHOW_CPLUSPLUS_API 0553 U_NAMESPACE_BEGIN 0554 0555 /** 0556 * \class LocalUNumberFormatterPointer 0557 * "Smart pointer" class; closes a UNumberFormatter via unumf_close(). 0558 * For most methods see the LocalPointerBase base class. 0559 * 0560 * Usage: 0561 * <pre> 0562 * LocalUNumberFormatterPointer uformatter(unumf_openForSkeletonAndLocale(...)); 0563 * // no need to explicitly call unumf_close() 0564 * </pre> 0565 * 0566 * @see LocalPointerBase 0567 * @see LocalPointer 0568 * @stable ICU 62 0569 */ 0570 U_DEFINE_LOCAL_OPEN_POINTER(LocalUNumberFormatterPointer, UNumberFormatter, unumf_close); 0571 0572 U_NAMESPACE_END 0573 #endif // U_SHOW_CPLUSPLUS_API 0574 0575 #endif /* #if !UCONFIG_NO_FORMATTING */ 0576 #endif //__UNUMBERFORMATTER_H__
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |