1 // © 2018 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 #include "unicode/utypes.h"
6 #if !UCONFIG_NO_FORMATTING
7 #ifndef __UNUMBERFORMATTER_H__
8 #define __UNUMBERFORMATTER_H__
10 #include "unicode/ufieldpositer.h"
11 #include "unicode/umisc.h"
16 * \brief C-compatible API for localized number formatting; not recommended for C++.
18 * This is the C-compatible version of the NumberFormatter API introduced in ICU 60. C++ users should
19 * include unicode/numberformatter.h and use the proper C++ APIs.
21 * The C API accepts a number skeleton string for specifying the settings for formatting, which covers a
22 * very large subset of all possible number formatting features. For more information on number skeleton
23 * strings, see unicode/numberformatter.h.
25 * When using UNumberFormatter, which is treated as immutable, the results are exported to a mutable
26 * UFormattedNumber object, which you subsequently use for populating your string buffer or iterating over
32 * UErrorCode ec = U_ZERO_ERROR;
33 * UNumberFormatter* uformatter = unumf_openForSkeletonAndLocale(u"precision-integer", -1, "en", &ec);
34 * UFormattedNumber* uresult = unumf_openResult(&ec);
35 * if (U_FAILURE(ec)) { return; }
38 * unumf_formatDouble(uformatter, 5142.3, uresult, &ec);
39 * if (U_FAILURE(ec)) { return; }
41 * // Export the string to a malloc'd buffer:
42 * int32_t len = unumf_resultToString(uresult, NULL, 0, &ec);
43 * // at this point, ec == U_BUFFER_OVERFLOW_ERROR
45 * UChar* buffer = (UChar*) malloc((len+1)*sizeof(UChar));
46 * unumf_resultToString(uresult, buffer, len+1, &ec);
47 * if (U_FAILURE(ec)) { return; }
48 * // buffer should equal "5,142"
51 * unumf_close(uformatter);
52 * unumf_closeResult(uresult);
56 * If you are a C++ user linking against the C libraries, you can use the LocalPointer versions of these
57 * APIs. The following example uses LocalPointer with the decimal number and field position APIs:
61 * LocalUNumberFormatterPointer uformatter(unumf_openForSkeletonAndLocale(u"percent", -1, "en", &ec));
62 * LocalUFormattedNumberPointer uresult(unumf_openResult(&ec));
63 * if (U_FAILURE(ec)) { return; }
65 * // Format a decimal number:
66 * unumf_formatDecimal(uformatter.getAlias(), "9.87E-3", -1, uresult.getAlias(), &ec);
67 * if (U_FAILURE(ec)) { return; }
69 * // Get the location of the percent sign:
70 * UFieldPosition ufpos = {UNUM_PERCENT_FIELD, 0, 0};
71 * unumf_resultNextFieldPosition(uresult.getAlias(), &ufpos, &ec);
72 * // ufpos should contain beginIndex=7 and endIndex=8 since the string is "0.00987%"
74 * // No need to do any cleanup since we are using LocalPointer.
79 #ifndef U_HIDE_DRAFT_API
81 * An enum declaring how to render units, including currencies. Example outputs when formatting 123 USD and 123
82 * meters in <em>en-CA</em>:
86 * <li>NARROW*: "$123.00" and "123 m"
87 * <li>SHORT: "US$ 123.00" and "123 m"
88 * <li>FULL_NAME: "123.00 US dollars" and "123 meters"
89 * <li>ISO_CODE: "USD 123.00" and undefined behavior
90 * <li>HIDDEN: "123.00" and "123"
94 * This enum is similar to {@link com.ibm.icu.text.MeasureFormat.FormatWidth}.
98 typedef enum UNumberUnitWidth
{
100 * Print an abbreviated version of the unit name. Similar to SHORT, but always use the shortest available
101 * abbreviation or symbol. This option can be used when the context hints at the identity of the unit. For more
102 * information on the difference between NARROW and SHORT, see SHORT.
105 * In CLDR, this option corresponds to the "Narrow" format for measure units and the "¤¤¤¤¤" placeholder for
110 UNUM_UNIT_WIDTH_NARROW
,
113 * Print an abbreviated version of the unit name. Similar to NARROW, but use a slightly wider abbreviation or
114 * symbol when there may be ambiguity. This is the default behavior.
117 * For example, in <em>es-US</em>, the SHORT form for Fahrenheit is "{0} °F", but the NARROW form is "{0}°",
118 * since Fahrenheit is the customary unit for temperature in that locale.
121 * In CLDR, this option corresponds to the "Short" format for measure units and the "¤" placeholder for
126 UNUM_UNIT_WIDTH_SHORT
,
129 * Print the full name of the unit, without any abbreviations.
132 * In CLDR, this option corresponds to the default format for measure units and the "¤¤¤" placeholder for
137 UNUM_UNIT_WIDTH_FULL_NAME
,
140 * Use the three-digit ISO XXX code in place of the symbol for displaying currencies. The behavior of this
141 * option is currently undefined for use with measure units.
144 * In CLDR, this option corresponds to the "¤¤" placeholder for currencies.
148 UNUM_UNIT_WIDTH_ISO_CODE
,
151 * Format the number according to the specified unit, but do not display the unit. For currencies, apply
152 * monetary symbols and formats as with SHORT, but omit the currency symbol. For measure units, the behavior is
153 * equivalent to not specifying the unit at all.
157 UNUM_UNIT_WIDTH_HIDDEN
,
160 * One more than the highest UNumberUnitWidth value.
162 * @internal ICU 60: The numeric value may change over time; see ICU ticket #12420.
164 UNUM_UNIT_WIDTH_COUNT
166 #endif /* U_HIDE_DRAFT_API */
168 #ifndef U_HIDE_DRAFT_API
170 * An enum declaring the strategy for when and how to display grouping separators (i.e., the
171 * separator, often a comma or period, after every 2-3 powers of ten). The choices are several
172 * pre-built strategies for different use cases that employ locale data whenever possible. Example
173 * outputs for 1234 and 1234567 in <em>en-IN</em>:
176 * <li>OFF: 1234 and 12345
177 * <li>MIN2: 1234 and 12,34,567
178 * <li>AUTO: 1,234 and 12,34,567
179 * <li>ON_ALIGNED: 1,234 and 12,34,567
180 * <li>THOUSANDS: 1,234 and 1,234,567
184 * The default is AUTO, which displays grouping separators unless the locale data says that grouping
185 * is not customary. To force grouping for all numbers greater than 1000 consistently across locales,
186 * use ON_ALIGNED. On the other hand, to display grouping less frequently than the default, use MIN2
187 * or OFF. See the docs of each option for details.
190 * Note: This enum specifies the strategy for grouping sizes. To set which character to use as the
191 * grouping separator, use the "symbols" setter.
193 * @draft ICU 61 -- TODO: This should be renamed to UNumberGroupingStrategy before promoting to stable,
194 * for consistency with the other enums.
196 typedef enum UGroupingStrategy
{
198 * Do not display grouping separators in any locale.
205 * Display grouping using locale defaults, except do not show grouping on values smaller than
206 * 10000 (such that there is a <em>minimum of two digits</em> before the first separator).
209 * Note that locales may restrict grouping separators to be displayed only on 1 million or
210 * greater (for example, ee and hu) or disable grouping altogether (for example, bg currency).
213 * Locale data is used to determine whether to separate larger numbers into groups of 2
214 * (customary in South Asia) or groups of 3 (customary in Europe and the Americas).
221 * Display grouping using the default strategy for all locales. This is the default behavior.
224 * Note that locales may restrict grouping separators to be displayed only on 1 million or
225 * greater (for example, ee and hu) or disable grouping altogether (for example, bg currency).
228 * Locale data is used to determine whether to separate larger numbers into groups of 2
229 * (customary in South Asia) or groups of 3 (customary in Europe and the Americas).
236 * Always display the grouping separator on values of at least 1000.
239 * This option ignores the locale data that restricts or disables grouping, described in MIN2 and
240 * AUTO. This option may be useful to normalize the alignment of numbers, such as in a
244 * Locale data is used to determine whether to separate larger numbers into groups of 2
245 * (customary in South Asia) or groups of 3 (customary in Europe and the Americas).
249 UNUM_GROUPING_ON_ALIGNED
,
252 * Use the Western defaults: groups of 3 and enabled for all numbers 1000 or greater. Do not use
253 * locale data for determining the grouping strategy.
257 UNUM_GROUPING_THOUSANDS
,
260 * One more than the highest UGroupingStrategy value.
262 * @internal ICU 62: The numeric value may change over time; see ICU ticket #12420.
267 #endif /* U_HIDE_DRAFT_API */
269 #ifndef U_HIDE_DRAFT_API
271 * An enum declaring how to denote positive and negative numbers. Example outputs when formatting
272 * 123, 0, and -123 in <em>en-US</em>:
275 * <li>AUTO: "123", "0", and "-123"
276 * <li>ALWAYS: "+123", "+0", and "-123"
277 * <li>NEVER: "123", "0", and "123"
278 * <li>ACCOUNTING: "$123", "$0", and "($123)"
279 * <li>ACCOUNTING_ALWAYS: "+$123", "+$0", and "($123)"
280 * <li>EXCEPT_ZERO: "+123", "0", and "-123"
281 * <li>ACCOUNTING_EXCEPT_ZERO: "+$123", "$0", and "($123)"
285 * The exact format, including the position and the code point of the sign, differ by locale.
289 typedef enum UNumberSignDisplay
{
291 * Show the minus sign on negative numbers, and do not show the sign on positive numbers. This is the default
299 * Show the minus sign on negative numbers and the plus sign on positive numbers, including zero.
300 * To hide the sign on zero, see {@link UNUM_SIGN_EXCEPT_ZERO}.
307 * Do not show the sign on positive or negative numbers.
314 * Use the locale-dependent accounting format on negative numbers, and do not show the sign on positive numbers.
317 * The accounting format is defined in CLDR and varies by locale; in many Western locales, the format is a pair
318 * of parentheses around the number.
321 * Note: Since CLDR defines the accounting format in the monetary context only, this option falls back to the
322 * AUTO sign display strategy when formatting without a currency unit. This limitation may be lifted in the
327 UNUM_SIGN_ACCOUNTING
,
330 * Use the locale-dependent accounting format on negative numbers, and show the plus sign on
331 * positive numbers, including zero. For more information on the accounting format, see the
332 * ACCOUNTING sign display strategy. To hide the sign on zero, see
333 * {@link UNUM_SIGN_ACCOUNTING_EXCEPT_ZERO}.
337 UNUM_SIGN_ACCOUNTING_ALWAYS
,
340 * Show the minus sign on negative numbers and the plus sign on positive numbers. Do not show a
345 UNUM_SIGN_EXCEPT_ZERO
,
348 * Use the locale-dependent accounting format on negative numbers, and show the plus sign on
349 * positive numbers. Do not show a sign on zero. For more information on the accounting format,
350 * see the ACCOUNTING sign display strategy.
354 UNUM_SIGN_ACCOUNTING_EXCEPT_ZERO
,
357 * One more than the highest UNumberSignDisplay value.
359 * @internal ICU 60: The numeric value may change over time; see ICU ticket #12420.
362 } UNumberSignDisplay
;
363 #endif /* U_HIDE_DRAFT_API */
365 #ifndef U_HIDE_DRAFT_API
367 * An enum declaring how to render the decimal separator.
371 * <li>UNUM_DECIMAL_SEPARATOR_AUTO: "1", "1.1"
372 * <li>UNUM_DECIMAL_SEPARATOR_ALWAYS: "1.", "1.1"
375 typedef enum UNumberDecimalSeparatorDisplay
{
377 * Show the decimal separator when there are one or more digits to display after the separator, and do not show
378 * it otherwise. This is the default behavior.
382 UNUM_DECIMAL_SEPARATOR_AUTO
,
385 * Always show the decimal separator, even if there are no digits to display after the separator.
389 UNUM_DECIMAL_SEPARATOR_ALWAYS
,
392 * One more than the highest UNumberDecimalSeparatorDisplay value.
394 * @internal ICU 60: The numeric value may change over time; see ICU ticket #12420.
396 UNUM_DECIMAL_SEPARATOR_COUNT
397 } UNumberDecimalSeparatorDisplay
;
398 #endif /* U_HIDE_DRAFT_API */
400 #ifndef U_HIDE_DRAFT_API
402 * C-compatible version of icu::number::LocalizedNumberFormatter.
404 * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead.
408 struct UNumberFormatter
;
409 typedef struct UNumberFormatter UNumberFormatter
;
413 * C-compatible version of icu::number::FormattedNumber.
415 * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead.
419 struct UFormattedNumber
;
420 typedef struct UFormattedNumber UFormattedNumber
;
424 * Creates a new UNumberFormatter for the given skeleton string and locale. This is currently the only
425 * method for creating a new UNumberFormatter.
427 * Objects of type UNumberFormatter returned by this method are threadsafe.
429 * For more details on skeleton strings, see the documentation in numberformatter.h. For more details on
430 * the usage of this API, see the documentation at the top of unumberformatter.h.
432 * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead.
434 * @param skeleton The skeleton string, like u"percent precision-integer"
435 * @param skeletonLen The number of UChars in the skeleton string, or -1 it it is NUL-terminated.
436 * @param locale The NUL-terminated locale ID.
437 * @param ec Set if an error occurs.
440 U_DRAFT UNumberFormatter
* U_EXPORT2
441 unumf_openForSkeletonAndLocale(const UChar
* skeleton
, int32_t skeletonLen
, const char* locale
,
446 * Creates a new UFormattedNumber for holding the result of a number formatting operation.
448 * Objects of type UFormattedNumber are not guaranteed to be threadsafe.
450 * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead.
452 * @param ec Set if an error occurs.
455 U_DRAFT UFormattedNumber
* U_EXPORT2
456 unumf_openResult(UErrorCode
* ec
);
460 * Uses a UNumberFormatter to format an integer to a UFormattedNumber. A string, field position, and other
461 * information can be retrieved from the UFormattedNumber.
463 * The UNumberFormatter can be shared between threads. Each thread should have its own local
464 * UFormattedNumber, however, for storing the result of the formatting operation.
466 * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead.
468 * @param uformatter A formatter object created by unumf_openForSkeletonAndLocale or similar.
469 * @param value The number to be formatted.
470 * @param uresult The object that will be mutated to store the result; see unumf_openResult.
471 * @param ec Set if an error occurs.
474 U_DRAFT
void U_EXPORT2
475 unumf_formatInt(const UNumberFormatter
* uformatter
, int64_t value
, UFormattedNumber
* uresult
,
480 * Uses a UNumberFormatter to format a double to a UFormattedNumber. A string, field position, and other
481 * information can be retrieved from the UFormattedNumber.
483 * The UNumberFormatter can be shared between threads. Each thread should have its own local
484 * UFormattedNumber, however, for storing the result of the formatting operation.
486 * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead.
488 * @param uformatter A formatter object created by unumf_openForSkeletonAndLocale or similar.
489 * @param value The number to be formatted.
490 * @param uresult The object that will be mutated to store the result; see unumf_openResult.
491 * @param ec Set if an error occurs.
494 U_DRAFT
void U_EXPORT2
495 unumf_formatDouble(const UNumberFormatter
* uformatter
, double value
, UFormattedNumber
* uresult
,
500 * Uses a UNumberFormatter to format a decimal number to a UFormattedNumber. A string, field position, and
501 * other information can be retrieved from the UFormattedNumber.
503 * The UNumberFormatter can be shared between threads. Each thread should have its own local
504 * UFormattedNumber, however, for storing the result of the formatting operation.
506 * The syntax of the unformatted number is a "numeric string" as defined in the Decimal Arithmetic
507 * Specification, available at http://speleotrove.com/decimal
509 * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead.
511 * @param uformatter A formatter object created by unumf_openForSkeletonAndLocale or similar.
512 * @param value The numeric string to be formatted.
513 * @param valueLen The length of the numeric string, or -1 if it is NUL-terminated.
514 * @param uresult The object that will be mutated to store the result; see unumf_openResult.
515 * @param ec Set if an error occurs.
518 U_DRAFT
void U_EXPORT2
519 unumf_formatDecimal(const UNumberFormatter
* uformatter
, const char* value
, int32_t valueLen
,
520 UFormattedNumber
* uresult
, UErrorCode
* ec
);
524 * Extracts the result number string out of a UFormattedNumber to a UChar buffer if possible.
525 * If bufferCapacity is greater than the required length, a terminating NUL is written.
526 * If bufferCapacity is less than the required length, an error code is set.
528 * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead.
530 * @param uresult The object containing the formatted number.
531 * @param buffer Where to save the string output.
532 * @param bufferCapacity The number of UChars available in the buffer.
533 * @param ec Set if an error occurs.
534 * @return The required length.
537 U_DRAFT
int32_t U_EXPORT2
538 unumf_resultToString(const UFormattedNumber
* uresult
, UChar
* buffer
, int32_t bufferCapacity
,
543 * Determines the start and end indices of the next occurrence of the given <em>field</em> in the
544 * output string. This allows you to determine the locations of, for example, the integer part,
545 * fraction part, or symbols.
547 * If a field occurs just once, calling this method will find that occurrence and return it. If a
548 * field occurs multiple times, this method may be called repeatedly with the following pattern:
551 * UFieldPosition ufpos = {UNUM_GROUPING_SEPARATOR_FIELD, 0, 0};
552 * while (unumf_resultNextFieldPosition(uresult, ufpos, &ec)) {
553 * // do something with ufpos.
557 * This method is useful if you know which field to query. If you want all available field position
558 * information, use unumf_resultGetAllFieldPositions().
560 * NOTE: All fields of the UFieldPosition must be initialized before calling this method.
562 * @param fieldPosition
563 * Input+output variable. On input, the "field" property determines which field to look up,
564 * and the "endIndex" property determines where to begin the search. On output, the
565 * "beginIndex" field is set to the beginning of the first occurrence of the field after the
566 * input "endIndex", and "endIndex" is set to the end of that occurrence of the field
567 * (exclusive index). If a field position is not found, the FieldPosition is not changed and
568 * the method returns FALSE.
569 * @param ec Set if an error occurs.
572 U_DRAFT UBool U_EXPORT2
573 unumf_resultNextFieldPosition(const UFormattedNumber
* uresult
, UFieldPosition
* ufpos
, UErrorCode
* ec
);
577 * Populates the given iterator with all fields in the formatted output string. This allows you to
578 * determine the locations of the integer part, fraction part, and sign.
580 * If you need information on only one field, use unumf_resultNextFieldPosition().
582 * @param uresult The object containing the formatted number.
584 * A pointer to a UFieldPositionIterator created by {@link #ufieldpositer_open}. Iteration
585 * information already present in the UFieldPositionIterator is deleted, and the iterator is reset
586 * to apply to the fields in the formatted string created by this function call. The field values
587 * and indexes returned by {@link #ufieldpositer_next} represent fields denoted by
588 * the UNumberFormatFields enum. Fields are not returned in a guaranteed order. Fields cannot
589 * overlap, but they may nest. For example, 1234 could format as "1,234" which might consist of a
590 * grouping separator field for ',' and an integer field encompassing the entire string.
591 * @param ec Set if an error occurs.
594 U_DRAFT
void U_EXPORT2
595 unumf_resultGetAllFieldPositions(const UFormattedNumber
* uresult
, UFieldPositionIterator
* ufpositer
,
600 * Releases the UNumberFormatter created by unumf_openForSkeletonAndLocale().
602 * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead.
604 * @param uformatter An object created by unumf_openForSkeletonAndLocale().
607 U_DRAFT
void U_EXPORT2
608 unumf_close(UNumberFormatter
* uformatter
);
612 * Releases the UFormattedNumber created by unumf_openResult().
614 * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead.
616 * @param uresult An object created by unumf_openResult().
619 U_DRAFT
void U_EXPORT2
620 unumf_closeResult(UFormattedNumber
* uresult
);
623 #if U_SHOW_CPLUSPLUS_API
627 * \class LocalUNumberFormatterPointer
628 * "Smart pointer" class; closes a UNumberFormatter via unumf_close().
629 * For most methods see the LocalPointerBase base class.
633 * LocalUNumberFormatterPointer uformatter(unumf_openForSkeletonAndLocale(...));
634 * // no need to explicitly call unumf_close()
637 * @see LocalPointerBase
641 U_DEFINE_LOCAL_OPEN_POINTER(LocalUNumberFormatterPointer
, UNumberFormatter
, unumf_close
);
644 * \class LocalUNumberFormatterPointer
645 * "Smart pointer" class; closes a UFormattedNumber via unumf_closeResult().
646 * For most methods see the LocalPointerBase base class.
650 * LocalUFormattedNumberPointer uformatter(unumf_openResult(...));
651 * // no need to explicitly call unumf_closeResult()
654 * @see LocalPointerBase
658 U_DEFINE_LOCAL_OPEN_POINTER(LocalUFormattedNumberPointer
, UFormattedNumber
, unumf_closeResult
);
661 #endif // U_SHOW_CPLUSPLUS_API
663 #endif /* U_HIDE_DRAFT_API */
665 #endif //__UNUMBERFORMATTER_H__
666 #endif /* #if !UCONFIG_NO_FORMATTING */