numfmt.h 50 KB

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