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