unum.h 55 KB

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