]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/unicode/unum.h
ICU-62135.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / unum.h
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
b75a7d8f
A
3/*
4*******************************************************************************
b331163b 5* Copyright (C) 1997-2015, International Business Machines Corporation and others.
73c04bcf 6* All Rights Reserved.
b75a7d8f
A
7* Modification History:
8*
9* Date Name Description
10* 06/24/99 helena Integrated Alan's NF enhancements and Java2 bug fixes
11*******************************************************************************
12*/
13
14#ifndef _UNUM
15#define _UNUM
16
17#include "unicode/utypes.h"
18
19#if !UCONFIG_NO_FORMATTING
20
729e4ab9 21#include "unicode/localpointer.h"
374ca955 22#include "unicode/uloc.h"
b331163b 23#include "unicode/ucurr.h"
b75a7d8f
A
24#include "unicode/umisc.h"
25#include "unicode/parseerr.h"
57a6839d
A
26#include "unicode/uformattable.h"
27#include "unicode/udisplaycontext.h"
f3c0d7a5 28#include "unicode/ufieldpositer.h"
57a6839d 29
b75a7d8f
A
30/**
31 * \file
0f5d89e8 32 * \brief C API: Compatibility APIs for number formatting.
b75a7d8f
A
33 *
34 * <h2> Number Format C API </h2>
0f5d89e8
A
35 *
36 * <p><strong>IMPORTANT:</strong> New users with are strongly encouraged to
37 * see if unumberformatter.h fits their use case. Although not deprecated,
38 * this header is provided for backwards compatibility only.
b75a7d8f
A
39 *
40 * Number Format C API Provides functions for
41 * formatting and parsing a number. Also provides methods for
42 * determining which locales have number formats, and what their names
43 * are.
44 * <P>
45 * UNumberFormat helps you to format and parse numbers for any locale.
46 * Your code can be completely independent of the locale conventions
47 * for decimal points, thousands-separators, or even the particular
48 * decimal digits used, or whether the number format is even decimal.
49 * There are different number format styles like decimal, currency,
50 * percent and spellout.
51 * <P>
52 * To format a number for the current Locale, use one of the static
53 * factory methods:
54 * <pre>
55 * \code
56 * UChar myString[20];
57 * double myNumber = 7.0;
58 * UErrorCode status = U_ZERO_ERROR;
59 * UNumberFormat* nf = unum_open(UNUM_DEFAULT, NULL, -1, NULL, NULL, &status);
60 * unum_formatDouble(nf, myNumber, myString, 20, NULL, &status);
61 * printf(" Example 1: %s\n", austrdup(myString) ); //austrdup( a function used to convert UChar* to char*)
62 * \endcode
63 * </pre>
64 * If you are formatting multiple numbers, it is more efficient to get
65 * the format and use it multiple times so that the system doesn't
66 * have to fetch the information about the local language and country
67 * conventions multiple times.
68 * <pre>
69 * \code
70 * uint32_t i, resultlength, reslenneeded;
71 * UErrorCode status = U_ZERO_ERROR;
72 * UFieldPosition pos;
73 * uint32_t a[] = { 123, 3333, -1234567 };
74 * const uint32_t a_len = sizeof(a) / sizeof(a[0]);
75 * UNumberFormat* nf;
76 * UChar* result = NULL;
77 *
78 * nf = unum_open(UNUM_DEFAULT, NULL, -1, NULL, NULL, &status);
79 * for (i = 0; i < a_len; i++) {
80 * resultlength=0;
81 * reslenneeded=unum_format(nf, a[i], NULL, resultlength, &pos, &status);
82 * result = NULL;
83 * if(status==U_BUFFER_OVERFLOW_ERROR){
84 * status=U_ZERO_ERROR;
85 * resultlength=reslenneeded+1;
86 * result=(UChar*)malloc(sizeof(UChar) * resultlength);
87 * unum_format(nf, a[i], result, resultlength, &pos, &status);
88 * }
89 * printf( " Example 2: %s\n", austrdup(result));
90 * free(result);
91 * }
92 * \endcode
93 * </pre>
94 * To format a number for a different Locale, specify it in the
95 * call to unum_open().
96 * <pre>
97 * \code
98 * UNumberFormat* nf = unum_open(UNUM_DEFAULT, NULL, -1, "fr_FR", NULL, &success)
99 * \endcode
100 * </pre>
101 * You can use a NumberFormat API unum_parse() to parse.
102 * <pre>
103 * \code
104 * UErrorCode status = U_ZERO_ERROR;
105 * int32_t pos=0;
106 * int32_t num;
107 * num = unum_parse(nf, str, u_strlen(str), &pos, &status);
108 * \endcode
109 * </pre>
46f4442e
A
110 * Use UNUM_DECIMAL to get the normal number format for that country.
111 * There are other static options available. Use UNUM_CURRENCY
112 * to get the currency number format for that country. Use UNUM_PERCENT
b75a7d8f
A
113 * to get a format for displaying percentages. With this format, a
114 * fraction from 0.53 is displayed as 53%.
115 * <P>
374ca955
A
116 * Use a pattern to create either a DecimalFormat or a RuleBasedNumberFormat
117 * formatter. The pattern must conform to the syntax defined for those
118 * formatters.
119 * <P>
b75a7d8f 120 * You can also control the display of numbers with such function as
51004dcb 121 * unum_getAttributes() and unum_setAttributes(), which let you set the
0f5d89e8 122 * minimum fraction digits, grouping, etc.
b75a7d8f
A
123 * @see UNumberFormatAttributes for more details
124 * <P>
125 * You can also use forms of the parse and format methods with
126 * ParsePosition and UFieldPosition to allow you to:
127 * <ul type=round>
128 * <li>(a) progressively parse through pieces of a string.
129 * <li>(b) align the decimal point and other areas.
130 * </ul>
131 * <p>
132 * It is also possible to change or set the symbols used for a particular
0f5d89e8 133 * locale like the currency symbol, the grouping separator , monetary separator
b75a7d8f
A
134 * etc by making use of functions unum_setSymbols() and unum_getSymbols().
135 */
136
137/** A number formatter.
138 * For usage in C programs.
139 * @stable ICU 2.0
140 */
141typedef void* UNumberFormat;
142
143/** The possible number format styles.
144 * @stable ICU 2.0
145 */
146typedef enum UNumberFormatStyle {
374ca955 147 /**
4388f060 148 * Decimal format defined by a pattern string.
73c04bcf 149 * @stable ICU 3.0
374ca955
A
150 */
151 UNUM_PATTERN_DECIMAL=0,
4388f060
A
152 /**
153 * Decimal format ("normal" style).
154 * @stable ICU 2.0
155 */
b75a7d8f 156 UNUM_DECIMAL=1,
4388f060 157 /**
2ca993e8
A
158 * Currency format (generic).
159 * Defaults to UNUM_CURRENCY_STANDARD style
160 * (using currency symbol, e.g., "$1.00", with non-accounting
161 * style for negative values e.g. using minus sign).
162 * The specific style may be specified using the -cf- locale key.
4388f060
A
163 * @stable ICU 2.0
164 */
b331163b 165 UNUM_CURRENCY=2,
4388f060
A
166 /**
167 * Percent format
168 * @stable ICU 2.0
169 */
b331163b 170 UNUM_PERCENT=3,
4388f060
A
171 /**
172 * Scientific format
173 * @stable ICU 2.1
174 */
b331163b 175 UNUM_SCIENTIFIC=4,
4388f060 176 /**
b331163b
A
177 * Spellout rule-based format. The default ruleset can be specified/changed using
178 * unum_setTextAttribute with UNUM_DEFAULT_RULESET; the available public rulesets
179 * can be listed using unum_getTextAttribute with UNUM_PUBLIC_RULESETS.
4388f060
A
180 * @stable ICU 2.0
181 */
b331163b 182 UNUM_SPELLOUT=5,
374ca955 183 /**
b331163b
A
184 * Ordinal rule-based format . The default ruleset can be specified/changed using
185 * unum_setTextAttribute with UNUM_DEFAULT_RULESET; the available public rulesets
186 * can be listed using unum_getTextAttribute with UNUM_PUBLIC_RULESETS.
73c04bcf 187 * @stable ICU 3.0
374ca955 188 */
b331163b 189 UNUM_ORDINAL=6,
374ca955
A
190 /**
191 * Duration rule-based format
73c04bcf 192 * @stable ICU 3.0
374ca955 193 */
b331163b 194 UNUM_DURATION=7,
729e4ab9 195 /**
4388f060 196 * Numbering system rule-based format
729e4ab9
A
197 * @stable ICU 4.2
198 */
b331163b 199 UNUM_NUMBERING_SYSTEM=8,
374ca955 200 /**
4388f060 201 * Rule-based format defined by a pattern string.
73c04bcf 202 * @stable ICU 3.0
374ca955 203 */
b331163b 204 UNUM_PATTERN_RULEBASED=9,
4388f060
A
205 /**
206 * Currency format with an ISO currency code, e.g., "USD1.00".
207 * @stable ICU 4.8
208 */
b331163b 209 UNUM_CURRENCY_ISO=10,
4388f060
A
210 /**
211 * Currency format with a pluralized currency name,
212 * e.g., "1.00 US dollar" and "3.00 US dollars".
213 * @stable ICU 4.8
214 */
b331163b 215 UNUM_CURRENCY_PLURAL=11,
57a6839d
A
216 /**
217 * Currency format for accounting, e.g., "($3.00)" for
218 * negative currency amount instead of "-$3.00" ({@link #UNUM_CURRENCY}).
2ca993e8 219 * Overrides any style specified using -cf- key in locale.
b331163b 220 * @stable ICU 53
57a6839d 221 */
b331163b 222 UNUM_CURRENCY_ACCOUNTING=12,
b331163b
A
223 /**
224 * Currency format with a currency symbol given CASH usage, e.g.,
225 * "NT$3" instead of "NT$3.23".
2ca993e8 226 * @stable ICU 54
b331163b
A
227 */
228 UNUM_CASH_CURRENCY=13,
b331163b
A
229 /**
230 * Decimal format expressed using compact notation
231 * (short form, corresponds to UNumberCompactStyle=UNUM_SHORT)
232 * e.g. "23K", "45B"
f3c0d7a5 233 * @stable ICU 56
b331163b
A
234 */
235 UNUM_DECIMAL_COMPACT_SHORT=14,
236 /**
237 * Decimal format expressed using compact notation
238 * (long form, corresponds to UNumberCompactStyle=UNUM_LONG)
239 * e.g. "23 thousand", "45 billion"
f3c0d7a5 240 * @stable ICU 56
b331163b
A
241 */
242 UNUM_DECIMAL_COMPACT_LONG=15,
2ca993e8
A
243 /**
244 * Currency format with a currency symbol, e.g., "$1.00",
245 * using non-accounting style for negative values (e.g. minus sign).
246 * Overrides any style specified using -cf- key in locale.
f3c0d7a5 247 * @stable ICU 56
2ca993e8
A
248 */
249 UNUM_CURRENCY_STANDARD=16,
b331163b 250
f3c0d7a5 251#ifndef U_HIDE_DEPRECATED_API
4388f060 252 /**
f3c0d7a5
A
253 * One more than the highest normal UNumberFormatStyle value.
254 * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
4388f060 255 */
2ca993e8 256 UNUM_FORMAT_STYLE_COUNT=17,
f3c0d7a5 257#endif /* U_HIDE_DEPRECATED_API */
b331163b 258
4388f060
A
259 /**
260 * Default format
261 * @stable ICU 2.0
262 */
374ca955 263 UNUM_DEFAULT = UNUM_DECIMAL,
4388f060
A
264 /**
265 * Alias for UNUM_PATTERN_DECIMAL
266 * @stable ICU 3.0
267 */
374ca955 268 UNUM_IGNORE = UNUM_PATTERN_DECIMAL
b75a7d8f
A
269} UNumberFormatStyle;
270
0f5d89e8
A
271/** The possible number format rounding modes.
272 *
273 * <p>
274 * For more detail on rounding modes, see:
275 * http://userguide.icu-project.org/formatparse/numbers/rounding-modes
276 *
277 * @stable ICU 2.0
b75a7d8f
A
278 */
279typedef enum UNumberFormatRoundingMode {
280 UNUM_ROUND_CEILING,
281 UNUM_ROUND_FLOOR,
282 UNUM_ROUND_DOWN,
283 UNUM_ROUND_UP,
4388f060
A
284 /**
285 * Half-even rounding
286 * @stable, ICU 3.8
287 */
288 UNUM_ROUND_HALFEVEN,
289#ifndef U_HIDE_DEPRECATED_API
46f4442e
A
290 /**
291 * Half-even rounding, misspelled name
292 * @deprecated, ICU 3.8
293 */
4388f060
A
294 UNUM_FOUND_HALFEVEN = UNUM_ROUND_HALFEVEN,
295#endif /* U_HIDE_DEPRECATED_API */
57a6839d 296 UNUM_ROUND_HALFDOWN = UNUM_ROUND_HALFEVEN + 1,
46f4442e 297 UNUM_ROUND_HALFUP,
4388f060
A
298 /**
299 * ROUND_UNNECESSARY reports an error if formatted result is not exact.
300 * @stable ICU 4.8
301 */
302 UNUM_ROUND_UNNECESSARY
b75a7d8f
A
303} UNumberFormatRoundingMode;
304
305/** The possible number format pad positions.
306 * @stable ICU 2.0
307 */
308typedef enum UNumberFormatPadPosition {
309 UNUM_PAD_BEFORE_PREFIX,
310 UNUM_PAD_AFTER_PREFIX,
311 UNUM_PAD_BEFORE_SUFFIX,
312 UNUM_PAD_AFTER_SUFFIX
313} UNumberFormatPadPosition;
314
51004dcb
A
315/**
316 * Constants for specifying short or long format.
57a6839d 317 * @stable ICU 51
51004dcb
A
318 */
319typedef enum UNumberCompactStyle {
57a6839d 320 /** @stable ICU 51 */
51004dcb 321 UNUM_SHORT,
57a6839d 322 /** @stable ICU 51 */
51004dcb 323 UNUM_LONG
57a6839d 324 /** @stable ICU 51 */
51004dcb 325} UNumberCompactStyle;
51004dcb 326
4388f060
A
327/**
328 * Constants for specifying currency spacing
329 * @stable ICU 4.8
330 */
331enum UCurrencySpacing {
332 /** @stable ICU 4.8 */
333 UNUM_CURRENCY_MATCH,
334 /** @stable ICU 4.8 */
335 UNUM_CURRENCY_SURROUNDING_MATCH,
336 /** @stable ICU 4.8 */
337 UNUM_CURRENCY_INSERT,
f3c0d7a5
A
338
339 /* Do not conditionalize the following with #ifndef U_HIDE_DEPRECATED_API,
340 * it is needed for layout of DecimalFormatSymbols object. */
341 /**
342 * One more than the highest normal UCurrencySpacing value.
343 * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
344 */
4388f060
A
345 UNUM_CURRENCY_SPACING_COUNT
346};
347typedef enum UCurrencySpacing UCurrencySpacing; /**< @stable ICU 4.8 */
348
349
350/**
351 * FieldPosition and UFieldPosition selectors for format fields
352 * defined by NumberFormat and UNumberFormat.
353 * @stable ICU 49
354 */
355typedef enum UNumberFormatFields {
356 /** @stable ICU 49 */
357 UNUM_INTEGER_FIELD,
358 /** @stable ICU 49 */
359 UNUM_FRACTION_FIELD,
360 /** @stable ICU 49 */
361 UNUM_DECIMAL_SEPARATOR_FIELD,
362 /** @stable ICU 49 */
363 UNUM_EXPONENT_SYMBOL_FIELD,
364 /** @stable ICU 49 */
365 UNUM_EXPONENT_SIGN_FIELD,
366 /** @stable ICU 49 */
367 UNUM_EXPONENT_FIELD,
368 /** @stable ICU 49 */
369 UNUM_GROUPING_SEPARATOR_FIELD,
370 /** @stable ICU 49 */
371 UNUM_CURRENCY_FIELD,
372 /** @stable ICU 49 */
373 UNUM_PERCENT_FIELD,
374 /** @stable ICU 49 */
375 UNUM_PERMILL_FIELD,
376 /** @stable ICU 49 */
377 UNUM_SIGN_FIELD,
f3c0d7a5
A
378#ifndef U_HIDE_DEPRECATED_API
379 /**
380 * One more than the highest normal UNumberFormatFields value.
381 * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
382 */
4388f060 383 UNUM_FIELD_COUNT
f3c0d7a5 384#endif /* U_HIDE_DEPRECATED_API */
4388f060
A
385} UNumberFormatFields;
386
387
b75a7d8f 388/**
374ca955
A
389 * Create and return a new UNumberFormat for formatting and parsing
390 * numbers. A UNumberFormat may be used to format numbers by calling
391 * {@link #unum_format }, and to parse numbers by calling {@link #unum_parse }.
392 * The caller must call {@link #unum_close } when done to release resources
393 * used by this object.
394 * @param style The type of number format to open: one of
57a6839d
A
395 * UNUM_DECIMAL, UNUM_CURRENCY, UNUM_PERCENT, UNUM_SCIENTIFIC,
396 * UNUM_CURRENCY_ISO, UNUM_CURRENCY_PLURAL, UNUM_SPELLOUT,
397 * UNUM_ORDINAL, UNUM_DURATION, UNUM_NUMBERING_SYSTEM,
374ca955
A
398 * UNUM_PATTERN_DECIMAL, UNUM_PATTERN_RULEBASED, or UNUM_DEFAULT.
399 * If UNUM_PATTERN_DECIMAL or UNUM_PATTERN_RULEBASED is passed then the
400 * number format is opened using the given pattern, which must conform
401 * to the syntax described in DecimalFormat or RuleBasedNumberFormat,
402 * respectively.
0f5d89e8
A
403 *
404 * <p><strong>NOTE::</strong> New users with are strongly encouraged to
405 * use unumf_openWithSkeletonAndLocale instead of unum_open.
406 *
374ca955
A
407 * @param pattern A pattern specifying the format to use.
408 * This parameter is ignored unless the style is
409 * UNUM_PATTERN_DECIMAL or UNUM_PATTERN_RULEBASED.
410 * @param patternLength The number of characters in the pattern, or -1
411 * if null-terminated. This parameter is ignored unless the style is
412 * UNUM_PATTERN.
413 * @param locale A locale identifier to use to determine formatting
414 * and parsing conventions, or NULL to use the default locale.
415 * @param parseErr A pointer to a UParseError struct to receive the
416 * details of any parsing errors, or NULL if no parsing error details
417 * are desired.
418 * @param status A pointer to an input-output UErrorCode.
419 * @return A pointer to a newly created UNumberFormat, or NULL if an
420 * error occurred.
421 * @see unum_close
422 * @see DecimalFormat
423 * @stable ICU 2.0
424 */
73c04bcf 425U_STABLE UNumberFormat* U_EXPORT2
b75a7d8f
A
426unum_open( UNumberFormatStyle style,
427 const UChar* pattern,
428 int32_t patternLength,
429 const char* locale,
430 UParseError* parseErr,
431 UErrorCode* status);
432
433
434/**
435* Close a UNumberFormat.
436* Once closed, a UNumberFormat may no longer be used.
437* @param fmt The formatter to close.
438* @stable ICU 2.0
439*/
73c04bcf 440U_STABLE void U_EXPORT2
b75a7d8f
A
441unum_close(UNumberFormat* fmt);
442
729e4ab9
A
443#if U_SHOW_CPLUSPLUS_API
444
445U_NAMESPACE_BEGIN
446
447/**
448 * \class LocalUNumberFormatPointer
449 * "Smart pointer" class, closes a UNumberFormat via unum_close().
450 * For most methods see the LocalPointerBase base class.
451 *
452 * @see LocalPointerBase
453 * @see LocalPointer
454 * @stable ICU 4.4
455 */
456U_DEFINE_LOCAL_OPEN_POINTER(LocalUNumberFormatPointer, UNumberFormat, unum_close);
457
458U_NAMESPACE_END
459
f3c0d7a5 460#endif // U_SHOW_CPLUSPLUS_API
729e4ab9 461
b75a7d8f
A
462/**
463 * Open a copy of a UNumberFormat.
464 * This function performs a deep copy.
465 * @param fmt The format to copy
466 * @param status A pointer to an UErrorCode to receive any errors.
467 * @return A pointer to a UNumberFormat identical to fmt.
468 * @stable ICU 2.0
469 */
73c04bcf 470U_STABLE UNumberFormat* U_EXPORT2
b75a7d8f
A
471unum_clone(const UNumberFormat *fmt,
472 UErrorCode *status);
473
474/**
475* Format an integer using a UNumberFormat.
476* The integer will be formatted according to the UNumberFormat's locale.
477* @param fmt The formatter to use.
478* @param number The number to format.
57a6839d
A
479* @param result A pointer to a buffer to receive the NULL-terminated formatted number. If
480* the formatted number fits into dest but cannot be NULL-terminated (length == resultLength)
481* then the error code is set to U_STRING_NOT_TERMINATED_WARNING. If the formatted number
482* doesn't fit into result then the error code is set to U_BUFFER_OVERFLOW_ERROR.
b75a7d8f
A
483* @param resultLength The maximum size of result.
484* @param pos A pointer to a UFieldPosition. On input, position->field
485* is read. On output, position->beginIndex and position->endIndex indicate
486* the beginning and ending indices of field number position->field, if such
487* a field exists. This parameter may be NULL, in which case no field
488* @param status A pointer to an UErrorCode to receive any errors
489* @return The total buffer size needed; if greater than resultLength, the output was truncated.
374ca955 490* @see unum_formatInt64
b75a7d8f
A
491* @see unum_formatDouble
492* @see unum_parse
374ca955 493* @see unum_parseInt64
b75a7d8f
A
494* @see unum_parseDouble
495* @see UFieldPosition
496* @stable ICU 2.0
497*/
73c04bcf 498U_STABLE int32_t U_EXPORT2
b75a7d8f
A
499unum_format( const UNumberFormat* fmt,
500 int32_t number,
501 UChar* result,
502 int32_t resultLength,
503 UFieldPosition *pos,
504 UErrorCode* status);
505
374ca955
A
506/**
507* Format an int64 using a UNumberFormat.
508* The int64 will be formatted according to the UNumberFormat's locale.
509* @param fmt The formatter to use.
510* @param number The number to format.
57a6839d
A
511* @param result A pointer to a buffer to receive the NULL-terminated formatted number. If
512* the formatted number fits into dest but cannot be NULL-terminated (length == resultLength)
513* then the error code is set to U_STRING_NOT_TERMINATED_WARNING. If the formatted number
514* doesn't fit into result then the error code is set to U_BUFFER_OVERFLOW_ERROR.
374ca955
A
515* @param resultLength The maximum size of result.
516* @param pos A pointer to a UFieldPosition. On input, position->field
517* is read. On output, position->beginIndex and position->endIndex indicate
518* the beginning and ending indices of field number position->field, if such
519* a field exists. This parameter may be NULL, in which case no field
520* @param status A pointer to an UErrorCode to receive any errors
521* @return The total buffer size needed; if greater than resultLength, the output was truncated.
522* @see unum_format
523* @see unum_formatDouble
524* @see unum_parse
525* @see unum_parseInt64
526* @see unum_parseDouble
527* @see UFieldPosition
528* @stable ICU 2.0
529*/
73c04bcf 530U_STABLE int32_t U_EXPORT2
374ca955
A
531unum_formatInt64(const UNumberFormat *fmt,
532 int64_t number,
533 UChar* result,
534 int32_t resultLength,
535 UFieldPosition *pos,
536 UErrorCode* status);
537
b75a7d8f
A
538/**
539* Format a double using a UNumberFormat.
540* The double will be formatted according to the UNumberFormat's locale.
541* @param fmt The formatter to use.
542* @param number The number to format.
57a6839d
A
543* @param result A pointer to a buffer to receive the NULL-terminated formatted number. If
544* the formatted number fits into dest but cannot be NULL-terminated (length == resultLength)
545* then the error code is set to U_STRING_NOT_TERMINATED_WARNING. If the formatted number
546* doesn't fit into result then the error code is set to U_BUFFER_OVERFLOW_ERROR.
b75a7d8f
A
547* @param resultLength The maximum size of result.
548* @param pos A pointer to a UFieldPosition. On input, position->field
549* is read. On output, position->beginIndex and position->endIndex indicate
550* the beginning and ending indices of field number position->field, if such
551* a field exists. This parameter may be NULL, in which case no field
552* @param status A pointer to an UErrorCode to receive any errors
553* @return The total buffer size needed; if greater than resultLength, the output was truncated.
554* @see unum_format
374ca955 555* @see unum_formatInt64
b75a7d8f 556* @see unum_parse
374ca955 557* @see unum_parseInt64
b75a7d8f
A
558* @see unum_parseDouble
559* @see UFieldPosition
560* @stable ICU 2.0
561*/
73c04bcf 562U_STABLE int32_t U_EXPORT2
b75a7d8f
A
563unum_formatDouble( const UNumberFormat* fmt,
564 double number,
565 UChar* result,
566 int32_t resultLength,
567 UFieldPosition *pos, /* 0 if ignore */
568 UErrorCode* status);
569
f3c0d7a5
A
570/**
571* Format a double using a UNumberFormat according to the UNumberFormat's locale,
572* and initialize a UFieldPositionIterator that enumerates the subcomponents of
573* the resulting string.
574*
575* @param format
576* The formatter to use.
577* @param number
578* The number to format.
579* @param result
580* A pointer to a buffer to receive the NULL-terminated formatted
581* number. If the formatted number fits into dest but cannot be
582* NULL-terminated (length == resultLength) then the error code is set
583* to U_STRING_NOT_TERMINATED_WARNING. If the formatted number doesn't
584* fit into result then the error code is set to
585* U_BUFFER_OVERFLOW_ERROR.
586* @param resultLength
587* The maximum size of result.
588* @param fpositer
589* A pointer to a UFieldPositionIterator created by {@link #ufieldpositer_open}
590* (may be NULL if field position information is not needed, but in this
591* case it's preferable to use {@link #unum_formatDouble}). Iteration
592* information already present in the UFieldPositionIterator is deleted,
593* and the iterator is reset to apply to the fields in the formatted
594* string created by this function call. The field values and indexes
595* returned by {@link #ufieldpositer_next} represent fields denoted by
596* the UNumberFormatFields enum. Fields are not returned in a guaranteed
597* order. Fields cannot overlap, but they may nest. For example, 1234
598* could format as "1,234" which might consist of a grouping separator
599* field for ',' and an integer field encompassing the entire string.
600* @param status
601* A pointer to an UErrorCode to receive any errors
602* @return
603* The total buffer size needed; if greater than resultLength, the
604* output was truncated.
605* @see unum_formatDouble
606* @see unum_parse
607* @see unum_parseDouble
608* @see UFieldPositionIterator
609* @see UNumberFormatFields
0f5d89e8 610* @stable ICU 59
f3c0d7a5 611*/
0f5d89e8 612U_STABLE int32_t U_EXPORT2
f3c0d7a5
A
613unum_formatDoubleForFields(const UNumberFormat* format,
614 double number,
615 UChar* result,
616 int32_t resultLength,
617 UFieldPositionIterator* fpositer,
618 UErrorCode* status);
619
f3c0d7a5 620
729e4ab9
A
621/**
622* Format a decimal number using a UNumberFormat.
623* The number will be formatted according to the UNumberFormat's locale.
624* The syntax of the input number is a "numeric string"
625* as defined in the Decimal Arithmetic Specification, available at
626* http://speleotrove.com/decimal
627* @param fmt The formatter to use.
628* @param number The number to format.
629* @param length The length of the input number, or -1 if the input is nul-terminated.
57a6839d
A
630* @param result A pointer to a buffer to receive the NULL-terminated formatted number. If
631* the formatted number fits into dest but cannot be NULL-terminated (length == resultLength)
632* then the error code is set to U_STRING_NOT_TERMINATED_WARNING. If the formatted number
633* doesn't fit into result then the error code is set to U_BUFFER_OVERFLOW_ERROR.
729e4ab9
A
634* @param resultLength The maximum size of result.
635* @param pos A pointer to a UFieldPosition. On input, position->field
636* is read. On output, position->beginIndex and position->endIndex indicate
637* the beginning and ending indices of field number position->field, if such
638* a field exists. This parameter may be NULL, in which case it is ignored.
639* @param status A pointer to an UErrorCode to receive any errors
640* @return The total buffer size needed; if greater than resultLength, the output was truncated.
641* @see unum_format
642* @see unum_formatInt64
643* @see unum_parse
644* @see unum_parseInt64
645* @see unum_parseDouble
646* @see UFieldPosition
647* @stable ICU 4.4
648*/
649U_STABLE int32_t U_EXPORT2
650unum_formatDecimal( const UNumberFormat* fmt,
651 const char * number,
652 int32_t length,
653 UChar* result,
654 int32_t resultLength,
655 UFieldPosition *pos, /* 0 if ignore */
656 UErrorCode* status);
657
374ca955
A
658/**
659 * Format a double currency amount using a UNumberFormat.
660 * The double will be formatted according to the UNumberFormat's locale.
661 * @param fmt the formatter to use
662 * @param number the number to format
663 * @param currency the 3-letter null-terminated ISO 4217 currency code
57a6839d
A
664 * @param result A pointer to a buffer to receive the NULL-terminated formatted number. If
665 * the formatted number fits into dest but cannot be NULL-terminated (length == resultLength)
666 * then the error code is set to U_STRING_NOT_TERMINATED_WARNING. If the formatted number
667 * doesn't fit into result then the error code is set to U_BUFFER_OVERFLOW_ERROR.
374ca955
A
668 * @param resultLength the maximum number of UChars to write to result
669 * @param pos a pointer to a UFieldPosition. On input,
670 * position->field is read. On output, position->beginIndex and
671 * position->endIndex indicate the beginning and ending indices of
672 * field number position->field, if such a field exists. This
673 * parameter may be NULL, in which case it is ignored.
674 * @param status a pointer to an input-output UErrorCode
675 * @return the total buffer size needed; if greater than resultLength,
676 * the output was truncated.
677 * @see unum_formatDouble
678 * @see unum_parseDoubleCurrency
679 * @see UFieldPosition
73c04bcf 680 * @stable ICU 3.0
374ca955 681 */
57a6839d 682U_STABLE int32_t U_EXPORT2
374ca955
A
683unum_formatDoubleCurrency(const UNumberFormat* fmt,
684 double number,
685 UChar* currency,
686 UChar* result,
687 int32_t resultLength,
57a6839d 688 UFieldPosition* pos,
374ca955
A
689 UErrorCode* status);
690
57a6839d
A
691/**
692 * Format a UFormattable into a string.
693 * @param fmt the formatter to use
694 * @param number the number to format, as a UFormattable
695 * @param result A pointer to a buffer to receive the NULL-terminated formatted number. If
696 * the formatted number fits into dest but cannot be NULL-terminated (length == resultLength)
697 * then the error code is set to U_STRING_NOT_TERMINATED_WARNING. If the formatted number
698 * doesn't fit into result then the error code is set to U_BUFFER_OVERFLOW_ERROR.
699 * @param resultLength the maximum number of UChars to write to result
700 * @param pos a pointer to a UFieldPosition. On input,
701 * position->field is read. On output, position->beginIndex and
702 * position->endIndex indicate the beginning and ending indices of
703 * field number position->field, if such a field exists. This
704 * parameter may be NULL, in which case it is ignored.
705 * @param status a pointer to an input-output UErrorCode
706 * @return the total buffer size needed; if greater than resultLength,
707 * the output was truncated. Will return 0 on error.
708 * @see unum_parseToUFormattable
b331163b 709 * @stable ICU 52
57a6839d 710 */
b331163b 711U_STABLE int32_t U_EXPORT2
57a6839d
A
712unum_formatUFormattable(const UNumberFormat* fmt,
713 const UFormattable *number,
714 UChar *result,
715 int32_t resultLength,
716 UFieldPosition *pos,
717 UErrorCode *status);
57a6839d 718
b75a7d8f
A
719/**
720* Parse a string into an integer using a UNumberFormat.
721* The string will be parsed according to the UNumberFormat's locale.
b331163b
A
722* Note: parsing is not supported for styles UNUM_DECIMAL_COMPACT_SHORT
723* and UNUM_DECIMAL_COMPACT_LONG.
b75a7d8f
A
724* @param fmt The formatter to use.
725* @param text The text to parse.
726* @param textLength The length of text, or -1 if null-terminated.
57a6839d
A
727* @param parsePos If not NULL, on input a pointer to an integer specifying the offset at which
728* to begin parsing. If not NULL, on output the offset at which parsing ended.
b75a7d8f
A
729* @param status A pointer to an UErrorCode to receive any errors
730* @return The value of the parsed integer
374ca955 731* @see unum_parseInt64
b75a7d8f
A
732* @see unum_parseDouble
733* @see unum_format
374ca955 734* @see unum_formatInt64
b75a7d8f
A
735* @see unum_formatDouble
736* @stable ICU 2.0
737*/
73c04bcf 738U_STABLE int32_t U_EXPORT2
b75a7d8f
A
739unum_parse( const UNumberFormat* fmt,
740 const UChar* text,
741 int32_t textLength,
742 int32_t *parsePos /* 0 = start */,
743 UErrorCode *status);
744
374ca955
A
745/**
746* Parse a string into an int64 using a UNumberFormat.
747* The string will be parsed according to the UNumberFormat's locale.
b331163b
A
748* Note: parsing is not supported for styles UNUM_DECIMAL_COMPACT_SHORT
749* and UNUM_DECIMAL_COMPACT_LONG.
374ca955
A
750* @param fmt The formatter to use.
751* @param text The text to parse.
752* @param textLength The length of text, or -1 if null-terminated.
57a6839d
A
753* @param parsePos If not NULL, on input a pointer to an integer specifying the offset at which
754* to begin parsing. If not NULL, on output the offset at which parsing ended.
374ca955
A
755* @param status A pointer to an UErrorCode to receive any errors
756* @return The value of the parsed integer
757* @see unum_parse
758* @see unum_parseDouble
759* @see unum_format
760* @see unum_formatInt64
761* @see unum_formatDouble
73c04bcf 762* @stable ICU 2.8
374ca955 763*/
73c04bcf 764U_STABLE int64_t U_EXPORT2
374ca955
A
765unum_parseInt64(const UNumberFormat* fmt,
766 const UChar* text,
767 int32_t textLength,
768 int32_t *parsePos /* 0 = start */,
769 UErrorCode *status);
770
b75a7d8f
A
771/**
772* Parse a string into a double using a UNumberFormat.
773* The string will be parsed according to the UNumberFormat's locale.
b331163b
A
774* Note: parsing is not supported for styles UNUM_DECIMAL_COMPACT_SHORT
775* and UNUM_DECIMAL_COMPACT_LONG.
b75a7d8f
A
776* @param fmt The formatter to use.
777* @param text The text to parse.
778* @param textLength The length of text, or -1 if null-terminated.
57a6839d
A
779* @param parsePos If not NULL, on input a pointer to an integer specifying the offset at which
780* to begin parsing. If not NULL, on output the offset at which parsing ended.
b75a7d8f
A
781* @param status A pointer to an UErrorCode to receive any errors
782* @return The value of the parsed double
783* @see unum_parse
374ca955 784* @see unum_parseInt64
b75a7d8f 785* @see unum_format
374ca955 786* @see unum_formatInt64
b75a7d8f
A
787* @see unum_formatDouble
788* @stable ICU 2.0
789*/
73c04bcf 790U_STABLE double U_EXPORT2
b75a7d8f
A
791unum_parseDouble( const UNumberFormat* fmt,
792 const UChar* text,
793 int32_t textLength,
794 int32_t *parsePos /* 0 = start */,
795 UErrorCode *status);
796
729e4ab9
A
797
798/**
799* Parse a number from a string into an unformatted numeric string using a UNumberFormat.
800* The input string will be parsed according to the UNumberFormat's locale.
801* The syntax of the output is a "numeric string"
802* as defined in the Decimal Arithmetic Specification, available at
803* http://speleotrove.com/decimal
b331163b
A
804* Note: parsing is not supported for styles UNUM_DECIMAL_COMPACT_SHORT
805* and UNUM_DECIMAL_COMPACT_LONG.
729e4ab9
A
806* @param fmt The formatter to use.
807* @param text The text to parse.
808* @param textLength The length of text, or -1 if null-terminated.
57a6839d
A
809* @param parsePos If not NULL, on input a pointer to an integer specifying the offset at which
810* to begin parsing. If not NULL, on output the offset at which parsing ended.
729e4ab9
A
811* @param outBuf A (char *) buffer to receive the parsed number as a string. The output string
812* will be nul-terminated if there is sufficient space.
813* @param outBufLength The size of the output buffer. May be zero, in which case
814* the outBuf pointer may be NULL, and the function will return the
815* size of the output string.
816* @param status A pointer to an UErrorCode to receive any errors
817* @return the length of the output string, not including any terminating nul.
818* @see unum_parse
819* @see unum_parseInt64
820* @see unum_format
821* @see unum_formatInt64
822* @see unum_formatDouble
823* @stable ICU 4.4
824*/
825U_STABLE int32_t U_EXPORT2
826unum_parseDecimal(const UNumberFormat* fmt,
827 const UChar* text,
828 int32_t textLength,
829 int32_t *parsePos /* 0 = start */,
830 char *outBuf,
831 int32_t outBufLength,
832 UErrorCode *status);
833
b75a7d8f 834/**
374ca955
A
835 * Parse a string into a double and a currency using a UNumberFormat.
836 * The string will be parsed according to the UNumberFormat's locale.
837 * @param fmt the formatter to use
838 * @param text the text to parse
839 * @param textLength the length of text, or -1 if null-terminated
840 * @param parsePos a pointer to an offset index into text at which to
841 * begin parsing. On output, *parsePos will point after the last
57a6839d 842 * parsed character. This parameter may be NULL, in which case parsing
374ca955
A
843 * begins at offset 0.
844 * @param currency a pointer to the buffer to receive the parsed null-
845 * terminated currency. This buffer must have a capacity of at least
846 * 4 UChars.
847 * @param status a pointer to an input-output UErrorCode
848 * @return the parsed double
849 * @see unum_parseDouble
850 * @see unum_formatDoubleCurrency
73c04bcf 851 * @stable ICU 3.0
374ca955 852 */
73c04bcf 853U_STABLE double U_EXPORT2
374ca955
A
854unum_parseDoubleCurrency(const UNumberFormat* fmt,
855 const UChar* text,
856 int32_t textLength,
857 int32_t* parsePos, /* 0 = start */
858 UChar* currency,
859 UErrorCode* status);
860
57a6839d
A
861/**
862 * Parse a UChar string into a UFormattable.
863 * Example code:
864 * \snippet test/cintltst/cnumtst.c unum_parseToUFormattable
b331163b
A
865 * Note: parsing is not supported for styles UNUM_DECIMAL_COMPACT_SHORT
866 * and UNUM_DECIMAL_COMPACT_LONG.
57a6839d
A
867 * @param fmt the formatter to use
868 * @param result the UFormattable to hold the result. If NULL, a new UFormattable will be allocated (which the caller must close with ufmt_close).
869 * @param text the text to parse
870 * @param textLength the length of text, or -1 if null-terminated
871 * @param parsePos a pointer to an offset index into text at which to
872 * begin parsing. On output, *parsePos will point after the last
873 * parsed character. This parameter may be NULL in which case parsing
874 * begins at offset 0.
875 * @param status a pointer to an input-output UErrorCode
876 * @return the UFormattable. Will be ==result unless NULL was passed in for result, in which case it will be the newly opened UFormattable.
877 * @see ufmt_getType
878 * @see ufmt_close
b331163b 879 * @stable ICU 52
57a6839d 880 */
b331163b 881U_STABLE UFormattable* U_EXPORT2
57a6839d
A
882unum_parseToUFormattable(const UNumberFormat* fmt,
883 UFormattable *result,
884 const UChar* text,
885 int32_t textLength,
886 int32_t* parsePos, /* 0 = start */
887 UErrorCode* status);
57a6839d 888
374ca955
A
889/**
890 * Set the pattern used by a UNumberFormat. This can only be used
51004dcb 891 * on a DecimalFormat, other formats return U_UNSUPPORTED_ERROR
374ca955
A
892 * in the status.
893 * @param format The formatter to set.
894 * @param localized TRUE if the pattern is localized, FALSE otherwise.
895 * @param pattern The new pattern
896 * @param patternLength The length of pattern, or -1 if null-terminated.
0f5d89e8 897 * @param parseError A pointer to UParseError to receive information
374ca955
A
898 * about errors occurred during parsing, or NULL if no parse error
899 * information is desired.
900 * @param status A pointer to an input-output UErrorCode.
901 * @see unum_toPattern
902 * @see DecimalFormat
903 * @stable ICU 2.0
904 */
73c04bcf 905U_STABLE void U_EXPORT2
b75a7d8f
A
906unum_applyPattern( UNumberFormat *format,
907 UBool localized,
908 const UChar *pattern,
909 int32_t patternLength,
910 UParseError *parseError,
911 UErrorCode *status
912 );
913
914/**
374ca955 915* Get a locale for which decimal formatting patterns are available.
b75a7d8f 916* A UNumberFormat in a locale returned by this function will perform the correct
374ca955
A
917* formatting and parsing for the locale. The results of this call are not
918* valid for rule-based number formats.
729e4ab9 919* @param localeIndex The index of the desired locale.
b75a7d8f
A
920* @return A locale for which number formatting patterns are available, or 0 if none.
921* @see unum_countAvailable
922* @stable ICU 2.0
923*/
73c04bcf 924U_STABLE const char* U_EXPORT2
729e4ab9 925unum_getAvailable(int32_t localeIndex);
b75a7d8f
A
926
927/**
374ca955
A
928* Determine how many locales have decimal formatting patterns available. The
929* results of this call are not valid for rule-based number formats.
930* This function is useful for determining the loop ending condition for
931* calls to {@link #unum_getAvailable }.
932* @return The number of locales for which decimal formatting patterns are available.
b75a7d8f
A
933* @see unum_getAvailable
934* @stable ICU 2.0
935*/
73c04bcf 936U_STABLE int32_t U_EXPORT2
b75a7d8f
A
937unum_countAvailable(void);
938
51004dcb 939#if UCONFIG_HAVE_PARSEALLINPUT
57a6839d 940/* The UNumberFormatAttributeValue type cannot be #ifndef U_HIDE_INTERNAL_API, needed for .h variable declaration */
51004dcb
A
941/**
942 * @internal
943 */
944typedef enum UNumberFormatAttributeValue {
57a6839d 945#ifndef U_HIDE_INTERNAL_API
51004dcb
A
946 /** @internal */
947 UNUM_NO = 0,
948 /** @internal */
949 UNUM_YES = 1,
950 /** @internal */
951 UNUM_MAYBE = 2
2ca993e8
A
952#else
953 /** @internal */
954 UNUM_FORMAT_ATTRIBUTE_VALUE_HIDDEN
57a6839d 955#endif /* U_HIDE_INTERNAL_API */
51004dcb
A
956} UNumberFormatAttributeValue;
957#endif
958
b75a7d8f
A
959/** The possible UNumberFormat numeric attributes @stable ICU 2.0 */
960typedef enum UNumberFormatAttribute {
961 /** Parse integers only */
962 UNUM_PARSE_INT_ONLY,
963 /** Use grouping separator */
964 UNUM_GROUPING_USED,
965 /** Always show decimal point */
966 UNUM_DECIMAL_ALWAYS_SHOWN,
967 /** Maximum integer digits */
968 UNUM_MAX_INTEGER_DIGITS,
969 /** Minimum integer digits */
970 UNUM_MIN_INTEGER_DIGITS,
971 /** Integer digits */
972 UNUM_INTEGER_DIGITS,
973 /** Maximum fraction digits */
974 UNUM_MAX_FRACTION_DIGITS,
975 /** Minimum fraction digits */
976 UNUM_MIN_FRACTION_DIGITS,
977 /** Fraction digits */
978 UNUM_FRACTION_DIGITS,
979 /** Multiplier */
980 UNUM_MULTIPLIER,
981 /** Grouping size */
982 UNUM_GROUPING_SIZE,
983 /** Rounding Mode */
984 UNUM_ROUNDING_MODE,
985 /** Rounding increment */
986 UNUM_ROUNDING_INCREMENT,
987 /** The width to which the output of <code>format()</code> is padded. */
988 UNUM_FORMAT_WIDTH,
989 /** The position at which padding will take place. */
990 UNUM_PADDING_POSITION,
991 /** Secondary grouping size */
374ca955
A
992 UNUM_SECONDARY_GROUPING_SIZE,
993 /** Use significant digits
73c04bcf 994 * @stable ICU 3.0 */
374ca955
A
995 UNUM_SIGNIFICANT_DIGITS_USED,
996 /** Minimum significant digits
73c04bcf 997 * @stable ICU 3.0 */
374ca955
A
998 UNUM_MIN_SIGNIFICANT_DIGITS,
999 /** Maximum significant digits
73c04bcf 1000 * @stable ICU 3.0 */
374ca955
A
1001 UNUM_MAX_SIGNIFICANT_DIGITS,
1002 /** Lenient parse mode used by rule-based formats.
73c04bcf 1003 * @stable ICU 3.0
374ca955 1004 */
51004dcb
A
1005 UNUM_LENIENT_PARSE,
1006#if UCONFIG_HAVE_PARSEALLINPUT
1007 /** Consume all input. (may use fastpath). Set to UNUM_YES (require fastpath), UNUM_NO (skip fastpath), or UNUM_MAYBE (heuristic).
1008 * This is an internal ICU API. Do not use.
1009 * @internal
1010 */
2ca993e8 1011 UNUM_PARSE_ALL_INPUT = 20,
51004dcb 1012#endif
51004dcb
A
1013 /**
1014 * Scale, which adjusts the position of the
1015 * decimal point when formatting. Amounts will be multiplied by 10 ^ (scale)
1016 * before they are formatted. The default value for the scale is 0 ( no adjustment ).
1017 *
1018 * <p>Example: setting the scale to 3, 123 formats as "123,000"
1019 * <p>Example: setting the scale to -4, 123 formats as "0.0123"
1020 *
0f5d89e8
A
1021 * This setting is analogous to getMultiplierScale() and setMultiplierScale() in decimfmt.h.
1022 *
57a6839d 1023 * @stable ICU 51 */
2ca993e8
A
1024 UNUM_SCALE = 21,
1025#ifndef U_HIDE_INTERNAL_API
1026 /**
1027 * Minimum grouping digits, technology preview.
1028 * See DecimalFormat::getMinimumGroupingDigits().
1029 *
1030 * @internal technology preview
1031 */
1032 UNUM_MINIMUM_GROUPING_DIGITS = 22,
1033 /* TODO: test C API when it becomes @draft */
1034#endif /* U_HIDE_INTERNAL_API */
51004dcb
A
1035#ifndef U_HIDE_INTERNAL_API
1036 /** Count of "regular" numeric attributes.
2ca993e8
A
1037 * This internal value was unused by ICU and removed in ICU 56.
1038 * Unfortunately CF references it (but does not use it) so it
1039 * is being temporarily restored per <rdar://problem/23544928>
51004dcb 1040 * @internal */
0f5d89e8
A
1041 UNUM_NUMERIC_ATTRIBUTE_COUNT = 23, /* UNUM_LENIENT_PARSE + 4 */
1042
1043 /** For unum_formatDouble and similar, Boolean that controls
1044 * whether to pin double to string conversion at DBL_DIG=15
1045 * (from <float.h>) significant digits. This was the default
1046 * in open-source ICU before ICU 61,and is currently the default
1047 * in Apple ICU, but can be overriden. <rdar://problem/39240173>.
1048 * @internal */
1049 UNUM_FORMAT_WITH_FULL_PRECISION = 48,
57a6839d 1050#endif /* U_HIDE_INTERNAL_API */
51004dcb 1051
b331163b
A
1052 /**
1053 * if this attribute is set to 0, it is set to UNUM_CURRENCY_STANDARD purpose,
1054 * otherwise it is UNUM_CURRENCY_CASH purpose
1055 * Default: 0 (UNUM_CURRENCY_STANDARD purpose)
2ca993e8 1056 * @stable ICU 54
b331163b 1057 */
2ca993e8 1058 UNUM_CURRENCY_USAGE = 23,
b331163b 1059
57a6839d 1060 /* The following cannot be #ifndef U_HIDE_INTERNAL_API, needed in .h file variable declararions */
51004dcb
A
1061 /** One below the first bitfield-boolean item.
1062 * All items after this one are stored in boolean form.
1063 * @internal */
1064 UNUM_MAX_NONBOOLEAN_ATTRIBUTE = 0x0FFF,
51004dcb 1065
51004dcb
A
1066 /** If 1, specifies that if setting the "max integer digits" attribute would truncate a value, set an error status rather than silently truncating.
1067 * For example, formatting the value 1234 with 4 max int digits would succeed, but formatting 12345 would fail. There is no effect on parsing.
1068 * Default: 0 (not set)
57a6839d 1069 * @stable ICU 50
51004dcb
A
1070 */
1071 UNUM_FORMAT_FAIL_IF_MORE_THAN_MAX_DIGITS = 0x1000,
1072 /**
1073 * if this attribute is set to 1, specifies that, if the pattern doesn't contain an exponent, the exponent will not be parsed. If the pattern does contain an exponent, this attribute has no effect.
1074 * Has no effect on formatting.
1075 * Default: 0 (unset)
57a6839d 1076 * @stable ICU 50
51004dcb 1077 */
0f5d89e8 1078 UNUM_PARSE_NO_EXPONENT = 0x1001,
51004dcb 1079
b331163b
A
1080 /**
1081 * if this attribute is set to 1, specifies that, if the pattern contains a
1082 * decimal mark the input is required to have one. If this attribute is set to 0,
1083 * specifies that input does not have to contain a decimal mark.
1084 * Has no effect on formatting.
1085 * Default: 0 (unset)
2ca993e8 1086 * @stable ICU 54
b331163b 1087 */
2ca993e8 1088 UNUM_PARSE_DECIMAL_MARK_REQUIRED = 0x1002,
b331163b 1089
57a6839d 1090 /* The following cannot be #ifndef U_HIDE_INTERNAL_API, needed in .h file variable declararions */
51004dcb
A
1091 /** Limit of boolean attributes.
1092 * @internal */
0f5d89e8
A
1093 UNUM_LIMIT_BOOLEAN_ATTRIBUTE = 0x1003,
1094
1095 /**
1096 * Whether parsing is sensitive to case (lowercase/uppercase).
1097 * TODO: Add to the test suite.
1098 * @internal This API is a technical preview. It may change in an upcoming release.
1099 */
1100 UNUM_PARSE_CASE_SENSITIVE = 0x1004,
1101
1102 /**
1103 * Formatting: whether to show the plus sign on non-negative numbers.
1104 * TODO: Add to the test suite.
1105 * @internal This API is a technical preview. It may change in an upcoming release.
1106 */
1107 UNUM_SIGN_ALWAYS_SHOWN = 0x1005,
b75a7d8f
A
1108} UNumberFormatAttribute;
1109
1110/**
1111* Get a numeric attribute associated with a UNumberFormat.
1112* An example of a numeric attribute is the number of integer digits a formatter will produce.
1113* @param fmt The formatter to query.
1114* @param attr The attribute to query; one of UNUM_PARSE_INT_ONLY, UNUM_GROUPING_USED,
1115* UNUM_DECIMAL_ALWAYS_SHOWN, UNUM_MAX_INTEGER_DIGITS, UNUM_MIN_INTEGER_DIGITS, UNUM_INTEGER_DIGITS,
1116* UNUM_MAX_FRACTION_DIGITS, UNUM_MIN_FRACTION_DIGITS, UNUM_FRACTION_DIGITS, UNUM_MULTIPLIER,
51004dcb 1117* UNUM_GROUPING_SIZE, UNUM_ROUNDING_MODE, UNUM_FORMAT_WIDTH, UNUM_PADDING_POSITION, UNUM_SECONDARY_GROUPING_SIZE,
2ca993e8 1118* UNUM_SCALE, UNUM_MINIMUM_GROUPING_DIGITS.
b75a7d8f
A
1119* @return The value of attr.
1120* @see unum_setAttribute
1121* @see unum_getDoubleAttribute
1122* @see unum_setDoubleAttribute
1123* @see unum_getTextAttribute
1124* @see unum_setTextAttribute
1125* @stable ICU 2.0
1126*/
73c04bcf 1127U_STABLE int32_t U_EXPORT2
b75a7d8f
A
1128unum_getAttribute(const UNumberFormat* fmt,
1129 UNumberFormatAttribute attr);
1130
1131/**
1132* Set a numeric attribute associated with a UNumberFormat.
374ca955
A
1133* An example of a numeric attribute is the number of integer digits a formatter will produce. If the
1134* formatter does not understand the attribute, the call is ignored. Rule-based formatters only understand
1135* the lenient-parse attribute.
b75a7d8f
A
1136* @param fmt The formatter to set.
1137* @param attr The attribute to set; one of UNUM_PARSE_INT_ONLY, UNUM_GROUPING_USED,
1138* UNUM_DECIMAL_ALWAYS_SHOWN, UNUM_MAX_INTEGER_DIGITS, UNUM_MIN_INTEGER_DIGITS, UNUM_INTEGER_DIGITS,
1139* UNUM_MAX_FRACTION_DIGITS, UNUM_MIN_FRACTION_DIGITS, UNUM_FRACTION_DIGITS, UNUM_MULTIPLIER,
374ca955 1140* UNUM_GROUPING_SIZE, UNUM_ROUNDING_MODE, UNUM_FORMAT_WIDTH, UNUM_PADDING_POSITION, UNUM_SECONDARY_GROUPING_SIZE,
2ca993e8 1141* UNUM_LENIENT_PARSE, UNUM_SCALE, UNUM_MINIMUM_GROUPING_DIGITS.
b75a7d8f
A
1142* @param newValue The new value of attr.
1143* @see unum_getAttribute
1144* @see unum_getDoubleAttribute
1145* @see unum_setDoubleAttribute
1146* @see unum_getTextAttribute
1147* @see unum_setTextAttribute
1148* @stable ICU 2.0
1149*/
73c04bcf 1150U_STABLE void U_EXPORT2
b75a7d8f
A
1151unum_setAttribute( UNumberFormat* fmt,
1152 UNumberFormatAttribute attr,
1153 int32_t newValue);
1154
1155
1156/**
1157* Get a numeric attribute associated with a UNumberFormat.
1158* An example of a numeric attribute is the number of integer digits a formatter will produce.
374ca955 1159* If the formatter does not understand the attribute, -1 is returned.
b75a7d8f
A
1160* @param fmt The formatter to query.
1161* @param attr The attribute to query; e.g. UNUM_ROUNDING_INCREMENT.
1162* @return The value of attr.
1163* @see unum_getAttribute
1164* @see unum_setAttribute
1165* @see unum_setDoubleAttribute
1166* @see unum_getTextAttribute
1167* @see unum_setTextAttribute
1168* @stable ICU 2.0
1169*/
73c04bcf 1170U_STABLE double U_EXPORT2
b75a7d8f
A
1171unum_getDoubleAttribute(const UNumberFormat* fmt,
1172 UNumberFormatAttribute attr);
1173
1174/**
1175* Set a numeric attribute associated with a UNumberFormat.
1176* An example of a numeric attribute is the number of integer digits a formatter will produce.
374ca955 1177* If the formatter does not understand the attribute, this call is ignored.
b75a7d8f
A
1178* @param fmt The formatter to set.
1179* @param attr The attribute to set; e.g. UNUM_ROUNDING_INCREMENT.
1180* @param newValue The new value of attr.
1181* @see unum_getAttribute
1182* @see unum_setAttribute
1183* @see unum_getDoubleAttribute
1184* @see unum_getTextAttribute
1185* @see unum_setTextAttribute
1186* @stable ICU 2.0
1187*/
73c04bcf 1188U_STABLE void U_EXPORT2
b75a7d8f
A
1189unum_setDoubleAttribute( UNumberFormat* fmt,
1190 UNumberFormatAttribute attr,
1191 double newValue);
1192
1193/** The possible UNumberFormat text attributes @stable ICU 2.0*/
1194typedef enum UNumberFormatTextAttribute {
1195 /** Positive prefix */
1196 UNUM_POSITIVE_PREFIX,
1197 /** Positive suffix */
1198 UNUM_POSITIVE_SUFFIX,
1199 /** Negative prefix */
1200 UNUM_NEGATIVE_PREFIX,
1201 /** Negative suffix */
1202 UNUM_NEGATIVE_SUFFIX,
1203 /** The character used to pad to the format width. */
1204 UNUM_PADDING_CHARACTER,
1205 /** The ISO currency code */
374ca955
A
1206 UNUM_CURRENCY_CODE,
1207 /**
b331163b
A
1208 * The default rule set, such as "%spellout-numbering-year:", "%spellout-cardinal:",
1209 * "%spellout-ordinal-masculine-plural:", "%spellout-ordinal-feminine:", or
1210 * "%spellout-ordinal-neuter:". The available public rulesets can be listed using
1211 * unum_getTextAttribute with UNUM_PUBLIC_RULESETS. This is only available with
1212 * rule-based formatters.
73c04bcf 1213 * @stable ICU 3.0
374ca955
A
1214 */
1215 UNUM_DEFAULT_RULESET,
1216 /**
1217 * The public rule sets. This is only available with rule-based formatters.
1218 * This is a read-only attribute. The public rulesets are returned as a
b331163b
A
1219 * single string, with each ruleset name delimited by ';' (semicolon). See the
1220 * CLDR LDML spec for more information about RBNF rulesets:
1221 * http://www.unicode.org/reports/tr35/tr35-numbers.html#Rule-Based_Number_Formatting
73c04bcf 1222 * @stable ICU 3.0
374ca955
A
1223 */
1224 UNUM_PUBLIC_RULESETS
b75a7d8f
A
1225} UNumberFormatTextAttribute;
1226
1227/**
1228* Get a text attribute associated with a UNumberFormat.
374ca955 1229* An example of a text attribute is the suffix for positive numbers. If the formatter
51004dcb 1230* does not understand the attribute, U_UNSUPPORTED_ERROR is returned as the status.
374ca955 1231* Rule-based formatters only understand UNUM_DEFAULT_RULESET and UNUM_PUBLIC_RULESETS.
b75a7d8f
A
1232* @param fmt The formatter to query.
1233* @param tag The attribute to query; one of UNUM_POSITIVE_PREFIX, UNUM_POSITIVE_SUFFIX,
374ca955
A
1234* UNUM_NEGATIVE_PREFIX, UNUM_NEGATIVE_SUFFIX, UNUM_PADDING_CHARACTER, UNUM_CURRENCY_CODE,
1235* UNUM_DEFAULT_RULESET, or UNUM_PUBLIC_RULESETS.
b75a7d8f
A
1236* @param result A pointer to a buffer to receive the attribute.
1237* @param resultLength The maximum size of result.
1238* @param status A pointer to an UErrorCode to receive any errors
1239* @return The total buffer size needed; if greater than resultLength, the output was truncated.
1240* @see unum_setTextAttribute
1241* @see unum_getAttribute
1242* @see unum_setAttribute
1243* @stable ICU 2.0
1244*/
73c04bcf 1245U_STABLE int32_t U_EXPORT2
b75a7d8f
A
1246unum_getTextAttribute( const UNumberFormat* fmt,
1247 UNumberFormatTextAttribute tag,
1248 UChar* result,
1249 int32_t resultLength,
1250 UErrorCode* status);
1251
1252/**
1253* Set a text attribute associated with a UNumberFormat.
374ca955
A
1254* An example of a text attribute is the suffix for positive numbers. Rule-based formatters
1255* only understand UNUM_DEFAULT_RULESET.
b75a7d8f
A
1256* @param fmt The formatter to set.
1257* @param tag The attribute to set; one of UNUM_POSITIVE_PREFIX, UNUM_POSITIVE_SUFFIX,
374ca955
A
1258* UNUM_NEGATIVE_PREFIX, UNUM_NEGATIVE_SUFFIX, UNUM_PADDING_CHARACTER, UNUM_CURRENCY_CODE,
1259* or UNUM_DEFAULT_RULESET.
b75a7d8f
A
1260* @param newValue The new value of attr.
1261* @param newValueLength The length of newValue, or -1 if null-terminated.
1262* @param status A pointer to an UErrorCode to receive any errors
1263* @see unum_getTextAttribute
1264* @see unum_getAttribute
1265* @see unum_setAttribute
1266* @stable ICU 2.0
1267*/
73c04bcf 1268U_STABLE void U_EXPORT2
b75a7d8f
A
1269unum_setTextAttribute( UNumberFormat* fmt,
1270 UNumberFormatTextAttribute tag,
1271 const UChar* newValue,
1272 int32_t newValueLength,
1273 UErrorCode *status);
1274
1275/**
374ca955
A
1276 * Extract the pattern from a UNumberFormat. The pattern will follow
1277 * the DecimalFormat pattern syntax.
1278 * @param fmt The formatter to query.
1279 * @param isPatternLocalized TRUE if the pattern should be localized,
1280 * FALSE otherwise. This is ignored if the formatter is a rule-based
1281 * formatter.
1282 * @param result A pointer to a buffer to receive the pattern.
1283 * @param resultLength The maximum size of result.
1284 * @param status A pointer to an input-output UErrorCode.
1285 * @return The total buffer size needed; if greater than resultLength,
1286 * the output was truncated.
1287 * @see unum_applyPattern
1288 * @see DecimalFormat
1289 * @stable ICU 2.0
1290 */
73c04bcf 1291U_STABLE int32_t U_EXPORT2
b75a7d8f
A
1292unum_toPattern( const UNumberFormat* fmt,
1293 UBool isPatternLocalized,
1294 UChar* result,
1295 int32_t resultLength,
1296 UErrorCode* status);
1297
b75a7d8f
A
1298
1299/**
1300 * Constants for specifying a number format symbol.
1301 * @stable ICU 2.0
1302 */
1303typedef enum UNumberFormatSymbol {
1304 /** The decimal separator */
73c04bcf 1305 UNUM_DECIMAL_SEPARATOR_SYMBOL = 0,
b75a7d8f 1306 /** The grouping separator */
73c04bcf 1307 UNUM_GROUPING_SEPARATOR_SYMBOL = 1,
b75a7d8f 1308 /** The pattern separator */
73c04bcf 1309 UNUM_PATTERN_SEPARATOR_SYMBOL = 2,
b75a7d8f 1310 /** The percent sign */
73c04bcf 1311 UNUM_PERCENT_SYMBOL = 3,
b75a7d8f 1312 /** Zero*/
73c04bcf 1313 UNUM_ZERO_DIGIT_SYMBOL = 4,
b75a7d8f 1314 /** Character representing a digit in the pattern */
73c04bcf 1315 UNUM_DIGIT_SYMBOL = 5,
b75a7d8f 1316 /** The minus sign */
73c04bcf 1317 UNUM_MINUS_SIGN_SYMBOL = 6,
b75a7d8f 1318 /** The plus sign */
73c04bcf 1319 UNUM_PLUS_SIGN_SYMBOL = 7,
b75a7d8f 1320 /** The currency symbol */
73c04bcf 1321 UNUM_CURRENCY_SYMBOL = 8,
b75a7d8f 1322 /** The international currency symbol */
73c04bcf 1323 UNUM_INTL_CURRENCY_SYMBOL = 9,
b75a7d8f 1324 /** The monetary separator */
73c04bcf 1325 UNUM_MONETARY_SEPARATOR_SYMBOL = 10,
b75a7d8f 1326 /** The exponential symbol */
73c04bcf 1327 UNUM_EXPONENTIAL_SYMBOL = 11,
b75a7d8f 1328 /** Per mill symbol */
73c04bcf 1329 UNUM_PERMILL_SYMBOL = 12,
b75a7d8f 1330 /** Escape padding character */
73c04bcf 1331 UNUM_PAD_ESCAPE_SYMBOL = 13,
b75a7d8f 1332 /** Infinity symbol */
73c04bcf 1333 UNUM_INFINITY_SYMBOL = 14,
b75a7d8f 1334 /** Nan symbol */
73c04bcf 1335 UNUM_NAN_SYMBOL = 15,
374ca955 1336 /** Significant digit symbol
73c04bcf
A
1337 * @stable ICU 3.0 */
1338 UNUM_SIGNIFICANT_DIGIT_SYMBOL = 16,
73c04bcf 1339 /** The monetary grouping separator
46f4442e 1340 * @stable ICU 3.6
73c04bcf 1341 */
4388f060 1342 UNUM_MONETARY_GROUPING_SEPARATOR_SYMBOL = 17,
729e4ab9 1343 /** One
4388f060 1344 * @stable ICU 4.6
729e4ab9
A
1345 */
1346 UNUM_ONE_DIGIT_SYMBOL = 18,
1347 /** Two
4388f060 1348 * @stable ICU 4.6
729e4ab9
A
1349 */
1350 UNUM_TWO_DIGIT_SYMBOL = 19,
1351 /** Three
4388f060 1352 * @stable ICU 4.6
729e4ab9
A
1353 */
1354 UNUM_THREE_DIGIT_SYMBOL = 20,
1355 /** Four
4388f060 1356 * @stable ICU 4.6
729e4ab9
A
1357 */
1358 UNUM_FOUR_DIGIT_SYMBOL = 21,
1359 /** Five
4388f060 1360 * @stable ICU 4.6
729e4ab9
A
1361 */
1362 UNUM_FIVE_DIGIT_SYMBOL = 22,
1363 /** Six
4388f060 1364 * @stable ICU 4.6
729e4ab9
A
1365 */
1366 UNUM_SIX_DIGIT_SYMBOL = 23,
1367 /** Seven
4388f060 1368 * @stable ICU 4.6
729e4ab9
A
1369 */
1370 UNUM_SEVEN_DIGIT_SYMBOL = 24,
1371 /** Eight
4388f060 1372 * @stable ICU 4.6
729e4ab9
A
1373 */
1374 UNUM_EIGHT_DIGIT_SYMBOL = 25,
1375 /** Nine
4388f060 1376 * @stable ICU 4.6
729e4ab9
A
1377 */
1378 UNUM_NINE_DIGIT_SYMBOL = 26,
b331163b 1379
b331163b 1380 /** Multiplication sign
2ca993e8 1381 * @stable ICU 54
b331163b
A
1382 */
1383 UNUM_EXPONENT_MULTIPLICATION_SYMBOL = 27,
b331163b 1384
f3c0d7a5
A
1385#ifndef U_HIDE_DEPRECATED_API
1386 /**
1387 * One more than the highest normal UNumberFormatSymbol value.
1388 * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
1389 */
b331163b 1390 UNUM_FORMAT_SYMBOL_COUNT = 28
f3c0d7a5 1391#endif /* U_HIDE_DEPRECATED_API */
b75a7d8f
A
1392} UNumberFormatSymbol;
1393
1394/**
1395* Get a symbol associated with a UNumberFormat.
1396* A UNumberFormat uses symbols to represent the special locale-dependent
374ca955
A
1397* characters in a number, for example the percent sign. This API is not
1398* supported for rule-based formatters.
b75a7d8f
A
1399* @param fmt The formatter to query.
1400* @param symbol The UNumberFormatSymbol constant for the symbol to get
1401* @param buffer The string buffer that will receive the symbol string;
1402* if it is NULL, then only the length of the symbol is returned
1403* @param size The size of the string buffer
1404* @param status A pointer to an UErrorCode to receive any errors
1405* @return The length of the symbol; the buffer is not modified if
1406* <code>length&gt;=size</code>
1407* @see unum_setSymbol
1408* @stable ICU 2.0
1409*/
73c04bcf 1410U_STABLE int32_t U_EXPORT2
374ca955 1411unum_getSymbol(const UNumberFormat *fmt,
b75a7d8f
A
1412 UNumberFormatSymbol symbol,
1413 UChar *buffer,
1414 int32_t size,
1415 UErrorCode *status);
1416
1417/**
1418* Set a symbol associated with a UNumberFormat.
1419* A UNumberFormat uses symbols to represent the special locale-dependent
374ca955
A
1420* characters in a number, for example the percent sign. This API is not
1421* supported for rule-based formatters.
b75a7d8f
A
1422* @param fmt The formatter to set.
1423* @param symbol The UNumberFormatSymbol constant for the symbol to set
1424* @param value The string to set the symbol to
1425* @param length The length of the string, or -1 for a zero-terminated string
1426* @param status A pointer to an UErrorCode to receive any errors.
1427* @see unum_getSymbol
1428* @stable ICU 2.0
1429*/
73c04bcf 1430U_STABLE void U_EXPORT2
b75a7d8f
A
1431unum_setSymbol(UNumberFormat *fmt,
1432 UNumberFormatSymbol symbol,
1433 const UChar *value,
1434 int32_t length,
1435 UErrorCode *status);
1436
1437
b75a7d8f 1438/**
374ca955
A
1439 * Get the locale for this number format object.
1440 * You can choose between valid and actual locale.
1441 * @param fmt The formatter to get the locale from
1442 * @param type type of the locale we're looking for (valid or actual)
1443 * @param status error code for the operation
1444 * @return the locale name
73c04bcf 1445 * @stable ICU 2.8
b75a7d8f 1446 */
73c04bcf 1447U_STABLE const char* U_EXPORT2
374ca955
A
1448unum_getLocaleByType(const UNumberFormat *fmt,
1449 ULocDataLocaleType type,
1450 UErrorCode* status);
b75a7d8f 1451
57a6839d
A
1452/**
1453 * Set a particular UDisplayContext value in the formatter, such as
1454 * UDISPCTX_CAPITALIZATION_FOR_STANDALONE.
1455 * @param fmt The formatter for which to set a UDisplayContext value.
1456 * @param value The UDisplayContext value to set.
1457 * @param status A pointer to an UErrorCode to receive any errors
b331163b 1458 * @stable ICU 53
57a6839d 1459 */
b331163b 1460U_STABLE void U_EXPORT2
57a6839d
A
1461unum_setContext(UNumberFormat* fmt, UDisplayContext value, UErrorCode* status);
1462
1463/**
1464 * Get the formatter's UDisplayContext value for the specified UDisplayContextType,
1465 * such as UDISPCTX_TYPE_CAPITALIZATION.
1466 * @param fmt The formatter to query.
1467 * @param type The UDisplayContextType whose value to return
1468 * @param status A pointer to an UErrorCode to receive any errors
1469 * @return The UDisplayContextValue for the specified type.
b331163b 1470 * @stable ICU 53
57a6839d 1471 */
b331163b 1472U_STABLE UDisplayContext U_EXPORT2
57a6839d
A
1473unum_getContext(const UNumberFormat *fmt, UDisplayContextType type, UErrorCode* status);
1474
b75a7d8f
A
1475#endif /* #if !UCONFIG_NO_FORMATTING */
1476
1477#endif