numfmt.h 50 KB

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