numfmt.h 46 KB

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