numfmt.h 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. // Copyright (C) 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. ********************************************************************************
  5. * Copyright (C) 1997-2016, International Business Machines Corporation and others.
  6. * All Rights Reserved.
  7. ********************************************************************************
  8. *
  9. * File NUMFMT.H
  10. *
  11. * Modification History:
  12. *
  13. * Date Name Description
  14. * 02/19/97 aliu Converted from java.
  15. * 03/18/97 clhuang Updated per C++ implementation.
  16. * 04/17/97 aliu Changed DigitCount to int per code review.
  17. * 07/20/98 stephen JDK 1.2 sync up. Added scientific support.
  18. * Changed naming conventions to match C++ guidelines
  19. * Derecated Java style constants (eg, INTEGER_FIELD)
  20. ********************************************************************************
  21. */
  22. #ifndef NUMFMT_H
  23. #define NUMFMT_H
  24. #include "unicode/utypes.h"
  25. /**
  26. * \file
  27. * \brief C++ API: Abstract base class for all number formats.
  28. */
  29. #if !UCONFIG_NO_FORMATTING
  30. #include "unicode/unistr.h"
  31. #include "unicode/format.h"
  32. #include "unicode/unum.h" // UNumberFormatStyle
  33. #include "unicode/locid.h"
  34. #include "unicode/stringpiece.h"
  35. #include "unicode/curramt.h"
  36. #include "unicode/udisplaycontext.h"
  37. class NumberFormatTest;
  38. U_NAMESPACE_BEGIN
  39. class SharedNumberFormat;
  40. #if !UCONFIG_NO_SERVICE
  41. class NumberFormatFactory;
  42. class StringEnumeration;
  43. #endif
  44. /**
  45. *
  46. * Abstract base class for all number formats. Provides interface for
  47. * formatting and parsing a number. Also provides methods for
  48. * determining which locales have number formats, and what their names
  49. * are.
  50. * \headerfile unicode/numfmt.h "unicode/numfmt.h"
  51. * <P>
  52. * NumberFormat helps you to format and parse numbers for any locale.
  53. * Your code can be completely independent of the locale conventions
  54. * for decimal points, thousands-separators, or even the particular
  55. * decimal digits used, or whether the number format is even decimal.
  56. * <P>
  57. * To format a number for the current Locale, use one of the static
  58. * factory methods:
  59. * \code
  60. * #include <iostream>
  61. * #include "unicode/numfmt.h"
  62. * #include "unicode/unistr.h"
  63. * #include "unicode/ustream.h"
  64. * using namespace std;
  65. *
  66. * int main() {
  67. * double myNumber = 7.0;
  68. * UnicodeString myString;
  69. * UErrorCode success = U_ZERO_ERROR;
  70. * NumberFormat* nf = NumberFormat::createInstance(success);
  71. * nf->format(myNumber, myString);
  72. * cout << " Example 1: " << myString << endl;
  73. * }
  74. * \endcode
  75. * Note that there are additional factory methods within subclasses of
  76. * NumberFormat.
  77. * <P>
  78. * If you are formatting multiple numbers, it is more efficient to get
  79. * the format and use it multiple times so that the system doesn't
  80. * have to fetch the information about the local language and country
  81. * conventions multiple times.
  82. * \code
  83. * UnicodeString myString;
  84. * UErrorCode success = U_ZERO_ERROR;
  85. * NumberFormat *nf = NumberFormat::createInstance( success );
  86. * for (int32_t number: {123, 3333, -1234567}) {
  87. * nf->format(number, myString);
  88. * myString += "; ";
  89. * }
  90. * cout << " Example 2: " << myString << endl;
  91. * \endcode
  92. * To format a number for a different Locale, specify it in the
  93. * call to \c createInstance().
  94. * \code
  95. * nf = NumberFormat::createInstance(Locale::getFrench(), success);
  96. * \endcode
  97. * You can use a \c NumberFormat to parse also.
  98. * \code
  99. * UErrorCode success;
  100. * Formattable result(-999); // initialized with error code
  101. * nf->parse(myString, result, success);
  102. * \endcode
  103. * Use \c createInstance() to get the normal number format for a \c Locale.
  104. * There are other static factory methods available. Use \c createCurrencyInstance()
  105. * to get the currency number format for that country. Use \c createPercentInstance()
  106. * to get a format for displaying percentages. With this format, a
  107. * fraction from 0.53 is displayed as 53%.
  108. * <P>
  109. * The type of number formatting can be specified by passing a 'style' parameter to \c createInstance().
  110. * For example, use\n
  111. * \c createInstance(locale, UNUM_DECIMAL, errorCode) to get the normal number format,\n
  112. * \c createInstance(locale, UNUM_PERCENT, errorCode) to get a format for displaying percentage,\n
  113. * \c createInstance(locale, UNUM_SCIENTIFIC, errorCode) to get a format for displaying scientific number,\n
  114. * \c createInstance(locale, UNUM_CURRENCY, errorCode) to get the currency number format,
  115. * in which the currency is represented by its symbol, for example, "$3.00".\n
  116. * \c createInstance(locale, UNUM_CURRENCY_ISO, errorCode) to get the currency number format,
  117. * in which the currency is represented by its ISO code, for example "USD3.00".\n
  118. * \c createInstance(locale, UNUM_CURRENCY_PLURAL, errorCode) to get the currency number format,
  119. * in which the currency is represented by its full name in plural format,
  120. * for example, "3.00 US dollars" or "1.00 US dollar".
  121. * <P>
  122. * You can also control the display of numbers with such methods as
  123. * \c getMinimumFractionDigits(). If you want even more control over the
  124. * format or parsing, or want to give your users more control, you can
  125. * try dynamic_casting the \c NumberFormat you get from the factory methods to a
  126. * \c DecimalFormat. This will work for the vast majority of
  127. * countries; just remember to test for NULL in case you
  128. * encounter an unusual one.
  129. * <P>
  130. * You can also use forms of the parse and format methods with
  131. * \c ParsePosition and \c FieldPosition to allow you to:
  132. * <ul type=round>
  133. * <li>(a) progressively parse through pieces of a string.
  134. * <li>(b) align the decimal point and other areas.
  135. * </ul>
  136. * For example, you can align numbers in two ways.
  137. * <P>
  138. * If you are using a monospaced font with spacing for alignment, you
  139. * can pass the \c FieldPosition in your format call, with field =
  140. * \c UNUM_INTEGER_FIELD. On output, \c getEndIndex will be set to the offset
  141. * between the last character of the integer and the decimal. Add
  142. * (desiredSpaceCount - getEndIndex) spaces at the front of the
  143. * string.
  144. * <P>
  145. * If you are using proportional fonts, instead of padding with
  146. * spaces, measure the width of the string in pixels from the start to
  147. * getEndIndex. Then move the pen by (desiredPixelWidth -
  148. * widthToAlignmentPoint) before drawing the text. It also works
  149. * where there is no decimal, but possibly additional characters at
  150. * the end, e.g. with parentheses in negative numbers: "(12)" for -12.
  151. * <p>
  152. * <em>User subclasses are not supported.</em> While clients may write
  153. * subclasses, such code will not necessarily work and will not be
  154. * guaranteed to work stably from release to release.
  155. *
  156. * @stable ICU 2.0
  157. */
  158. class U_I18N_API NumberFormat : public Format {
  159. public:
  160. /**
  161. * Alignment Field constants used to construct a FieldPosition object.
  162. * Signifies that the position of the integer part or fraction part of
  163. * a formatted number should be returned.
  164. *
  165. * Note: as of ICU 4.4, the values in this enum have been extended to
  166. * support identification of all number format fields, not just those
  167. * pertaining to alignment.
  168. *
  169. * These constants are provided for backwards compatibility only.
  170. * Please use the C style constants defined in the header file unum.h.
  171. *
  172. * @see FieldPosition
  173. * @stable ICU 2.0
  174. */
  175. enum EAlignmentFields {
  176. /** @stable ICU 2.0 */
  177. kIntegerField = UNUM_INTEGER_FIELD,
  178. /** @stable ICU 2.0 */
  179. kFractionField = UNUM_FRACTION_FIELD,
  180. /** @stable ICU 2.0 */
  181. kDecimalSeparatorField = UNUM_DECIMAL_SEPARATOR_FIELD,
  182. /** @stable ICU 2.0 */
  183. kExponentSymbolField = UNUM_EXPONENT_SYMBOL_FIELD,
  184. /** @stable ICU 2.0 */
  185. kExponentSignField = UNUM_EXPONENT_SIGN_FIELD,
  186. /** @stable ICU 2.0 */
  187. kExponentField = UNUM_EXPONENT_FIELD,
  188. /** @stable ICU 2.0 */
  189. kGroupingSeparatorField = UNUM_GROUPING_SEPARATOR_FIELD,
  190. /** @stable ICU 2.0 */
  191. kCurrencyField = UNUM_CURRENCY_FIELD,
  192. /** @stable ICU 2.0 */
  193. kPercentField = UNUM_PERCENT_FIELD,
  194. /** @stable ICU 2.0 */
  195. kPermillField = UNUM_PERMILL_FIELD,
  196. /** @stable ICU 2.0 */
  197. kSignField = UNUM_SIGN_FIELD,
  198. /**
  199. * These constants are provided for backwards compatibility only.
  200. * Please use the constants defined in the header file unum.h.
  201. */
  202. /** @stable ICU 2.0 */
  203. INTEGER_FIELD = UNUM_INTEGER_FIELD,
  204. /** @stable ICU 2.0 */
  205. FRACTION_FIELD = UNUM_FRACTION_FIELD
  206. };
  207. /**
  208. * Destructor.
  209. * @stable ICU 2.0
  210. */
  211. virtual ~NumberFormat();
  212. /**
  213. * Return true if the given Format objects are semantically equal.
  214. * Objects of different subclasses are considered unequal.
  215. * @return true if the given Format objects are semantically equal.
  216. * @stable ICU 2.0
  217. */
  218. virtual UBool operator==(const Format& other) const;
  219. using Format::format;
  220. /**
  221. * Format an object to produce a string. This method handles
  222. * Formattable objects with numeric types. If the Formattable
  223. * object type is not a numeric type, then it returns a failing
  224. * UErrorCode.
  225. *
  226. * @param obj The object to format.
  227. * @param appendTo Output parameter to receive result.
  228. * Result is appended to existing contents.
  229. * @param pos On input: an alignment field, if desired.
  230. * On output: the offsets of the alignment field.
  231. * @param status Output param filled with success/failure status.
  232. * @return Reference to 'appendTo' parameter.
  233. * @stable ICU 2.0
  234. */
  235. virtual UnicodeString& format(const Formattable& obj,
  236. UnicodeString& appendTo,
  237. FieldPosition& pos,
  238. UErrorCode& status) const;
  239. /**
  240. * Format an object to produce a string. This method handles
  241. * Formattable objects with numeric types. If the Formattable
  242. * object type is not a numeric type, then it returns a failing
  243. * UErrorCode.
  244. *
  245. * @param obj The object to format.
  246. * @param appendTo Output parameter to receive result.
  247. * Result is appended to existing contents.
  248. * @param posIter On return, can be used to iterate over positions
  249. * of fields generated by this format call. Can be
  250. * NULL.
  251. * @param status Output param filled with success/failure status.
  252. * @return Reference to 'appendTo' parameter.
  253. * @stable ICU 4.4
  254. */
  255. virtual UnicodeString& format(const Formattable& obj,
  256. UnicodeString& appendTo,
  257. FieldPositionIterator* posIter,
  258. UErrorCode& status) const;
  259. /**
  260. * Parse a string to produce an object. This methods handles
  261. * parsing of numeric strings into Formattable objects with numeric
  262. * types.
  263. * <P>
  264. * Before calling, set parse_pos.index to the offset you want to
  265. * start parsing at in the source. After calling, parse_pos.index
  266. * indicates the position after the successfully parsed text. If
  267. * an error occurs, parse_pos.index is unchanged.
  268. * <P>
  269. * When parsing, leading whitespace is discarded (with successful
  270. * parse), while trailing whitespace is left as is.
  271. * <P>
  272. * See Format::parseObject() for more.
  273. *
  274. * @param source The string to be parsed into an object.
  275. * @param result Formattable to be set to the parse result.
  276. * If parse fails, return contents are undefined.
  277. * @param parse_pos The position to start parsing at. Upon return
  278. * this param is set to the position after the
  279. * last character successfully parsed. If the
  280. * source is not parsed successfully, this param
  281. * will remain unchanged.
  282. * @return A newly created Formattable* object, or NULL
  283. * on failure. The caller owns this and should
  284. * delete it when done.
  285. * @stable ICU 2.0
  286. */
  287. virtual void parseObject(const UnicodeString& source,
  288. Formattable& result,
  289. ParsePosition& parse_pos) const;
  290. /**
  291. * Format a double number. These methods call the NumberFormat
  292. * pure virtual format() methods with the default FieldPosition.
  293. *
  294. * @param number The value to be formatted.
  295. * @param appendTo Output parameter to receive result.
  296. * Result is appended to existing contents.
  297. * @return Reference to 'appendTo' parameter.
  298. * @stable ICU 2.0
  299. */
  300. UnicodeString& format( double number,
  301. UnicodeString& appendTo) const;
  302. /**
  303. * Format a long number. These methods call the NumberFormat
  304. * pure virtual format() methods with the default FieldPosition.
  305. *
  306. * @param number The value to be formatted.
  307. * @param appendTo Output parameter to receive result.
  308. * Result is appended to existing contents.
  309. * @return Reference to 'appendTo' parameter.
  310. * @stable ICU 2.0
  311. */
  312. UnicodeString& format( int32_t number,
  313. UnicodeString& appendTo) const;
  314. /**
  315. * Format an int64 number. These methods call the NumberFormat
  316. * pure virtual format() methods with the default FieldPosition.
  317. *
  318. * @param number The value to be formatted.
  319. * @param appendTo Output parameter to receive result.
  320. * Result is appended to existing contents.
  321. * @return Reference to 'appendTo' parameter.
  322. * @stable ICU 2.8
  323. */
  324. UnicodeString& format( int64_t number,
  325. UnicodeString& appendTo) const;
  326. /**
  327. * Format a double number. Concrete subclasses must implement
  328. * these pure virtual methods.
  329. *
  330. * @param number The value to be formatted.
  331. * @param appendTo Output parameter to receive result.
  332. * Result is appended to existing contents.
  333. * @param pos On input: an alignment field, if desired.
  334. * On output: the offsets of the alignment field.
  335. * @return Reference to 'appendTo' parameter.
  336. * @stable ICU 2.0
  337. */
  338. virtual UnicodeString& format(double number,
  339. UnicodeString& appendTo,
  340. FieldPosition& pos) const = 0;
  341. /**
  342. * Format a double number. By default, the parent function simply
  343. * calls the base class and does not return an error status.
  344. * Therefore, the status may be ignored in some subclasses.
  345. *
  346. * @param number The value to be formatted.
  347. * @param appendTo Output parameter to receive result.
  348. * Result is appended to existing contents.
  349. * @param pos On input: an alignment field, if desired.
  350. * On output: the offsets of the alignment field.
  351. * @param status error status
  352. * @return Reference to 'appendTo' parameter.
  353. * @internal
  354. */
  355. virtual UnicodeString& format(double number,
  356. UnicodeString& appendTo,
  357. FieldPosition& pos,
  358. UErrorCode &status) const;
  359. /**
  360. * Format a double number. Subclasses must implement
  361. * this method.
  362. *
  363. * @param number The value to be formatted.
  364. * @param appendTo Output parameter to receive result.
  365. * Result is appended to existing contents.
  366. * @param posIter On return, can be used to iterate over positions
  367. * of fields generated by this format call.
  368. * Can be NULL.
  369. * @param status Output param filled with success/failure status.
  370. * @return Reference to 'appendTo' parameter.
  371. * @stable ICU 4.4
  372. */
  373. virtual UnicodeString& format(double number,
  374. UnicodeString& appendTo,
  375. FieldPositionIterator* posIter,
  376. UErrorCode& status) const;
  377. /**
  378. * Format a long number. Concrete subclasses must implement
  379. * these pure virtual methods.
  380. *
  381. * @param number The value to be formatted.
  382. * @param appendTo Output parameter to receive result.
  383. * Result is appended to existing contents.
  384. * @param pos On input: an alignment field, if desired.
  385. * On output: the offsets of the alignment field.
  386. * @return Reference to 'appendTo' parameter.
  387. * @stable ICU 2.0
  388. */
  389. virtual UnicodeString& format(int32_t number,
  390. UnicodeString& appendTo,
  391. FieldPosition& pos) const = 0;
  392. /**
  393. * Format a long number. Concrete subclasses may override
  394. * this function to provide status return.
  395. *
  396. * @param number The value to be formatted.
  397. * @param appendTo Output parameter to receive result.
  398. * Result is appended to existing contents.
  399. * @param pos On input: an alignment field, if desired.
  400. * On output: the offsets of the alignment field.
  401. * @param status the output status.
  402. * @return Reference to 'appendTo' parameter.
  403. * @internal
  404. */
  405. virtual UnicodeString& format(int32_t number,
  406. UnicodeString& appendTo,
  407. FieldPosition& pos,
  408. UErrorCode &status) const;
  409. /**
  410. * Format an int32 number. Subclasses must implement
  411. * this method.
  412. *
  413. * @param number The value to be formatted.
  414. * @param appendTo Output parameter to receive result.
  415. * Result is appended to existing contents.
  416. * @param posIter On return, can be used to iterate over positions
  417. * of fields generated by this format call.
  418. * Can be NULL.
  419. * @param status Output param filled with success/failure status.
  420. * @return Reference to 'appendTo' parameter.
  421. * @stable ICU 4.4
  422. */
  423. virtual UnicodeString& format(int32_t number,
  424. UnicodeString& appendTo,
  425. FieldPositionIterator* posIter,
  426. UErrorCode& status) const;
  427. /**
  428. * Format an int64 number. (Not abstract to retain compatibility
  429. * with earlier releases, however subclasses should override this
  430. * method as it just delegates to format(int32_t number...);
  431. *
  432. * @param number The value to be formatted.
  433. * @param appendTo Output parameter to receive result.
  434. * Result is appended to existing contents.
  435. * @param pos On input: an alignment field, if desired.
  436. * On output: the offsets of the alignment field.
  437. * @return Reference to 'appendTo' parameter.
  438. * @stable ICU 2.8
  439. */
  440. virtual UnicodeString& format(int64_t number,
  441. UnicodeString& appendTo,
  442. FieldPosition& pos) const;
  443. /**
  444. * Format an int64 number. (Not abstract to retain compatibility
  445. * with earlier releases, however subclasses should override this
  446. * method as it just delegates to format(int32_t number...);
  447. *
  448. * @param number The value to be formatted.
  449. * @param appendTo Output parameter to receive result.
  450. * Result is appended to existing contents.
  451. * @param pos On input: an alignment field, if desired.
  452. * On output: the offsets of the alignment field.
  453. * @param status Output param filled with success/failure status.
  454. * @return Reference to 'appendTo' parameter.
  455. * @internal
  456. */
  457. virtual UnicodeString& format(int64_t number,
  458. UnicodeString& appendTo,
  459. FieldPosition& pos,
  460. UErrorCode& status) const;
  461. /**
  462. * Format an int64 number. Subclasses must implement
  463. * this method.
  464. *
  465. * @param number The value to be formatted.
  466. * @param appendTo Output parameter to receive result.
  467. * Result is appended to existing contents.
  468. * @param posIter On return, can be used to iterate over positions
  469. * of fields generated by this format call.
  470. * Can be NULL.
  471. * @param status Output param filled with success/failure status.
  472. * @return Reference to 'appendTo' parameter.
  473. * @stable ICU 4.4
  474. */
  475. virtual UnicodeString& format(int64_t number,
  476. UnicodeString& appendTo,
  477. FieldPositionIterator* posIter,
  478. UErrorCode& status) const;
  479. /**
  480. * Format a decimal number. Subclasses must implement
  481. * this method. The syntax of the unformatted number is a "numeric string"
  482. * as defined in the Decimal Arithmetic Specification, available at
  483. * http://speleotrove.com/decimal
  484. *
  485. * @param number The unformatted number, as a string, to be formatted.
  486. * @param appendTo Output parameter to receive result.
  487. * Result is appended to existing contents.
  488. * @param posIter On return, can be used to iterate over positions
  489. * of fields generated by this format call.
  490. * Can be NULL.
  491. * @param status Output param filled with success/failure status.
  492. * @return Reference to 'appendTo' parameter.
  493. * @stable ICU 4.4
  494. */
  495. virtual UnicodeString& format(StringPiece number,
  496. UnicodeString& appendTo,
  497. FieldPositionIterator* posIter,
  498. UErrorCode& status) const;
  499. public:
  500. /**
  501. * Format a decimal number.
  502. * The number is a DigitList wrapper onto a floating point decimal number.
  503. * The default implementation in NumberFormat converts the decimal number
  504. * to a double and formats that. Subclasses of NumberFormat that want
  505. * to specifically handle big decimal numbers must override this method.
  506. * class DecimalFormat does so.
  507. *
  508. * @param number The number, a DigitList format Decimal Floating Point.
  509. * @param appendTo Output parameter to receive result.
  510. * Result is appended to existing contents.
  511. * @param posIter On return, can be used to iterate over positions
  512. * of fields generated by this format call.
  513. * @param status Output param filled with success/failure status.
  514. * @return Reference to 'appendTo' parameter.
  515. * @internal
  516. */
  517. virtual UnicodeString& format(const DigitList &number,
  518. UnicodeString& appendTo,
  519. FieldPositionIterator* posIter,
  520. UErrorCode& status) const;
  521. /**
  522. * Format a decimal number.
  523. * The number is a DigitList wrapper onto a floating point decimal number.
  524. * The default implementation in NumberFormat converts the decimal number
  525. * to a double and formats that. Subclasses of NumberFormat that want
  526. * to specifically handle big decimal numbers must override this method.
  527. * class DecimalFormat does so.
  528. *
  529. * @param number The number, a DigitList format Decimal Floating Point.
  530. * @param appendTo Output parameter to receive result.
  531. * Result is appended to existing contents.
  532. * @param pos On input: an alignment field, if desired.
  533. * On output: the offsets of the alignment field.
  534. * @param status Output param filled with success/failure status.
  535. * @return Reference to 'appendTo' parameter.
  536. * @internal
  537. */
  538. virtual UnicodeString& format(const DigitList &number,
  539. UnicodeString& appendTo,
  540. FieldPosition& pos,
  541. UErrorCode& status) const;
  542. public:
  543. /**
  544. * Return a long if possible (e.g. within range LONG_MAX,
  545. * LONG_MAX], and with no decimals), otherwise a double. If
  546. * IntegerOnly is set, will stop at a decimal point (or equivalent;
  547. * e.g. for rational numbers "1 2/3", will stop after the 1).
  548. * <P>
  549. * If no object can be parsed, index is unchanged, and NULL is
  550. * returned.
  551. * <P>
  552. * This is a pure virtual which concrete subclasses must implement.
  553. *
  554. * @param text The text to be parsed.
  555. * @param result Formattable to be set to the parse result.
  556. * If parse fails, return contents are undefined.
  557. * @param parsePosition The position to start parsing at on input.
  558. * On output, moved to after the last successfully
  559. * parse character. On parse failure, does not change.
  560. * @stable ICU 2.0
  561. */
  562. virtual void parse(const UnicodeString& text,
  563. Formattable& result,
  564. ParsePosition& parsePosition) const = 0;
  565. /**
  566. * Parse a string as a numeric value, and return a Formattable
  567. * numeric object. This method parses integers only if IntegerOnly
  568. * is set.
  569. *
  570. * @param text The text to be parsed.
  571. * @param result Formattable to be set to the parse result.
  572. * If parse fails, return contents are undefined.
  573. * @param status Output parameter set to a failure error code
  574. * when a failure occurs.
  575. * @see NumberFormat::isParseIntegerOnly
  576. * @stable ICU 2.0
  577. */
  578. virtual void parse(const UnicodeString& text,
  579. Formattable& result,
  580. UErrorCode& status) const;
  581. /**
  582. * Parses text from the given string as a currency amount. Unlike
  583. * the parse() method, this method will attempt to parse a generic
  584. * currency name, searching for a match of this object's locale's
  585. * currency display names, or for a 3-letter ISO currency code.
  586. * This method will fail if this format is not a currency format,
  587. * that is, if it does not contain the currency pattern symbol
  588. * (U+00A4) in its prefix or suffix.
  589. *
  590. * @param text the string to parse
  591. * @param pos input-output position; on input, the position within text
  592. * to match; must have 0 <= pos.getIndex() < text.length();
  593. * on output, the position after the last matched character.
  594. * If the parse fails, the position in unchanged upon output.
  595. * @return if parse succeeds, a pointer to a newly-created CurrencyAmount
  596. * object (owned by the caller) containing information about
  597. * the parsed currency; if parse fails, this is NULL.
  598. * @stable ICU 49
  599. */
  600. virtual CurrencyAmount* parseCurrency(const UnicodeString& text,
  601. ParsePosition& pos) const;
  602. /**
  603. * Return true if this format will parse numbers as integers
  604. * only. For example in the English locale, with ParseIntegerOnly
  605. * true, the string "1234." would be parsed as the integer value
  606. * 1234 and parsing would stop at the "." character. Of course,
  607. * the exact format accepted by the parse operation is locale
  608. * dependant and determined by sub-classes of NumberFormat.
  609. * @return true if this format will parse numbers as integers
  610. * only.
  611. * @stable ICU 2.0
  612. */
  613. UBool isParseIntegerOnly(void) const;
  614. /**
  615. * Sets whether or not numbers should be parsed as integers only.
  616. * @param value set True, this format will parse numbers as integers
  617. * only.
  618. * @see isParseIntegerOnly
  619. * @stable ICU 2.0
  620. */
  621. virtual void setParseIntegerOnly(UBool value);
  622. /**
  623. * Sets whether lenient parsing should be enabled (it is off by default).
  624. *
  625. * @param enable \c TRUE if lenient parsing should be used,
  626. * \c FALSE otherwise.
  627. * @stable ICU 4.8
  628. */
  629. virtual void setLenient(UBool enable);
  630. /**
  631. * Returns whether lenient parsing is enabled (it is off by default).
  632. *
  633. * @return \c TRUE if lenient parsing is enabled,
  634. * \c FALSE otherwise.
  635. * @see #setLenient
  636. * @stable ICU 4.8
  637. */
  638. virtual UBool isLenient(void) const;
  639. /**
  640. * Create a default style NumberFormat for the current default locale.
  641. * The default formatting style is locale dependent.
  642. * @stable ICU 2.0
  643. */
  644. static NumberFormat* U_EXPORT2 createInstance(UErrorCode&);
  645. /**
  646. * Create a default style NumberFormat for the specified locale.
  647. * The default formatting style is locale dependent.
  648. * @param inLocale the given locale.
  649. * @stable ICU 2.0
  650. */
  651. static NumberFormat* U_EXPORT2 createInstance(const Locale& inLocale,
  652. UErrorCode&);
  653. /**
  654. * Create a specific style NumberFormat for the specified locale.
  655. * @param desiredLocale the given locale.
  656. * @param style the given style.
  657. * @param errorCode Output param filled with success/failure status.
  658. * @return A new NumberFormat instance.
  659. * @stable ICU 4.8
  660. */
  661. static NumberFormat* U_EXPORT2 createInstance(const Locale& desiredLocale,
  662. UNumberFormatStyle style,
  663. UErrorCode& errorCode);
  664. #ifndef U_HIDE_INTERNAL_API
  665. /**
  666. * ICU use only.
  667. * Creates NumberFormat instance without using the cache.
  668. * @internal
  669. */
  670. static NumberFormat* internalCreateInstance(
  671. const Locale& desiredLocale,
  672. UNumberFormatStyle style,
  673. UErrorCode& errorCode);
  674. /**
  675. * ICU use only.
  676. * Returns handle to the shared, cached NumberFormat instance for given
  677. * locale. On success, caller must call removeRef() on returned value
  678. * once it is done with the shared instance.
  679. * @internal
  680. */
  681. static const SharedNumberFormat* U_EXPORT2 createSharedInstance(
  682. const Locale& inLocale, UNumberFormatStyle style, UErrorCode& status);
  683. #endif /* U_HIDE_INTERNAL_API */
  684. /**
  685. * Returns a currency format for the current default locale.
  686. * @stable ICU 2.0
  687. */
  688. static NumberFormat* U_EXPORT2 createCurrencyInstance(UErrorCode&);
  689. /**
  690. * Returns a currency format for the specified locale.
  691. * @param inLocale the given locale.
  692. * @stable ICU 2.0
  693. */
  694. static NumberFormat* U_EXPORT2 createCurrencyInstance(const Locale& inLocale,
  695. UErrorCode&);
  696. /**
  697. * Returns a percentage format for the current default locale.
  698. * @stable ICU 2.0
  699. */
  700. static NumberFormat* U_EXPORT2 createPercentInstance(UErrorCode&);
  701. /**
  702. * Returns a percentage format for the specified locale.
  703. * @param inLocale the given locale.
  704. * @stable ICU 2.0
  705. */
  706. static NumberFormat* U_EXPORT2 createPercentInstance(const Locale& inLocale,
  707. UErrorCode&);
  708. /**
  709. * Returns a scientific format for the current default locale.
  710. * @stable ICU 2.0
  711. */
  712. static NumberFormat* U_EXPORT2 createScientificInstance(UErrorCode&);
  713. /**
  714. * Returns a scientific format for the specified locale.
  715. * @param inLocale the given locale.
  716. * @stable ICU 2.0
  717. */
  718. static NumberFormat* U_EXPORT2 createScientificInstance(const Locale& inLocale,
  719. UErrorCode&);
  720. /**
  721. * Get the set of Locales for which NumberFormats are installed.
  722. * @param count Output param to receive the size of the locales
  723. * @stable ICU 2.0
  724. */
  725. static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
  726. #if !UCONFIG_NO_SERVICE
  727. /**
  728. * Register a new NumberFormatFactory. The factory will be adopted.
  729. * Because ICU may choose to cache NumberFormat objects internally,
  730. * this must be called at application startup, prior to any calls to
  731. * NumberFormat::createInstance to avoid undefined behavior.
  732. * @param toAdopt the NumberFormatFactory instance to be adopted
  733. * @param status the in/out status code, no special meanings are assigned
  734. * @return a registry key that can be used to unregister this factory
  735. * @stable ICU 2.6
  736. */
  737. static URegistryKey U_EXPORT2 registerFactory(NumberFormatFactory* toAdopt, UErrorCode& status);
  738. /**
  739. * Unregister a previously-registered NumberFormatFactory using the key returned from the
  740. * register call. Key becomes invalid after a successful call and should not be used again.
  741. * The NumberFormatFactory corresponding to the key will be deleted.
  742. * Because ICU may choose to cache NumberFormat objects internally,
  743. * this should be called during application shutdown, after all calls to
  744. * NumberFormat::createInstance to avoid undefined behavior.
  745. * @param key the registry key returned by a previous call to registerFactory
  746. * @param status the in/out status code, no special meanings are assigned
  747. * @return TRUE if the factory for the key was successfully unregistered
  748. * @stable ICU 2.6
  749. */
  750. static UBool U_EXPORT2 unregister(URegistryKey key, UErrorCode& status);
  751. /**
  752. * Return a StringEnumeration over the locales available at the time of the call,
  753. * including registered locales.
  754. * @return a StringEnumeration over the locales available at the time of the call
  755. * @stable ICU 2.6
  756. */
  757. static StringEnumeration* U_EXPORT2 getAvailableLocales(void);
  758. #endif /* UCONFIG_NO_SERVICE */
  759. /**
  760. * Returns true if grouping is used in this format. For example,
  761. * in the English locale, with grouping on, the number 1234567
  762. * might be formatted as "1,234,567". The grouping separator as
  763. * well as the size of each group is locale dependant and is
  764. * determined by sub-classes of NumberFormat.
  765. * @see setGroupingUsed
  766. * @stable ICU 2.0
  767. */
  768. UBool isGroupingUsed(void) const;
  769. /**
  770. * Set whether or not grouping will be used in this format.
  771. * @param newValue True, grouping will be used in this format.
  772. * @see getGroupingUsed
  773. * @stable ICU 2.0
  774. */
  775. virtual void setGroupingUsed(UBool newValue);
  776. /**
  777. * Returns the maximum number of digits allowed in the integer portion of a
  778. * number.
  779. * @return the maximum number of digits allowed in the integer portion of a
  780. * number.
  781. * @see setMaximumIntegerDigits
  782. * @stable ICU 2.0
  783. */
  784. int32_t getMaximumIntegerDigits(void) const;
  785. /**
  786. * Sets the maximum number of digits allowed in the integer portion of a
  787. * number. maximumIntegerDigits must be >= minimumIntegerDigits. If the
  788. * new value for maximumIntegerDigits is less than the current value
  789. * of minimumIntegerDigits, then minimumIntegerDigits will also be set to
  790. * the new value.
  791. *
  792. * @param newValue the new value for the maximum number of digits
  793. * allowed in the integer portion of a number.
  794. * @see getMaximumIntegerDigits
  795. * @stable ICU 2.0
  796. */
  797. virtual void setMaximumIntegerDigits(int32_t newValue);
  798. /**
  799. * Returns the minimum number of digits allowed in the integer portion of a
  800. * number.
  801. * @return the minimum number of digits allowed in the integer portion of a
  802. * number.
  803. * @see setMinimumIntegerDigits
  804. * @stable ICU 2.0
  805. */
  806. int32_t getMinimumIntegerDigits(void) const;
  807. /**
  808. * Sets the minimum number of digits allowed in the integer portion of a
  809. * number. minimumIntegerDigits must be &lt;= maximumIntegerDigits. If the
  810. * new value for minimumIntegerDigits exceeds the current value
  811. * of maximumIntegerDigits, then maximumIntegerDigits will also be set to
  812. * the new value.
  813. * @param newValue the new value to be set.
  814. * @see getMinimumIntegerDigits
  815. * @stable ICU 2.0
  816. */
  817. virtual void setMinimumIntegerDigits(int32_t newValue);
  818. /**
  819. * Returns the maximum number of digits allowed in the fraction portion of a
  820. * number.
  821. * @return the maximum number of digits allowed in the fraction portion of a
  822. * number.
  823. * @see setMaximumFractionDigits
  824. * @stable ICU 2.0
  825. */
  826. int32_t getMaximumFractionDigits(void) const;
  827. /**
  828. * Sets the maximum number of digits allowed in the fraction portion of a
  829. * number. maximumFractionDigits must be >= minimumFractionDigits. If the
  830. * new value for maximumFractionDigits is less than the current value
  831. * of minimumFractionDigits, then minimumFractionDigits will also be set to
  832. * the new value.
  833. * @param newValue the new value to be set.
  834. * @see getMaximumFractionDigits
  835. * @stable ICU 2.0
  836. */
  837. virtual void setMaximumFractionDigits(int32_t newValue);
  838. /**
  839. * Returns the minimum number of digits allowed in the fraction portion of a
  840. * number.
  841. * @return the minimum number of digits allowed in the fraction portion of a
  842. * number.
  843. * @see setMinimumFractionDigits
  844. * @stable ICU 2.0
  845. */
  846. int32_t getMinimumFractionDigits(void) const;
  847. /**
  848. * Sets the minimum number of digits allowed in the fraction portion of a
  849. * number. minimumFractionDigits must be &lt;= maximumFractionDigits. If the
  850. * new value for minimumFractionDigits exceeds the current value
  851. * of maximumFractionDigits, then maximumIntegerDigits will also be set to
  852. * the new value
  853. * @param newValue the new value to be set.
  854. * @see getMinimumFractionDigits
  855. * @stable ICU 2.0
  856. */
  857. virtual void setMinimumFractionDigits(int32_t newValue);
  858. /**
  859. * Sets the currency used to display currency
  860. * amounts. This takes effect immediately, if this format is a
  861. * currency format. If this format is not a currency format, then
  862. * the currency is used if and when this object becomes a
  863. * currency format.
  864. * @param theCurrency a 3-letter ISO code indicating new currency
  865. * to use. It need not be null-terminated. May be the empty
  866. * string or NULL to indicate no currency.
  867. * @param ec input-output error code
  868. * @stable ICU 3.0
  869. */
  870. virtual void setCurrency(const UChar* theCurrency, UErrorCode& ec);
  871. /**
  872. * Gets the currency used to display currency
  873. * amounts. This may be an empty string for some subclasses.
  874. * @return a 3-letter null-terminated ISO code indicating
  875. * the currency in use, or a pointer to the empty string.
  876. * @stable ICU 2.6
  877. */
  878. const UChar* getCurrency() const;
  879. /**
  880. * Set a particular UDisplayContext value in the formatter, such as
  881. * UDISPCTX_CAPITALIZATION_FOR_STANDALONE.
  882. * @param value The UDisplayContext value to set.
  883. * @param status Input/output status. If at entry this indicates a failure
  884. * status, the function will do nothing; otherwise this will be
  885. * updated with any new status from the function.
  886. * @stable ICU 53
  887. */
  888. virtual void setContext(UDisplayContext value, UErrorCode& status);
  889. /**
  890. * Get the formatter's UDisplayContext value for the specified UDisplayContextType,
  891. * such as UDISPCTX_TYPE_CAPITALIZATION.
  892. * @param type The UDisplayContextType whose value to return
  893. * @param status Input/output status. If at entry this indicates a failure
  894. * status, the function will do nothing; otherwise this will be
  895. * updated with any new status from the function.
  896. * @return The UDisplayContextValue for the specified type.
  897. * @stable ICU 53
  898. */
  899. virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const;
  900. public:
  901. /**
  902. * Return the class ID for this class. This is useful for
  903. * comparing to a return value from getDynamicClassID(). Note that,
  904. * because NumberFormat is an abstract base class, no fully constructed object
  905. * will have the class ID returned by NumberFormat::getStaticClassID().
  906. * @return The class ID for all objects of this class.
  907. * @stable ICU 2.0
  908. */
  909. static UClassID U_EXPORT2 getStaticClassID(void);
  910. /**
  911. * Returns a unique class ID POLYMORPHICALLY. Pure virtual override.
  912. * This method is to implement a simple version of RTTI, since not all
  913. * C++ compilers support genuine RTTI. Polymorphic operator==() and
  914. * clone() methods call this method.
  915. * <P>
  916. * @return The class ID for this object. All objects of a
  917. * given class have the same class ID. Objects of
  918. * other classes have different class IDs.
  919. * @stable ICU 2.0
  920. */
  921. virtual UClassID getDynamicClassID(void) const = 0;
  922. protected:
  923. /**
  924. * Default constructor for subclass use only.
  925. * @stable ICU 2.0
  926. */
  927. NumberFormat();
  928. /**
  929. * Copy constructor.
  930. * @stable ICU 2.0
  931. */
  932. NumberFormat(const NumberFormat&);
  933. /**
  934. * Assignment operator.
  935. * @stable ICU 2.0
  936. */
  937. NumberFormat& operator=(const NumberFormat&);
  938. /**
  939. * Returns the currency in effect for this formatter. Subclasses
  940. * should override this method as needed. Unlike getCurrency(),
  941. * this method should never return "".
  942. * @result output parameter for null-terminated result, which must
  943. * have a capacity of at least 4
  944. * @internal
  945. */
  946. virtual void getEffectiveCurrency(UChar* result, UErrorCode& ec) const;
  947. #ifndef U_HIDE_INTERNAL_API
  948. /**
  949. * Creates the specified number format style of the desired locale.
  950. * If mustBeDecimalFormat is TRUE, then the returned pointer is
  951. * either a DecimalFormat or it is NULL.
  952. * @internal
  953. */
  954. static NumberFormat* makeInstance(const Locale& desiredLocale,
  955. UNumberFormatStyle style,
  956. UBool mustBeDecimalFormat,
  957. UErrorCode& errorCode);
  958. #endif /* U_HIDE_INTERNAL_API */
  959. private:
  960. static UBool isStyleSupported(UNumberFormatStyle style);
  961. /**
  962. * Creates the specified decimal format style of the desired locale.
  963. * @param desiredLocale the given locale.
  964. * @param style the given style.
  965. * @param errorCode Output param filled with success/failure status.
  966. * @return A new NumberFormat instance.
  967. */
  968. static NumberFormat* makeInstance(const Locale& desiredLocale,
  969. UNumberFormatStyle style,
  970. UErrorCode& errorCode);
  971. UBool fGroupingUsed;
  972. int32_t fMaxIntegerDigits;
  973. int32_t fMinIntegerDigits;
  974. int32_t fMaxFractionDigits;
  975. int32_t fMinFractionDigits;
  976. protected:
  977. /** \internal */
  978. static const int32_t gDefaultMaxIntegerDigits;
  979. /** \internal */
  980. static const int32_t gDefaultMinIntegerDigits;
  981. private:
  982. UBool fParseIntegerOnly;
  983. UBool fLenient; // TRUE => lenient parse is enabled
  984. // ISO currency code
  985. UChar fCurrency[4];
  986. UDisplayContext fCapitalizationContext;
  987. friend class ICUNumberFormatFactory; // access to makeInstance
  988. friend class ICUNumberFormatService;
  989. friend class ::NumberFormatTest; // access to isStyleSupported()
  990. };
  991. #if !UCONFIG_NO_SERVICE
  992. /**
  993. * A NumberFormatFactory is used to register new number formats. The factory
  994. * should be able to create any of the predefined formats for each locale it
  995. * supports. When registered, the locales it supports extend or override the
  996. * locale already supported by ICU.
  997. *
  998. * @stable ICU 2.6
  999. */
  1000. class U_I18N_API NumberFormatFactory : public UObject {
  1001. public:
  1002. /**
  1003. * Destructor
  1004. * @stable ICU 3.0
  1005. */
  1006. virtual ~NumberFormatFactory();
  1007. /**
  1008. * Return true if this factory will be visible. Default is true.
  1009. * If not visible, the locales supported by this factory will not
  1010. * be listed by getAvailableLocales.
  1011. * @stable ICU 2.6
  1012. */
  1013. virtual UBool visible(void) const = 0;
  1014. /**
  1015. * Return the locale names directly supported by this factory. The number of names
  1016. * is returned in count;
  1017. * @stable ICU 2.6
  1018. */
  1019. virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const = 0;
  1020. /**
  1021. * Return a number format of the appropriate type. If the locale
  1022. * is not supported, return null. If the locale is supported, but
  1023. * the type is not provided by this service, return null. Otherwise
  1024. * return an appropriate instance of NumberFormat.
  1025. * @stable ICU 2.6
  1026. */
  1027. virtual NumberFormat* createFormat(const Locale& loc, UNumberFormatStyle formatType) = 0;
  1028. };
  1029. /**
  1030. * A NumberFormatFactory that supports a single locale. It can be visible or invisible.
  1031. * @stable ICU 2.6
  1032. */
  1033. class U_I18N_API SimpleNumberFormatFactory : public NumberFormatFactory {
  1034. protected:
  1035. /**
  1036. * True if the locale supported by this factory is visible.
  1037. * @stable ICU 2.6
  1038. */
  1039. const UBool _visible;
  1040. /**
  1041. * The locale supported by this factory, as a UnicodeString.
  1042. * @stable ICU 2.6
  1043. */
  1044. UnicodeString _id;
  1045. public:
  1046. /**
  1047. * @stable ICU 2.6
  1048. */
  1049. SimpleNumberFormatFactory(const Locale& locale, UBool visible = TRUE);
  1050. /**
  1051. * @stable ICU 3.0
  1052. */
  1053. virtual ~SimpleNumberFormatFactory();
  1054. /**
  1055. * @stable ICU 2.6
  1056. */
  1057. virtual UBool visible(void) const;
  1058. /**
  1059. * @stable ICU 2.6
  1060. */
  1061. virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) const;
  1062. };
  1063. #endif /* #if !UCONFIG_NO_SERVICE */
  1064. // -------------------------------------
  1065. inline UBool
  1066. NumberFormat::isParseIntegerOnly() const
  1067. {
  1068. return fParseIntegerOnly;
  1069. }
  1070. inline UBool
  1071. NumberFormat::isLenient() const
  1072. {
  1073. return fLenient;
  1074. }
  1075. U_NAMESPACE_END
  1076. #endif /* #if !UCONFIG_NO_FORMATTING */
  1077. #endif // _NUMFMT
  1078. //eof