unum.h 54 KB

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