jpayne@69: // © 2018 and later: Unicode, Inc. and others. jpayne@69: // License & terms of use: http://www.unicode.org/copyright.html jpayne@69: jpayne@69: #include "unicode/utypes.h" jpayne@69: jpayne@69: #if !UCONFIG_NO_FORMATTING jpayne@69: #ifndef __UNUMBERFORMATTER_H__ jpayne@69: #define __UNUMBERFORMATTER_H__ jpayne@69: jpayne@69: #include "unicode/parseerr.h" jpayne@69: #include "unicode/ufieldpositer.h" jpayne@69: #include "unicode/umisc.h" jpayne@69: #include "unicode/uformattedvalue.h" jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * \file jpayne@69: * \brief C-compatible API for localized number formatting; not recommended for C++. jpayne@69: * jpayne@69: * This is the C-compatible version of the NumberFormatter API introduced in ICU 60. C++ users should jpayne@69: * include unicode/numberformatter.h and use the proper C++ APIs. jpayne@69: * jpayne@69: * The C API accepts a number skeleton string for specifying the settings for formatting, which covers a jpayne@69: * very large subset of all possible number formatting features. For more information on number skeleton jpayne@69: * strings, see unicode/numberformatter.h. jpayne@69: * jpayne@69: * When using UNumberFormatter, which is treated as immutable, the results are exported to a mutable jpayne@69: * UFormattedNumber object, which you subsequently use for populating your string buffer or iterating over jpayne@69: * the fields. jpayne@69: * jpayne@69: * Example code: jpayne@69: *
jpayne@69:  * // Setup:
jpayne@69:  * UErrorCode ec = U_ZERO_ERROR;
jpayne@69:  * UNumberFormatter* uformatter = unumf_openForSkeletonAndLocale(u"precision-integer", -1, "en", &ec);
jpayne@69:  * UFormattedNumber* uresult = unumf_openResult(&ec);
jpayne@69:  * if (U_FAILURE(ec)) { return; }
jpayne@69:  *
jpayne@69:  * // Format a double:
jpayne@69:  * unumf_formatDouble(uformatter, 5142.3, uresult, &ec);
jpayne@69:  * if (U_FAILURE(ec)) { return; }
jpayne@69:  *
jpayne@69:  * // Export the string to a malloc'd buffer:
jpayne@69:  * int32_t len = unumf_resultToString(uresult, NULL, 0, &ec);
jpayne@69:  * // at this point, ec == U_BUFFER_OVERFLOW_ERROR
jpayne@69:  * ec = U_ZERO_ERROR;
jpayne@69:  * UChar* buffer = (UChar*) malloc((len+1)*sizeof(UChar));
jpayne@69:  * unumf_resultToString(uresult, buffer, len+1, &ec);
jpayne@69:  * if (U_FAILURE(ec)) { return; }
jpayne@69:  * // buffer should equal "5,142"
jpayne@69:  *
jpayne@69:  * // Cleanup:
jpayne@69:  * unumf_close(uformatter);
jpayne@69:  * unumf_closeResult(uresult);
jpayne@69:  * free(buffer);
jpayne@69:  * 
jpayne@69: * jpayne@69: * If you are a C++ user linking against the C libraries, you can use the LocalPointer versions of these jpayne@69: * APIs. The following example uses LocalPointer with the decimal number and field position APIs: jpayne@69: * jpayne@69: *
jpayne@69:  * // Setup:
jpayne@69:  * LocalUNumberFormatterPointer uformatter(unumf_openForSkeletonAndLocale(u"percent", -1, "en", &ec));
jpayne@69:  * LocalUFormattedNumberPointer uresult(unumf_openResult(&ec));
jpayne@69:  * if (U_FAILURE(ec)) { return; }
jpayne@69:  *
jpayne@69:  * // Format a decimal number:
jpayne@69:  * unumf_formatDecimal(uformatter.getAlias(), "9.87E-3", -1, uresult.getAlias(), &ec);
jpayne@69:  * if (U_FAILURE(ec)) { return; }
jpayne@69:  *
jpayne@69:  * // Get the location of the percent sign:
jpayne@69:  * UFieldPosition ufpos = {UNUM_PERCENT_FIELD, 0, 0};
jpayne@69:  * unumf_resultNextFieldPosition(uresult.getAlias(), &ufpos, &ec);
jpayne@69:  * // ufpos should contain beginIndex=7 and endIndex=8 since the string is "0.00987%"
jpayne@69:  *
jpayne@69:  * // No need to do any cleanup since we are using LocalPointer.
jpayne@69:  * 
jpayne@69: */ jpayne@69: jpayne@69: /** jpayne@69: * An enum declaring how to render units, including currencies. Example outputs when formatting 123 USD and 123 jpayne@69: * meters in en-CA: jpayne@69: * jpayne@69: *

jpayne@69: *

jpayne@69: * jpayne@69: *

jpayne@69: * This enum is similar to {@link UMeasureFormatWidth}. jpayne@69: * jpayne@69: * @stable ICU 60 jpayne@69: */ jpayne@69: typedef enum UNumberUnitWidth { jpayne@69: /** jpayne@69: * Print an abbreviated version of the unit name. Similar to SHORT, but always use the shortest available jpayne@69: * abbreviation or symbol. This option can be used when the context hints at the identity of the unit. For more jpayne@69: * information on the difference between NARROW and SHORT, see SHORT. jpayne@69: * jpayne@69: *

jpayne@69: * In CLDR, this option corresponds to the "Narrow" format for measure units and the "¤¤¤¤¤" placeholder for jpayne@69: * currencies. jpayne@69: * jpayne@69: * @stable ICU 60 jpayne@69: */ jpayne@69: UNUM_UNIT_WIDTH_NARROW, jpayne@69: jpayne@69: /** jpayne@69: * Print an abbreviated version of the unit name. Similar to NARROW, but use a slightly wider abbreviation or jpayne@69: * symbol when there may be ambiguity. This is the default behavior. jpayne@69: * jpayne@69: *

jpayne@69: * For example, in es-US, the SHORT form for Fahrenheit is "{0} °F", but the NARROW form is "{0}°", jpayne@69: * since Fahrenheit is the customary unit for temperature in that locale. jpayne@69: * jpayne@69: *

jpayne@69: * In CLDR, this option corresponds to the "Short" format for measure units and the "¤" placeholder for jpayne@69: * currencies. jpayne@69: * jpayne@69: * @stable ICU 60 jpayne@69: */ jpayne@69: UNUM_UNIT_WIDTH_SHORT, jpayne@69: jpayne@69: /** jpayne@69: * Print the full name of the unit, without any abbreviations. jpayne@69: * jpayne@69: *

jpayne@69: * In CLDR, this option corresponds to the default format for measure units and the "¤¤¤" placeholder for jpayne@69: * currencies. jpayne@69: * jpayne@69: * @stable ICU 60 jpayne@69: */ jpayne@69: UNUM_UNIT_WIDTH_FULL_NAME, jpayne@69: jpayne@69: /** jpayne@69: * Use the three-digit ISO XXX code in place of the symbol for displaying currencies. The behavior of this jpayne@69: * option is currently undefined for use with measure units. jpayne@69: * jpayne@69: *

jpayne@69: * In CLDR, this option corresponds to the "¤¤" placeholder for currencies. jpayne@69: * jpayne@69: * @stable ICU 60 jpayne@69: */ jpayne@69: UNUM_UNIT_WIDTH_ISO_CODE, jpayne@69: jpayne@69: /** jpayne@69: * Format the number according to the specified unit, but do not display the unit. For currencies, apply jpayne@69: * monetary symbols and formats as with SHORT, but omit the currency symbol. For measure units, the behavior is jpayne@69: * equivalent to not specifying the unit at all. jpayne@69: * jpayne@69: * @stable ICU 60 jpayne@69: */ jpayne@69: UNUM_UNIT_WIDTH_HIDDEN, jpayne@69: jpayne@69: /** jpayne@69: * One more than the highest UNumberUnitWidth value. jpayne@69: * jpayne@69: * @internal ICU 60: The numeric value may change over time; see ICU ticket #12420. jpayne@69: */ jpayne@69: UNUM_UNIT_WIDTH_COUNT jpayne@69: } UNumberUnitWidth; jpayne@69: jpayne@69: /** jpayne@69: * An enum declaring the strategy for when and how to display grouping separators (i.e., the jpayne@69: * separator, often a comma or period, after every 2-3 powers of ten). The choices are several jpayne@69: * pre-built strategies for different use cases that employ locale data whenever possible. Example jpayne@69: * outputs for 1234 and 1234567 in en-IN: jpayne@69: * jpayne@69: *

jpayne@69: * jpayne@69: *

jpayne@69: * The default is AUTO, which displays grouping separators unless the locale data says that grouping jpayne@69: * is not customary. To force grouping for all numbers greater than 1000 consistently across locales, jpayne@69: * use ON_ALIGNED. On the other hand, to display grouping less frequently than the default, use MIN2 jpayne@69: * or OFF. See the docs of each option for details. jpayne@69: * jpayne@69: *

jpayne@69: * Note: This enum specifies the strategy for grouping sizes. To set which character to use as the jpayne@69: * grouping separator, use the "symbols" setter. jpayne@69: * jpayne@69: * @stable ICU 63 jpayne@69: */ jpayne@69: typedef enum UNumberGroupingStrategy { jpayne@69: /** jpayne@69: * Do not display grouping separators in any locale. jpayne@69: * jpayne@69: * @stable ICU 61 jpayne@69: */ jpayne@69: UNUM_GROUPING_OFF, jpayne@69: jpayne@69: /** jpayne@69: * Display grouping using locale defaults, except do not show grouping on values smaller than jpayne@69: * 10000 (such that there is a minimum of two digits before the first separator). jpayne@69: * jpayne@69: *

jpayne@69: * Note that locales may restrict grouping separators to be displayed only on 1 million or jpayne@69: * greater (for example, ee and hu) or disable grouping altogether (for example, bg currency). jpayne@69: * jpayne@69: *

jpayne@69: * Locale data is used to determine whether to separate larger numbers into groups of 2 jpayne@69: * (customary in South Asia) or groups of 3 (customary in Europe and the Americas). jpayne@69: * jpayne@69: * @stable ICU 61 jpayne@69: */ jpayne@69: UNUM_GROUPING_MIN2, jpayne@69: jpayne@69: /** jpayne@69: * Display grouping using the default strategy for all locales. This is the default behavior. jpayne@69: * jpayne@69: *

jpayne@69: * Note that locales may restrict grouping separators to be displayed only on 1 million or jpayne@69: * greater (for example, ee and hu) or disable grouping altogether (for example, bg currency). jpayne@69: * jpayne@69: *

jpayne@69: * Locale data is used to determine whether to separate larger numbers into groups of 2 jpayne@69: * (customary in South Asia) or groups of 3 (customary in Europe and the Americas). jpayne@69: * jpayne@69: * @stable ICU 61 jpayne@69: */ jpayne@69: UNUM_GROUPING_AUTO, jpayne@69: jpayne@69: /** jpayne@69: * Always display the grouping separator on values of at least 1000. jpayne@69: * jpayne@69: *

jpayne@69: * This option ignores the locale data that restricts or disables grouping, described in MIN2 and jpayne@69: * AUTO. This option may be useful to normalize the alignment of numbers, such as in a jpayne@69: * spreadsheet. jpayne@69: * jpayne@69: *

jpayne@69: * Locale data is used to determine whether to separate larger numbers into groups of 2 jpayne@69: * (customary in South Asia) or groups of 3 (customary in Europe and the Americas). jpayne@69: * jpayne@69: * @stable ICU 61 jpayne@69: */ jpayne@69: UNUM_GROUPING_ON_ALIGNED, jpayne@69: jpayne@69: /** jpayne@69: * Use the Western defaults: groups of 3 and enabled for all numbers 1000 or greater. Do not use jpayne@69: * locale data for determining the grouping strategy. jpayne@69: * jpayne@69: * @stable ICU 61 jpayne@69: */ jpayne@69: UNUM_GROUPING_THOUSANDS jpayne@69: jpayne@69: #ifndef U_HIDE_INTERNAL_API jpayne@69: , jpayne@69: /** jpayne@69: * One more than the highest UNumberGroupingStrategy value. jpayne@69: * jpayne@69: * @internal ICU 62: The numeric value may change over time; see ICU ticket #12420. jpayne@69: */ jpayne@69: UNUM_GROUPING_COUNT jpayne@69: #endif /* U_HIDE_INTERNAL_API */ jpayne@69: jpayne@69: } UNumberGroupingStrategy; jpayne@69: jpayne@69: /** jpayne@69: * An enum declaring how to denote positive and negative numbers. Example outputs when formatting jpayne@69: * 123, 0, and -123 in en-US: jpayne@69: * jpayne@69: *

jpayne@69: * jpayne@69: *

jpayne@69: * The exact format, including the position and the code point of the sign, differ by locale. jpayne@69: * jpayne@69: * @stable ICU 60 jpayne@69: */ jpayne@69: typedef enum UNumberSignDisplay { jpayne@69: /** jpayne@69: * Show the minus sign on negative numbers, and do not show the sign on positive numbers. This is the default jpayne@69: * behavior. jpayne@69: * jpayne@69: * @stable ICU 60 jpayne@69: */ jpayne@69: UNUM_SIGN_AUTO, jpayne@69: jpayne@69: /** jpayne@69: * Show the minus sign on negative numbers and the plus sign on positive numbers, including zero. jpayne@69: * To hide the sign on zero, see {@link UNUM_SIGN_EXCEPT_ZERO}. jpayne@69: * jpayne@69: * @stable ICU 60 jpayne@69: */ jpayne@69: UNUM_SIGN_ALWAYS, jpayne@69: jpayne@69: /** jpayne@69: * Do not show the sign on positive or negative numbers. jpayne@69: * jpayne@69: * @stable ICU 60 jpayne@69: */ jpayne@69: UNUM_SIGN_NEVER, jpayne@69: jpayne@69: /** jpayne@69: * Use the locale-dependent accounting format on negative numbers, and do not show the sign on positive numbers. jpayne@69: * jpayne@69: *

jpayne@69: * The accounting format is defined in CLDR and varies by locale; in many Western locales, the format is a pair jpayne@69: * of parentheses around the number. jpayne@69: * jpayne@69: *

jpayne@69: * Note: Since CLDR defines the accounting format in the monetary context only, this option falls back to the jpayne@69: * AUTO sign display strategy when formatting without a currency unit. This limitation may be lifted in the jpayne@69: * future. jpayne@69: * jpayne@69: * @stable ICU 60 jpayne@69: */ jpayne@69: UNUM_SIGN_ACCOUNTING, jpayne@69: jpayne@69: /** jpayne@69: * Use the locale-dependent accounting format on negative numbers, and show the plus sign on jpayne@69: * positive numbers, including zero. For more information on the accounting format, see the jpayne@69: * ACCOUNTING sign display strategy. To hide the sign on zero, see jpayne@69: * {@link UNUM_SIGN_ACCOUNTING_EXCEPT_ZERO}. jpayne@69: * jpayne@69: * @stable ICU 60 jpayne@69: */ jpayne@69: UNUM_SIGN_ACCOUNTING_ALWAYS, jpayne@69: jpayne@69: /** jpayne@69: * Show the minus sign on negative numbers and the plus sign on positive numbers. Do not show a jpayne@69: * sign on zero, numbers that round to zero, or NaN. jpayne@69: * jpayne@69: * @stable ICU 61 jpayne@69: */ jpayne@69: UNUM_SIGN_EXCEPT_ZERO, jpayne@69: jpayne@69: /** jpayne@69: * Use the locale-dependent accounting format on negative numbers, and show the plus sign on jpayne@69: * positive numbers. Do not show a sign on zero, numbers that round to zero, or NaN. For more jpayne@69: * information on the accounting format, see the ACCOUNTING sign display strategy. jpayne@69: * jpayne@69: * @stable ICU 61 jpayne@69: */ jpayne@69: UNUM_SIGN_ACCOUNTING_EXCEPT_ZERO, jpayne@69: jpayne@69: /** jpayne@69: * One more than the highest UNumberSignDisplay value. jpayne@69: * jpayne@69: * @internal ICU 60: The numeric value may change over time; see ICU ticket #12420. jpayne@69: */ jpayne@69: UNUM_SIGN_COUNT jpayne@69: } UNumberSignDisplay; jpayne@69: jpayne@69: /** jpayne@69: * An enum declaring how to render the decimal separator. jpayne@69: * jpayne@69: *

jpayne@69: *

jpayne@69: * jpayne@69: * @stable ICU 60 jpayne@69: */ jpayne@69: typedef enum UNumberDecimalSeparatorDisplay { jpayne@69: /** jpayne@69: * Show the decimal separator when there are one or more digits to display after the separator, and do not show jpayne@69: * it otherwise. This is the default behavior. jpayne@69: * jpayne@69: * @stable ICU 60 jpayne@69: */ jpayne@69: UNUM_DECIMAL_SEPARATOR_AUTO, jpayne@69: jpayne@69: /** jpayne@69: * Always show the decimal separator, even if there are no digits to display after the separator. jpayne@69: * jpayne@69: * @stable ICU 60 jpayne@69: */ jpayne@69: UNUM_DECIMAL_SEPARATOR_ALWAYS, jpayne@69: jpayne@69: /** jpayne@69: * One more than the highest UNumberDecimalSeparatorDisplay value. jpayne@69: * jpayne@69: * @internal ICU 60: The numeric value may change over time; see ICU ticket #12420. jpayne@69: */ jpayne@69: UNUM_DECIMAL_SEPARATOR_COUNT jpayne@69: } UNumberDecimalSeparatorDisplay; jpayne@69: jpayne@69: struct UNumberFormatter; jpayne@69: /** jpayne@69: * C-compatible version of icu::number::LocalizedNumberFormatter. jpayne@69: * jpayne@69: * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead. jpayne@69: * jpayne@69: * @stable ICU 62 jpayne@69: */ jpayne@69: typedef struct UNumberFormatter UNumberFormatter; jpayne@69: jpayne@69: struct UFormattedNumber; jpayne@69: /** jpayne@69: * C-compatible version of icu::number::FormattedNumber. jpayne@69: * jpayne@69: * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead. jpayne@69: * jpayne@69: * @stable ICU 62 jpayne@69: */ jpayne@69: typedef struct UFormattedNumber UFormattedNumber; jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Creates a new UNumberFormatter for the given skeleton string and locale. This is currently the only jpayne@69: * method for creating a new UNumberFormatter. jpayne@69: * jpayne@69: * Objects of type UNumberFormatter returned by this method are threadsafe. jpayne@69: * jpayne@69: * For more details on skeleton strings, see the documentation in numberformatter.h. For more details on jpayne@69: * the usage of this API, see the documentation at the top of unumberformatter.h. jpayne@69: * jpayne@69: * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead. jpayne@69: * jpayne@69: * @param skeleton The skeleton string, like u"percent precision-integer" jpayne@69: * @param skeletonLen The number of UChars in the skeleton string, or -1 if it is NUL-terminated. jpayne@69: * @param locale The NUL-terminated locale ID. jpayne@69: * @param ec Set if an error occurs. jpayne@69: * @stable ICU 62 jpayne@69: */ jpayne@69: U_STABLE UNumberFormatter* U_EXPORT2 jpayne@69: unumf_openForSkeletonAndLocale(const UChar* skeleton, int32_t skeletonLen, const char* locale, jpayne@69: UErrorCode* ec); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Like unumf_openForSkeletonAndLocale, but accepts a UParseError, which will be populated with the jpayne@69: * location of a skeleton syntax error if such a syntax error exists. jpayne@69: * jpayne@69: * @param skeleton The skeleton string, like u"percent precision-integer" jpayne@69: * @param skeletonLen The number of UChars in the skeleton string, or -1 if it is NUL-terminated. jpayne@69: * @param locale The NUL-terminated locale ID. jpayne@69: * @param perror A parse error struct populated if an error occurs when parsing. Can be NULL. jpayne@69: * If no error occurs, perror->offset will be set to -1. jpayne@69: * @param ec Set if an error occurs. jpayne@69: * @stable ICU 64 jpayne@69: */ jpayne@69: U_STABLE UNumberFormatter* U_EXPORT2 jpayne@69: unumf_openForSkeletonAndLocaleWithError( jpayne@69: const UChar* skeleton, int32_t skeletonLen, const char* locale, UParseError* perror, UErrorCode* ec); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Creates an object to hold the result of a UNumberFormatter jpayne@69: * operation. The object can be used repeatedly; it is cleared whenever jpayne@69: * passed to a format function. jpayne@69: * jpayne@69: * @param ec Set if an error occurs. jpayne@69: * @stable ICU 62 jpayne@69: */ jpayne@69: U_STABLE UFormattedNumber* U_EXPORT2 jpayne@69: unumf_openResult(UErrorCode* ec); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Uses a UNumberFormatter to format an integer to a UFormattedNumber. A string, field position, and other jpayne@69: * information can be retrieved from the UFormattedNumber. jpayne@69: * jpayne@69: * The UNumberFormatter can be shared between threads. Each thread should have its own local jpayne@69: * UFormattedNumber, however, for storing the result of the formatting operation. jpayne@69: * jpayne@69: * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead. jpayne@69: * jpayne@69: * @param uformatter A formatter object created by unumf_openForSkeletonAndLocale or similar. jpayne@69: * @param value The number to be formatted. jpayne@69: * @param uresult The object that will be mutated to store the result; see unumf_openResult. jpayne@69: * @param ec Set if an error occurs. jpayne@69: * @stable ICU 62 jpayne@69: */ jpayne@69: U_STABLE void U_EXPORT2 jpayne@69: unumf_formatInt(const UNumberFormatter* uformatter, int64_t value, UFormattedNumber* uresult, jpayne@69: UErrorCode* ec); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Uses a UNumberFormatter to format a double to a UFormattedNumber. A string, field position, and other jpayne@69: * information can be retrieved from the UFormattedNumber. jpayne@69: * jpayne@69: * The UNumberFormatter can be shared between threads. Each thread should have its own local jpayne@69: * UFormattedNumber, however, for storing the result of the formatting operation. jpayne@69: * jpayne@69: * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead. jpayne@69: * jpayne@69: * @param uformatter A formatter object created by unumf_openForSkeletonAndLocale or similar. jpayne@69: * @param value The number to be formatted. jpayne@69: * @param uresult The object that will be mutated to store the result; see unumf_openResult. jpayne@69: * @param ec Set if an error occurs. jpayne@69: * @stable ICU 62 jpayne@69: */ jpayne@69: U_STABLE void U_EXPORT2 jpayne@69: unumf_formatDouble(const UNumberFormatter* uformatter, double value, UFormattedNumber* uresult, jpayne@69: UErrorCode* ec); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Uses a UNumberFormatter to format a decimal number to a UFormattedNumber. A string, field position, and jpayne@69: * other information can be retrieved from the UFormattedNumber. jpayne@69: * jpayne@69: * The UNumberFormatter can be shared between threads. Each thread should have its own local jpayne@69: * UFormattedNumber, however, for storing the result of the formatting operation. jpayne@69: * jpayne@69: * The syntax of the unformatted number is a "numeric string" as defined in the Decimal Arithmetic jpayne@69: * Specification, available at http://speleotrove.com/decimal jpayne@69: * jpayne@69: * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead. jpayne@69: * jpayne@69: * @param uformatter A formatter object created by unumf_openForSkeletonAndLocale or similar. jpayne@69: * @param value The numeric string to be formatted. jpayne@69: * @param valueLen The length of the numeric string, or -1 if it is NUL-terminated. jpayne@69: * @param uresult The object that will be mutated to store the result; see unumf_openResult. jpayne@69: * @param ec Set if an error occurs. jpayne@69: * @stable ICU 62 jpayne@69: */ jpayne@69: U_STABLE void U_EXPORT2 jpayne@69: unumf_formatDecimal(const UNumberFormatter* uformatter, const char* value, int32_t valueLen, jpayne@69: UFormattedNumber* uresult, UErrorCode* ec); jpayne@69: jpayne@69: /** jpayne@69: * Returns a representation of a UFormattedNumber as a UFormattedValue, jpayne@69: * which can be subsequently passed to any API requiring that type. jpayne@69: * jpayne@69: * The returned object is owned by the UFormattedNumber and is valid jpayne@69: * only as long as the UFormattedNumber is present and unchanged in memory. jpayne@69: * jpayne@69: * You can think of this method as a cast between types. jpayne@69: * jpayne@69: * @param uresult The object containing the formatted string. jpayne@69: * @param ec Set if an error occurs. jpayne@69: * @return A UFormattedValue owned by the input object. jpayne@69: * @stable ICU 64 jpayne@69: */ jpayne@69: U_STABLE const UFormattedValue* U_EXPORT2 jpayne@69: unumf_resultAsValue(const UFormattedNumber* uresult, UErrorCode* ec); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Extracts the result number string out of a UFormattedNumber to a UChar buffer if possible. jpayne@69: * If bufferCapacity is greater than the required length, a terminating NUL is written. jpayne@69: * If bufferCapacity is less than the required length, an error code is set. jpayne@69: * jpayne@69: * Also see ufmtval_getString, which returns a NUL-terminated string: jpayne@69: * jpayne@69: * int32_t len; jpayne@69: * const UChar* str = ufmtval_getString(unumf_resultAsValue(uresult, &ec), &len, &ec); jpayne@69: * jpayne@69: * NOTE: This is a C-compatible API; C++ users should build against numberformatter.h instead. jpayne@69: * jpayne@69: * @param uresult The object containing the formatted number. jpayne@69: * @param buffer Where to save the string output. jpayne@69: * @param bufferCapacity The number of UChars available in the buffer. jpayne@69: * @param ec Set if an error occurs. jpayne@69: * @return The required length. jpayne@69: * @stable ICU 62 jpayne@69: */ jpayne@69: U_STABLE int32_t U_EXPORT2 jpayne@69: unumf_resultToString(const UFormattedNumber* uresult, UChar* buffer, int32_t bufferCapacity, jpayne@69: UErrorCode* ec); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Determines the start and end indices of the next occurrence of the given field in the jpayne@69: * output string. This allows you to determine the locations of, for example, the integer part, jpayne@69: * fraction part, or symbols. jpayne@69: * jpayne@69: * This is a simpler but less powerful alternative to {@link ufmtval_nextPosition}. jpayne@69: * jpayne@69: * If a field occurs just once, calling this method will find that occurrence and return it. If a jpayne@69: * field occurs multiple times, this method may be called repeatedly with the following pattern: jpayne@69: * jpayne@69: *
jpayne@69:  * UFieldPosition ufpos = {UNUM_GROUPING_SEPARATOR_FIELD, 0, 0};
jpayne@69:  * while (unumf_resultNextFieldPosition(uresult, ufpos, &ec)) {
jpayne@69:  *   // do something with ufpos.
jpayne@69:  * }
jpayne@69:  * 
jpayne@69: * jpayne@69: * This method is useful if you know which field to query. If you want all available field position jpayne@69: * information, use unumf_resultGetAllFieldPositions(). jpayne@69: * jpayne@69: * NOTE: All fields of the UFieldPosition must be initialized before calling this method. jpayne@69: * jpayne@69: * @param uresult The object containing the formatted number. jpayne@69: * @param ufpos jpayne@69: * Input+output variable. On input, the "field" property determines which field to look up, jpayne@69: * and the "endIndex" property determines where to begin the search. On output, the jpayne@69: * "beginIndex" field is set to the beginning of the first occurrence of the field after the jpayne@69: * input "endIndex", and "endIndex" is set to the end of that occurrence of the field jpayne@69: * (exclusive index). If a field position is not found, the FieldPosition is not changed and jpayne@69: * the method returns FALSE. jpayne@69: * @param ec Set if an error occurs. jpayne@69: * @stable ICU 62 jpayne@69: */ jpayne@69: U_STABLE UBool U_EXPORT2 jpayne@69: unumf_resultNextFieldPosition(const UFormattedNumber* uresult, UFieldPosition* ufpos, UErrorCode* ec); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Populates the given iterator with all fields in the formatted output string. This allows you to jpayne@69: * determine the locations of the integer part, fraction part, and sign. jpayne@69: * jpayne@69: * This is an alternative to the more powerful {@link ufmtval_nextPosition} API. jpayne@69: * jpayne@69: * If you need information on only one field, use {@link ufmtval_nextPosition} or jpayne@69: * {@link unumf_resultNextFieldPosition}. jpayne@69: * jpayne@69: * @param uresult The object containing the formatted number. jpayne@69: * @param ufpositer jpayne@69: * A pointer to a UFieldPositionIterator created by {@link #ufieldpositer_open}. Iteration jpayne@69: * information already present in the UFieldPositionIterator is deleted, and the iterator is reset jpayne@69: * to apply to the fields in the formatted string created by this function call. The field values jpayne@69: * and indexes returned by {@link #ufieldpositer_next} represent fields denoted by jpayne@69: * the UNumberFormatFields enum. Fields are not returned in a guaranteed order. Fields cannot jpayne@69: * overlap, but they may nest. For example, 1234 could format as "1,234" which might consist of a jpayne@69: * grouping separator field for ',' and an integer field encompassing the entire string. jpayne@69: * @param ec Set if an error occurs. jpayne@69: * @stable ICU 62 jpayne@69: */ jpayne@69: U_STABLE void U_EXPORT2 jpayne@69: unumf_resultGetAllFieldPositions(const UFormattedNumber* uresult, UFieldPositionIterator* ufpositer, jpayne@69: UErrorCode* ec); jpayne@69: jpayne@69: jpayne@69: // TODO(ICU-20775): Propose this as API. jpayne@69: // NOTE: This is not currently implemented. jpayne@69: // U_DRAFT int32_t U_EXPORT2 jpayne@69: // unumf_resultToDecimalNumber(const UFormattedNumber* uresult, char* buffer, int32_t bufferCapacity, jpayne@69: // UErrorCode* ec); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Releases the UNumberFormatter created by unumf_openForSkeletonAndLocale(). jpayne@69: * jpayne@69: * @param uformatter An object created by unumf_openForSkeletonAndLocale(). jpayne@69: * @stable ICU 62 jpayne@69: */ jpayne@69: U_STABLE void U_EXPORT2 jpayne@69: unumf_close(UNumberFormatter* uformatter); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Releases the UFormattedNumber created by unumf_openResult(). jpayne@69: * jpayne@69: * @param uresult An object created by unumf_openResult(). jpayne@69: * @stable ICU 62 jpayne@69: */ jpayne@69: U_STABLE void U_EXPORT2 jpayne@69: unumf_closeResult(UFormattedNumber* uresult); jpayne@69: jpayne@69: jpayne@69: #if U_SHOW_CPLUSPLUS_API jpayne@69: U_NAMESPACE_BEGIN jpayne@69: jpayne@69: /** jpayne@69: * \class LocalUNumberFormatterPointer jpayne@69: * "Smart pointer" class; closes a UNumberFormatter via unumf_close(). jpayne@69: * For most methods see the LocalPointerBase base class. jpayne@69: * jpayne@69: * Usage: jpayne@69: *
jpayne@69:  * LocalUNumberFormatterPointer uformatter(unumf_openForSkeletonAndLocale(...));
jpayne@69:  * // no need to explicitly call unumf_close()
jpayne@69:  * 
jpayne@69: * jpayne@69: * @see LocalPointerBase jpayne@69: * @see LocalPointer jpayne@69: * @stable ICU 62 jpayne@69: */ jpayne@69: U_DEFINE_LOCAL_OPEN_POINTER(LocalUNumberFormatterPointer, UNumberFormatter, unumf_close); jpayne@69: jpayne@69: /** jpayne@69: * \class LocalUFormattedNumberPointer jpayne@69: * "Smart pointer" class; closes a UFormattedNumber via unumf_closeResult(). jpayne@69: * For most methods see the LocalPointerBase base class. jpayne@69: * jpayne@69: * Usage: jpayne@69: *
jpayne@69:  * LocalUFormattedNumberPointer uformatter(unumf_openResult(...));
jpayne@69:  * // no need to explicitly call unumf_closeResult()
jpayne@69:  * 
jpayne@69: * jpayne@69: * @see LocalPointerBase jpayne@69: * @see LocalPointer jpayne@69: * @stable ICU 62 jpayne@69: */ jpayne@69: U_DEFINE_LOCAL_OPEN_POINTER(LocalUFormattedNumberPointer, UFormattedNumber, unumf_closeResult); jpayne@69: jpayne@69: U_NAMESPACE_END jpayne@69: #endif // U_SHOW_CPLUSPLUS_API jpayne@69: jpayne@69: #endif //__UNUMBERFORMATTER_H__ jpayne@69: #endif /* #if !UCONFIG_NO_FORMATTING */