decimfmt.h 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210
  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
  6. * Corporation and others. All Rights Reserved.
  7. ********************************************************************************
  8. *
  9. * File DECIMFMT.H
  10. *
  11. * Modification History:
  12. *
  13. * Date Name Description
  14. * 02/19/97 aliu Converted from java.
  15. * 03/20/97 clhuang Updated per C++ implementation.
  16. * 04/03/97 aliu Rewrote parsing and formatting completely, and
  17. * cleaned up and debugged. Actually works now.
  18. * 04/17/97 aliu Changed DigitCount to int per code review.
  19. * 07/10/97 helena Made ParsePosition a class and get rid of the function
  20. * hiding problems.
  21. * 09/09/97 aliu Ported over support for exponential formats.
  22. * 07/20/98 stephen Changed documentation
  23. * 01/30/13 emmons Added Scaling methods
  24. ********************************************************************************
  25. */
  26. #ifndef DECIMFMT_H
  27. #define DECIMFMT_H
  28. #include "unicode/utypes.h"
  29. #if U_SHOW_CPLUSPLUS_API
  30. /**
  31. * \file
  32. * \brief C++ API: Compatibility APIs for decimal formatting.
  33. */
  34. #if !UCONFIG_NO_FORMATTING
  35. #include "unicode/dcfmtsym.h"
  36. #include "unicode/numfmt.h"
  37. #include "unicode/locid.h"
  38. #include "unicode/fpositer.h"
  39. #include "unicode/stringpiece.h"
  40. #include "unicode/curramt.h"
  41. #include "unicode/enumset.h"
  42. U_NAMESPACE_BEGIN
  43. class CurrencyPluralInfo;
  44. class CompactDecimalFormat;
  45. namespace number {
  46. class LocalizedNumberFormatter;
  47. namespace impl {
  48. class DecimalQuantity;
  49. struct DecimalFormatFields;
  50. class UFormattedNumberData;
  51. }
  52. }
  53. namespace numparse::impl {
  54. class NumberParserImpl;
  55. }
  56. /**
  57. * **IMPORTANT:** New users are strongly encouraged to see if
  58. * numberformatter.h fits their use case. Although not deprecated, this header
  59. * is provided for backwards compatibility only.
  60. *
  61. * DecimalFormat is a concrete subclass of NumberFormat that formats decimal
  62. * numbers. It has a variety of features designed to make it possible to parse
  63. * and format numbers in any locale, including support for Western, Arabic, or
  64. * Indic digits. It also supports different flavors of numbers, including
  65. * integers ("123"), fixed-point numbers ("123.4"), scientific notation
  66. * ("1.23E4"), percentages ("12%"), and currency amounts ("$123", "USD123",
  67. * "123 US dollars"). All of these flavors can be easily localized.
  68. *
  69. * To obtain a NumberFormat for a specific locale (including the default
  70. * locale) call one of NumberFormat's factory methods such as
  71. * createInstance(). Do not call the DecimalFormat constructors directly, unless
  72. * you know what you are doing, since the NumberFormat factory methods may
  73. * return subclasses other than DecimalFormat.
  74. *
  75. * **Example Usage**
  76. *
  77. * \code
  78. * // Normally we would have a GUI with a menu for this
  79. * int32_t locCount;
  80. * const Locale* locales = NumberFormat::getAvailableLocales(locCount);
  81. *
  82. * double myNumber = -1234.56;
  83. * UErrorCode success = U_ZERO_ERROR;
  84. * NumberFormat* form;
  85. *
  86. * // Print out a number with the localized number, currency and percent
  87. * // format for each locale.
  88. * UnicodeString countryName;
  89. * UnicodeString displayName;
  90. * UnicodeString str;
  91. * UnicodeString pattern;
  92. * Formattable fmtable;
  93. * for (int32_t j = 0; j < 3; ++j) {
  94. * cout << endl << "FORMAT " << j << endl;
  95. * for (int32_t i = 0; i < locCount; ++i) {
  96. * if (locales[i].getCountry(countryName).size() == 0) {
  97. * // skip language-only
  98. * continue;
  99. * }
  100. * switch (j) {
  101. * case 0:
  102. * form = NumberFormat::createInstance(locales[i], success ); break;
  103. * case 1:
  104. * form = NumberFormat::createCurrencyInstance(locales[i], success ); break;
  105. * default:
  106. * form = NumberFormat::createPercentInstance(locales[i], success ); break;
  107. * }
  108. * if (form) {
  109. * str.remove();
  110. * pattern = ((DecimalFormat*)form)->toPattern(pattern);
  111. * cout << locales[i].getDisplayName(displayName) << ": " << pattern;
  112. * cout << " -> " << form->format(myNumber,str) << endl;
  113. * form->parse(form->format(myNumber,str), fmtable, success);
  114. * delete form;
  115. * }
  116. * }
  117. * }
  118. * \endcode
  119. *
  120. * **Another example use createInstance(style)**
  121. *
  122. * \code
  123. * // Print out a number using the localized number, currency,
  124. * // percent, scientific, integer, iso currency, and plural currency
  125. * // format for each locale</strong>
  126. * Locale* locale = new Locale("en", "US");
  127. * double myNumber = 1234.56;
  128. * UErrorCode success = U_ZERO_ERROR;
  129. * UnicodeString str;
  130. * Formattable fmtable;
  131. * for (int j=NumberFormat::kNumberStyle;
  132. * j<=NumberFormat::kPluralCurrencyStyle;
  133. * ++j) {
  134. * NumberFormat* form = NumberFormat::createInstance(locale, j, success);
  135. * str.remove();
  136. * cout << "format result " << form->format(myNumber, str) << endl;
  137. * format->parse(form->format(myNumber, str), fmtable, success);
  138. * delete form;
  139. * }
  140. * \endcode
  141. *
  142. *
  143. * <p><strong>Patterns</strong>
  144. *
  145. * <p>A DecimalFormat consists of a <em>pattern</em> and a set of
  146. * <em>symbols</em>. The pattern may be set directly using
  147. * applyPattern(), or indirectly using other API methods which
  148. * manipulate aspects of the pattern, such as the minimum number of integer
  149. * digits. The symbols are stored in a DecimalFormatSymbols
  150. * object. When using the NumberFormat factory methods, the
  151. * pattern and symbols are read from ICU's locale data.
  152. *
  153. * <p><strong>Special Pattern Characters</strong>
  154. *
  155. * <p>Many characters in a pattern are taken literally; they are matched during
  156. * parsing and output unchanged during formatting. Special characters, on the
  157. * other hand, stand for other characters, strings, or classes of characters.
  158. * For example, the '#' character is replaced by a localized digit. Often the
  159. * replacement character is the same as the pattern character; in the U.S. locale,
  160. * the ',' grouping character is replaced by ','. However, the replacement is
  161. * still happening, and if the symbols are modified, the grouping character
  162. * changes. Some special characters affect the behavior of the formatter by
  163. * their presence; for example, if the percent character is seen, then the
  164. * value is multiplied by 100 before being displayed.
  165. *
  166. * <p>To insert a special character in a pattern as a literal, that is, without
  167. * any special meaning, the character must be quoted. There are some exceptions to
  168. * this which are noted below.
  169. *
  170. * <p>The characters listed here are used in non-localized patterns. Localized
  171. * patterns use the corresponding characters taken from this formatter's
  172. * DecimalFormatSymbols object instead, and these characters lose
  173. * their special status. Two exceptions are the currency sign and quote, which
  174. * are not localized.
  175. *
  176. * <table border=0 cellspacing=3 cellpadding=0>
  177. * <tr bgcolor="#ccccff">
  178. * <td align=left><strong>Symbol</strong>
  179. * <td align=left><strong>Location</strong>
  180. * <td align=left><strong>Localized?</strong>
  181. * <td align=left><strong>Meaning</strong>
  182. * <tr valign=top>
  183. * <td><code>0</code>
  184. * <td>Number
  185. * <td>Yes
  186. * <td>Digit
  187. * <tr valign=top bgcolor="#eeeeff">
  188. * <td><code>1-9</code>
  189. * <td>Number
  190. * <td>Yes
  191. * <td>'1' through '9' indicate rounding.
  192. * <tr valign=top>
  193. * <td><code>\htmlonly&#x40;\endhtmlonly</code> <!--doxygen doesn't like @-->
  194. * <td>Number
  195. * <td>No
  196. * <td>Significant digit
  197. * <tr valign=top bgcolor="#eeeeff">
  198. * <td><code>#</code>
  199. * <td>Number
  200. * <td>Yes
  201. * <td>Digit, zero shows as absent
  202. * <tr valign=top>
  203. * <td><code>.</code>
  204. * <td>Number
  205. * <td>Yes
  206. * <td>Decimal separator or monetary decimal separator
  207. * <tr valign=top bgcolor="#eeeeff">
  208. * <td><code>-</code>
  209. * <td>Number
  210. * <td>Yes
  211. * <td>Minus sign
  212. * <tr valign=top>
  213. * <td><code>,</code>
  214. * <td>Number
  215. * <td>Yes
  216. * <td>Grouping separator
  217. * <tr valign=top bgcolor="#eeeeff">
  218. * <td><code>E</code>
  219. * <td>Number
  220. * <td>Yes
  221. * <td>Separates mantissa and exponent in scientific notation.
  222. * <em>Need not be quoted in prefix or suffix.</em>
  223. * <tr valign=top>
  224. * <td><code>+</code>
  225. * <td>Exponent
  226. * <td>Yes
  227. * <td>Prefix positive exponents with localized plus sign.
  228. * <em>Need not be quoted in prefix or suffix.</em>
  229. * <tr valign=top bgcolor="#eeeeff">
  230. * <td><code>;</code>
  231. * <td>Subpattern boundary
  232. * <td>Yes
  233. * <td>Separates positive and negative subpatterns
  234. * <tr valign=top>
  235. * <td><code>\%</code>
  236. * <td>Prefix or suffix
  237. * <td>Yes
  238. * <td>Multiply by 100 and show as percentage
  239. * <tr valign=top bgcolor="#eeeeff">
  240. * <td><code>\\u2030</code>
  241. * <td>Prefix or suffix
  242. * <td>Yes
  243. * <td>Multiply by 1000 and show as per mille
  244. * <tr valign=top>
  245. * <td><code>\htmlonly&curren;\endhtmlonly</code> (<code>\\u00A4</code>)
  246. * <td>Prefix or suffix
  247. * <td>No
  248. * <td>Currency sign, replaced by currency symbol. If
  249. * doubled, replaced by international currency symbol.
  250. * If tripled, replaced by currency plural names, for example,
  251. * "US dollar" or "US dollars" for America.
  252. * If present in a pattern, the monetary decimal separator
  253. * is used instead of the decimal separator.
  254. * <tr valign=top bgcolor="#eeeeff">
  255. * <td><code>'</code>
  256. * <td>Prefix or suffix
  257. * <td>No
  258. * <td>Used to quote special characters in a prefix or suffix,
  259. * for example, <code>"'#'#"</code> formats 123 to
  260. * <code>"#123"</code>. To create a single quote
  261. * itself, use two in a row: <code>"# o''clock"</code>.
  262. * <tr valign=top>
  263. * <td><code>*</code>
  264. * <td>Prefix or suffix boundary
  265. * <td>Yes
  266. * <td>Pad escape, precedes pad character
  267. * </table>
  268. *
  269. * <p>A DecimalFormat pattern contains a positive and negative
  270. * subpattern, for example, "#,##0.00;(#,##0.00)". Each subpattern has a
  271. * prefix, a numeric part, and a suffix. If there is no explicit negative
  272. * subpattern, the negative subpattern is the localized minus sign prefixed to the
  273. * positive subpattern. That is, "0.00" alone is equivalent to "0.00;-0.00". If there
  274. * is an explicit negative subpattern, it serves only to specify the negative
  275. * prefix and suffix; the number of digits, minimal digits, and other
  276. * characteristics are ignored in the negative subpattern. That means that
  277. * "#,##0.0#;(#)" has precisely the same result as "#,##0.0#;(#,##0.0#)".
  278. *
  279. * <p>The prefixes, suffixes, and various symbols used for infinity, digits,
  280. * thousands separators, decimal separators, etc. may be set to arbitrary
  281. * values, and they will appear properly during formatting. However, care must
  282. * be taken that the symbols and strings do not conflict, or parsing will be
  283. * unreliable. For example, either the positive and negative prefixes or the
  284. * suffixes must be distinct for parse() to be able
  285. * to distinguish positive from negative values. Another example is that the
  286. * decimal separator and thousands separator should be distinct characters, or
  287. * parsing will be impossible.
  288. *
  289. * <p>The <em>grouping separator</em> is a character that separates clusters of
  290. * integer digits to make large numbers more legible. It commonly used for
  291. * thousands, but in some locales it separates ten-thousands. The <em>grouping
  292. * size</em> is the number of digits between the grouping separators, such as 3
  293. * for "100,000,000" or 4 for "1 0000 0000". There are actually two different
  294. * grouping sizes: One used for the least significant integer digits, the
  295. * <em>primary grouping size</em>, and one used for all others, the
  296. * <em>secondary grouping size</em>. In most locales these are the same, but
  297. * sometimes they are different. For example, if the primary grouping interval
  298. * is 3, and the secondary is 2, then this corresponds to the pattern
  299. * "#,##,##0", and the number 123456789 is formatted as "12,34,56,789". If a
  300. * pattern contains multiple grouping separators, the interval between the last
  301. * one and the end of the integer defines the primary grouping size, and the
  302. * interval between the last two defines the secondary grouping size. All others
  303. * are ignored, so "#,##,###,####" == "###,###,####" == "##,#,###,####".
  304. *
  305. * <p>Illegal patterns, such as "#.#.#" or "#.###,###", will cause
  306. * DecimalFormat to set a failing UErrorCode.
  307. *
  308. * <p><strong>Pattern BNF</strong>
  309. *
  310. * <pre>
  311. * pattern := subpattern (';' subpattern)?
  312. * subpattern := prefix? number exponent? suffix?
  313. * number := (integer ('.' fraction)?) | sigDigits
  314. * prefix := '\\u0000'..'\\uFFFD' - specialCharacters
  315. * suffix := '\\u0000'..'\\uFFFD' - specialCharacters
  316. * integer := '#'* '0'* '0'
  317. * fraction := '0'* '#'*
  318. * sigDigits := '#'* '@' '@'* '#'*
  319. * exponent := 'E' '+'? '0'* '0'
  320. * padSpec := '*' padChar
  321. * padChar := '\\u0000'..'\\uFFFD' - quote
  322. * &nbsp;
  323. * Notation:
  324. * X* 0 or more instances of X
  325. * X? 0 or 1 instances of X
  326. * X|Y either X or Y
  327. * C..D any character from C up to D, inclusive
  328. * S-T characters in S, except those in T
  329. * </pre>
  330. * The first subpattern is for positive numbers. The second (optional)
  331. * subpattern is for negative numbers.
  332. *
  333. * <p>Not indicated in the BNF syntax above:
  334. *
  335. * <ul><li>The grouping separator ',' can occur inside the integer and
  336. * sigDigits elements, between any two pattern characters of that
  337. * element, as long as the integer or sigDigits element is not
  338. * followed by the exponent element.
  339. *
  340. * <li>Two grouping intervals are recognized: That between the
  341. * decimal point and the first grouping symbol, and that
  342. * between the first and second grouping symbols. These
  343. * intervals are identical in most locales, but in some
  344. * locales they differ. For example, the pattern
  345. * &quot;#,##,###&quot; formats the number 123456789 as
  346. * &quot;12,34,56,789&quot;.</li>
  347. *
  348. * <li>The pad specifier <code>padSpec</code> may appear before the prefix,
  349. * after the prefix, before the suffix, after the suffix, or not at all.
  350. *
  351. * <li>In place of '0', the digits '1' through '9' may be used to
  352. * indicate a rounding increment.
  353. * </ul>
  354. *
  355. * <p><strong>Parsing</strong>
  356. *
  357. * <p>DecimalFormat parses all Unicode characters that represent
  358. * decimal digits, as defined by u_charDigitValue(). In addition,
  359. * DecimalFormat also recognizes as digits the ten consecutive
  360. * characters starting with the localized zero digit defined in the
  361. * DecimalFormatSymbols object. During formatting, the
  362. * DecimalFormatSymbols-based digits are output.
  363. *
  364. * <p>During parsing, grouping separators are ignored if in lenient mode;
  365. * otherwise, if present, they must be in appropriate positions.
  366. *
  367. * <p>For currency parsing, the formatter is able to parse every currency
  368. * style formats no matter which style the formatter is constructed with.
  369. * For example, a formatter instance gotten from
  370. * NumberFormat.getInstance(ULocale, NumberFormat.CURRENCYSTYLE) can parse
  371. * formats such as "USD1.00" and "3.00 US dollars".
  372. *
  373. * <p>If parse(UnicodeString&,Formattable&,ParsePosition&)
  374. * fails to parse a string, it leaves the parse position unchanged.
  375. * The convenience method parse(UnicodeString&,Formattable&,UErrorCode&)
  376. * indicates parse failure by setting a failing
  377. * UErrorCode.
  378. *
  379. * <p><strong>Formatting</strong>
  380. *
  381. * <p>Formatting is guided by several parameters, all of which can be
  382. * specified either using a pattern or using the API. The following
  383. * description applies to formats that do not use <a href="#sci">scientific
  384. * notation</a> or <a href="#sigdig">significant digits</a>.
  385. *
  386. * <ul><li>If the number of actual integer digits exceeds the
  387. * <em>maximum integer digits</em>, then only the least significant
  388. * digits are shown. For example, 1997 is formatted as "97" if the
  389. * maximum integer digits is set to 2.
  390. *
  391. * <li>If the number of actual integer digits is less than the
  392. * <em>minimum integer digits</em>, then leading zeros are added. For
  393. * example, 1997 is formatted as "01997" if the minimum integer digits
  394. * is set to 5.
  395. *
  396. * <li>If the number of actual fraction digits exceeds the <em>maximum
  397. * fraction digits</em>, then rounding is performed to the
  398. * maximum fraction digits. For example, 0.125 is formatted as "0.12"
  399. * if the maximum fraction digits is 2. This behavior can be changed
  400. * by specifying a rounding increment and/or a rounding mode.
  401. *
  402. * <li>If the number of actual fraction digits is less than the
  403. * <em>minimum fraction digits</em>, then trailing zeros are added.
  404. * For example, 0.125 is formatted as "0.1250" if the minimum fraction
  405. * digits is set to 4.
  406. *
  407. * <li>Trailing fractional zeros are not displayed if they occur
  408. * <em>j</em> positions after the decimal, where <em>j</em> is less
  409. * than the maximum fraction digits. For example, 0.10004 is
  410. * formatted as "0.1" if the maximum fraction digits is four or less.
  411. * </ul>
  412. *
  413. * <p><strong>Special Values</strong>
  414. *
  415. * <p><code>NaN</code> is represented as a single character, typically
  416. * <code>\\uFFFD</code>. This character is determined by the
  417. * DecimalFormatSymbols object. This is the only value for which
  418. * the prefixes and suffixes are not used.
  419. *
  420. * <p>Infinity is represented as a single character, typically
  421. * <code>\\u221E</code>, with the positive or negative prefixes and suffixes
  422. * applied. The infinity character is determined by the
  423. * DecimalFormatSymbols object.
  424. *
  425. * <a name="sci"><strong>Scientific Notation</strong></a>
  426. *
  427. * <p>Numbers in scientific notation are expressed as the product of a mantissa
  428. * and a power of ten, for example, 1234 can be expressed as 1.234 x 10<sup>3</sup>. The
  429. * mantissa is typically in the half-open interval [1.0, 10.0) or sometimes [0.0, 1.0),
  430. * but it need not be. DecimalFormat supports arbitrary mantissas.
  431. * DecimalFormat can be instructed to use scientific
  432. * notation through the API or through the pattern. In a pattern, the exponent
  433. * character immediately followed by one or more digit characters indicates
  434. * scientific notation. Example: "0.###E0" formats the number 1234 as
  435. * "1.234E3".
  436. *
  437. * <ul>
  438. * <li>The number of digit characters after the exponent character gives the
  439. * minimum exponent digit count. There is no maximum. Negative exponents are
  440. * formatted using the localized minus sign, <em>not</em> the prefix and suffix
  441. * from the pattern. This allows patterns such as "0.###E0 m/s". To prefix
  442. * positive exponents with a localized plus sign, specify '+' between the
  443. * exponent and the digits: "0.###E+0" will produce formats "1E+1", "1E+0",
  444. * "1E-1", etc. (In localized patterns, use the localized plus sign rather than
  445. * '+'.)
  446. *
  447. * <li>The minimum number of integer digits is achieved by adjusting the
  448. * exponent. Example: 0.00123 formatted with "00.###E0" yields "12.3E-4". This
  449. * only happens if there is no maximum number of integer digits. If there is a
  450. * maximum, then the minimum number of integer digits is fixed at one.
  451. *
  452. * <li>The maximum number of integer digits, if present, specifies the exponent
  453. * grouping. The most common use of this is to generate <em>engineering
  454. * notation</em>, in which the exponent is a multiple of three, e.g.,
  455. * "##0.###E0". The number 12345 is formatted using "##0.####E0" as "12.345E3".
  456. *
  457. * <li>When using scientific notation, the formatter controls the
  458. * digit counts using significant digits logic. The maximum number of
  459. * significant digits limits the total number of integer and fraction
  460. * digits that will be shown in the mantissa; it does not affect
  461. * parsing. For example, 12345 formatted with "##0.##E0" is "12.3E3".
  462. * See the section on significant digits for more details.
  463. *
  464. * <li>The number of significant digits shown is determined as
  465. * follows: If areSignificantDigitsUsed() returns false, then the
  466. * minimum number of significant digits shown is one, and the maximum
  467. * number of significant digits shown is the sum of the <em>minimum
  468. * integer</em> and <em>maximum fraction</em> digits, and is
  469. * unaffected by the maximum integer digits. If this sum is zero,
  470. * then all significant digits are shown. If
  471. * areSignificantDigitsUsed() returns true, then the significant digit
  472. * counts are specified by getMinimumSignificantDigits() and
  473. * getMaximumSignificantDigits(). In this case, the number of
  474. * integer digits is fixed at one, and there is no exponent grouping.
  475. *
  476. * <li>Exponential patterns may not contain grouping separators.
  477. * </ul>
  478. *
  479. * <a name="sigdig"><strong>Significant Digits</strong></a>
  480. *
  481. * <code>DecimalFormat</code> has two ways of controlling how many
  482. * digits are shows: (a) significant digits counts, or (b) integer and
  483. * fraction digit counts. Integer and fraction digit counts are
  484. * described above. When a formatter is using significant digits
  485. * counts, the number of integer and fraction digits is not specified
  486. * directly, and the formatter settings for these counts are ignored.
  487. * Instead, the formatter uses however many integer and fraction
  488. * digits are required to display the specified number of significant
  489. * digits. Examples:
  490. *
  491. * <table border=0 cellspacing=3 cellpadding=0>
  492. * <tr bgcolor="#ccccff">
  493. * <td align=left>Pattern
  494. * <td align=left>Minimum significant digits
  495. * <td align=left>Maximum significant digits
  496. * <td align=left>Number
  497. * <td align=left>Output of format()
  498. * <tr valign=top>
  499. * <td><code>\@\@\@</code>
  500. * <td>3
  501. * <td>3
  502. * <td>12345
  503. * <td><code>12300</code>
  504. * <tr valign=top bgcolor="#eeeeff">
  505. * <td><code>\@\@\@</code>
  506. * <td>3
  507. * <td>3
  508. * <td>0.12345
  509. * <td><code>0.123</code>
  510. * <tr valign=top>
  511. * <td><code>\@\@##</code>
  512. * <td>2
  513. * <td>4
  514. * <td>3.14159
  515. * <td><code>3.142</code>
  516. * <tr valign=top bgcolor="#eeeeff">
  517. * <td><code>\@\@##</code>
  518. * <td>2
  519. * <td>4
  520. * <td>1.23004
  521. * <td><code>1.23</code>
  522. * </table>
  523. *
  524. * <ul>
  525. * <li>Significant digit counts may be expressed using patterns that
  526. * specify a minimum and maximum number of significant digits. These
  527. * are indicated by the <code>'@'</code> and <code>'#'</code>
  528. * characters. The minimum number of significant digits is the number
  529. * of <code>'@'</code> characters. The maximum number of significant
  530. * digits is the number of <code>'@'</code> characters plus the number
  531. * of <code>'#'</code> characters following on the right. For
  532. * example, the pattern <code>"@@@"</code> indicates exactly 3
  533. * significant digits. The pattern <code>"@##"</code> indicates from
  534. * 1 to 3 significant digits. Trailing zero digits to the right of
  535. * the decimal separator are suppressed after the minimum number of
  536. * significant digits have been shown. For example, the pattern
  537. * <code>"@##"</code> formats the number 0.1203 as
  538. * <code>"0.12"</code>.
  539. *
  540. * <li>If a pattern uses significant digits, it may not contain a
  541. * decimal separator, nor the <code>'0'</code> pattern character.
  542. * Patterns such as <code>"@00"</code> or <code>"@.###"</code> are
  543. * disallowed.
  544. *
  545. * <li>Any number of <code>'#'</code> characters may be prepended to
  546. * the left of the leftmost <code>'@'</code> character. These have no
  547. * effect on the minimum and maximum significant digits counts, but
  548. * may be used to position grouping separators. For example,
  549. * <code>"#,#@#"</code> indicates a minimum of one significant digits,
  550. * a maximum of two significant digits, and a grouping size of three.
  551. *
  552. * <li>In order to enable significant digits formatting, use a pattern
  553. * containing the <code>'@'</code> pattern character. Alternatively,
  554. * call setSignificantDigitsUsed(true).
  555. *
  556. * <li>In order to disable significant digits formatting, use a
  557. * pattern that does not contain the <code>'@'</code> pattern
  558. * character. Alternatively, call setSignificantDigitsUsed(false).
  559. *
  560. * <li>The number of significant digits has no effect on parsing.
  561. *
  562. * <li>Significant digits may be used together with exponential notation. Such
  563. * patterns are equivalent to a normal exponential pattern with a minimum and
  564. * maximum integer digit count of one, a minimum fraction digit count of
  565. * <code>getMinimumSignificantDigits() - 1</code>, and a maximum fraction digit
  566. * count of <code>getMaximumSignificantDigits() - 1</code>. For example, the
  567. * pattern <code>"@@###E0"</code> is equivalent to <code>"0.0###E0"</code>.
  568. *
  569. * <li>If significant digits are in use, then the integer and fraction
  570. * digit counts, as set via the API, are ignored. If significant
  571. * digits are not in use, then the significant digit counts, as set via
  572. * the API, are ignored.
  573. *
  574. * </ul>
  575. *
  576. * <p><strong>Padding</strong>
  577. *
  578. * <p>DecimalFormat supports padding the result of
  579. * format() to a specific width. Padding may be specified either
  580. * through the API or through the pattern syntax. In a pattern the pad escape
  581. * character, followed by a single pad character, causes padding to be parsed
  582. * and formatted. The pad escape character is '*' in unlocalized patterns, and
  583. * can be localized using DecimalFormatSymbols::setSymbol() with a
  584. * DecimalFormatSymbols::kPadEscapeSymbol
  585. * selector. For example, <code>"$*x#,##0.00"</code> formats 123 to
  586. * <code>"$xx123.00"</code>, and 1234 to <code>"$1,234.00"</code>.
  587. *
  588. * <ul>
  589. * <li>When padding is in effect, the width of the positive subpattern,
  590. * including prefix and suffix, determines the format width. For example, in
  591. * the pattern <code>"* #0 o''clock"</code>, the format width is 10.
  592. *
  593. * <li>The width is counted in 16-bit code units (char16_ts).
  594. *
  595. * <li>Some parameters which usually do not matter have meaning when padding is
  596. * used, because the pattern width is significant with padding. In the pattern
  597. * "* ##,##,#,##0.##", the format width is 14. The initial characters "##,##,"
  598. * do not affect the grouping size or maximum integer digits, but they do affect
  599. * the format width.
  600. *
  601. * <li>Padding may be inserted at one of four locations: before the prefix,
  602. * after the prefix, before the suffix, or after the suffix. If padding is
  603. * specified in any other location, applyPattern()
  604. * sets a failing UErrorCode. If there is no prefix,
  605. * before the prefix and after the prefix are equivalent, likewise for the
  606. * suffix.
  607. *
  608. * <li>When specified in a pattern, the 32-bit code point immediately
  609. * following the pad escape is the pad character. This may be any character,
  610. * including a special pattern character. That is, the pad escape
  611. * <em>escapes</em> the following character. If there is no character after
  612. * the pad escape, then the pattern is illegal.
  613. *
  614. * </ul>
  615. *
  616. * <p><strong>Rounding</strong>
  617. *
  618. * <p>DecimalFormat supports rounding to a specific increment. For
  619. * example, 1230 rounded to the nearest 50 is 1250. 1.234 rounded to the
  620. * nearest 0.65 is 1.3. The rounding increment may be specified through the API
  621. * or in a pattern. To specify a rounding increment in a pattern, include the
  622. * increment in the pattern itself. "#,#50" specifies a rounding increment of
  623. * 50. "#,##0.05" specifies a rounding increment of 0.05.
  624. *
  625. * <p>In the absence of an explicit rounding increment numbers are
  626. * rounded to their formatted width.
  627. *
  628. * <ul>
  629. * <li>Rounding only affects the string produced by formatting. It does
  630. * not affect parsing or change any numerical values.
  631. *
  632. * <li>A <em>rounding mode</em> determines how values are rounded; see
  633. * DecimalFormat::ERoundingMode. The default rounding mode is
  634. * DecimalFormat::kRoundHalfEven. The rounding mode can only be set
  635. * through the API; it can not be set with a pattern.
  636. *
  637. * <li>Some locales use rounding in their currency formats to reflect the
  638. * smallest currency denomination.
  639. *
  640. * <li>In a pattern, digits '1' through '9' specify rounding, but otherwise
  641. * behave identically to digit '0'.
  642. * </ul>
  643. *
  644. * <p><strong>Synchronization</strong>
  645. *
  646. * <p>DecimalFormat objects are not synchronized. Multiple
  647. * threads should not access one formatter concurrently.
  648. *
  649. * <p><strong>Subclassing</strong>
  650. *
  651. * <p><em>User subclasses are not supported.</em> While clients may write
  652. * subclasses, such code will not necessarily work and will not be
  653. * guaranteed to work stably from release to release.
  654. */
  655. class U_I18N_API DecimalFormat : public NumberFormat {
  656. public:
  657. /**
  658. * Pad position.
  659. * @stable ICU 2.4
  660. */
  661. enum EPadPosition {
  662. kPadBeforePrefix, kPadAfterPrefix, kPadBeforeSuffix, kPadAfterSuffix
  663. };
  664. /**
  665. * Create a DecimalFormat using the default pattern and symbols
  666. * for the default locale. This is a convenient way to obtain a
  667. * DecimalFormat when internationalization is not the main concern.
  668. * <P>
  669. * To obtain standard formats for a given locale, use the factory methods
  670. * on NumberFormat such as createInstance. These factories will
  671. * return the most appropriate sub-class of NumberFormat for a given
  672. * locale.
  673. * <p>
  674. * <strong>NOTE:</strong> New users are strongly encouraged to use
  675. * #icu::number::NumberFormatter instead of DecimalFormat.
  676. * @param status Output param set to success/failure code. If the
  677. * pattern is invalid this will be set to a failure code.
  678. * @stable ICU 2.0
  679. */
  680. DecimalFormat(UErrorCode& status);
  681. /**
  682. * Create a DecimalFormat from the given pattern and the symbols
  683. * for the default locale. This is a convenient way to obtain a
  684. * DecimalFormat when internationalization is not the main concern.
  685. * <P>
  686. * To obtain standard formats for a given locale, use the factory methods
  687. * on NumberFormat such as createInstance. These factories will
  688. * return the most appropriate sub-class of NumberFormat for a given
  689. * locale.
  690. * <p>
  691. * <strong>NOTE:</strong> New users are strongly encouraged to use
  692. * #icu::number::NumberFormatter instead of DecimalFormat.
  693. * @param pattern A non-localized pattern string.
  694. * @param status Output param set to success/failure code. If the
  695. * pattern is invalid this will be set to a failure code.
  696. * @stable ICU 2.0
  697. */
  698. DecimalFormat(const UnicodeString& pattern, UErrorCode& status);
  699. /**
  700. * Create a DecimalFormat from the given pattern and symbols.
  701. * Use this constructor when you need to completely customize the
  702. * behavior of the format.
  703. * <P>
  704. * To obtain standard formats for a given
  705. * locale, use the factory methods on NumberFormat such as
  706. * createInstance or createCurrencyInstance. If you need only minor adjustments
  707. * to a standard format, you can modify the format returned by
  708. * a NumberFormat factory method.
  709. * <p>
  710. * <strong>NOTE:</strong> New users are strongly encouraged to use
  711. * #icu::number::NumberFormatter instead of DecimalFormat.
  712. *
  713. * @param pattern a non-localized pattern string
  714. * @param symbolsToAdopt the set of symbols to be used. The caller should not
  715. * delete this object after making this call.
  716. * @param status Output param set to success/failure code. If the
  717. * pattern is invalid this will be set to a failure code.
  718. * @stable ICU 2.0
  719. */
  720. DecimalFormat(const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt, UErrorCode& status);
  721. #ifndef U_HIDE_INTERNAL_API
  722. /**
  723. * This API is for ICU use only.
  724. * Create a DecimalFormat from the given pattern, symbols, and style.
  725. *
  726. * @param pattern a non-localized pattern string
  727. * @param symbolsToAdopt the set of symbols to be used. The caller should not
  728. * delete this object after making this call.
  729. * @param style style of decimal format
  730. * @param status Output param set to success/failure code. If the
  731. * pattern is invalid this will be set to a failure code.
  732. * @internal
  733. */
  734. DecimalFormat(const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt,
  735. UNumberFormatStyle style, UErrorCode& status);
  736. #if UCONFIG_HAVE_PARSEALLINPUT
  737. /**
  738. * @internal
  739. */
  740. void setParseAllInput(UNumberFormatAttributeValue value);
  741. #endif
  742. #endif /* U_HIDE_INTERNAL_API */
  743. private:
  744. /**
  745. * Internal constructor for DecimalFormat; sets up internal fields. All public constructors should
  746. * call this constructor.
  747. */
  748. DecimalFormat(const DecimalFormatSymbols* symbolsToAdopt, UErrorCode& status);
  749. public:
  750. /**
  751. * Set an integer attribute on this DecimalFormat.
  752. * May return U_UNSUPPORTED_ERROR if this instance does not support
  753. * the specified attribute.
  754. * @param attr the attribute to set
  755. * @param newValue new value
  756. * @param status the error type
  757. * @return *this - for chaining (example: format.setAttribute(...).setAttribute(...) )
  758. * @stable ICU 51
  759. */
  760. virtual DecimalFormat& setAttribute(UNumberFormatAttribute attr, int32_t newValue, UErrorCode& status);
  761. /**
  762. * Get an integer
  763. * May return U_UNSUPPORTED_ERROR if this instance does not support
  764. * the specified attribute.
  765. * @param attr the attribute to set
  766. * @param status the error type
  767. * @return the attribute value. Undefined if there is an error.
  768. * @stable ICU 51
  769. */
  770. virtual int32_t getAttribute(UNumberFormatAttribute attr, UErrorCode& status) const;
  771. /**
  772. * Set whether or not grouping will be used in this format.
  773. * @param newValue True, grouping will be used in this format.
  774. * @see getGroupingUsed
  775. * @stable ICU 53
  776. */
  777. void setGroupingUsed(UBool newValue) override;
  778. /**
  779. * Sets whether or not numbers should be parsed as integers only.
  780. * @param value set True, this format will parse numbers as integers
  781. * only.
  782. * @see isParseIntegerOnly
  783. * @stable ICU 53
  784. */
  785. void setParseIntegerOnly(UBool value) override;
  786. /**
  787. * Sets whether lenient parsing should be enabled (it is off by default).
  788. *
  789. * @param enable \c true if lenient parsing should be used,
  790. * \c false otherwise.
  791. * @stable ICU 4.8
  792. */
  793. void setLenient(UBool enable) override;
  794. /**
  795. * Create a DecimalFormat from the given pattern and symbols.
  796. * Use this constructor when you need to completely customize the
  797. * behavior of the format.
  798. * <P>
  799. * To obtain standard formats for a given
  800. * locale, use the factory methods on NumberFormat such as
  801. * createInstance or createCurrencyInstance. If you need only minor adjustments
  802. * to a standard format, you can modify the format returned by
  803. * a NumberFormat factory method.
  804. * <p>
  805. * <strong>NOTE:</strong> New users are strongly encouraged to use
  806. * #icu::number::NumberFormatter instead of DecimalFormat.
  807. *
  808. * @param pattern a non-localized pattern string
  809. * @param symbolsToAdopt the set of symbols to be used. The caller should not
  810. * delete this object after making this call.
  811. * @param parseError Output param to receive errors occurred during parsing
  812. * @param status Output param set to success/failure code. If the
  813. * pattern is invalid this will be set to a failure code.
  814. * @stable ICU 2.0
  815. */
  816. DecimalFormat(const UnicodeString& pattern, DecimalFormatSymbols* symbolsToAdopt,
  817. UParseError& parseError, UErrorCode& status);
  818. /**
  819. * Create a DecimalFormat from the given pattern and symbols.
  820. * Use this constructor when you need to completely customize the
  821. * behavior of the format.
  822. * <P>
  823. * To obtain standard formats for a given
  824. * locale, use the factory methods on NumberFormat such as
  825. * createInstance or createCurrencyInstance. If you need only minor adjustments
  826. * to a standard format, you can modify the format returned by
  827. * a NumberFormat factory method.
  828. * <p>
  829. * <strong>NOTE:</strong> New users are strongly encouraged to use
  830. * #icu::number::NumberFormatter instead of DecimalFormat.
  831. *
  832. * @param pattern a non-localized pattern string
  833. * @param symbols the set of symbols to be used
  834. * @param status Output param set to success/failure code. If the
  835. * pattern is invalid this will be set to a failure code.
  836. * @stable ICU 2.0
  837. */
  838. DecimalFormat(const UnicodeString& pattern, const DecimalFormatSymbols& symbols, UErrorCode& status);
  839. /**
  840. * Copy constructor.
  841. *
  842. * @param source the DecimalFormat object to be copied from.
  843. * @stable ICU 2.0
  844. */
  845. DecimalFormat(const DecimalFormat& source);
  846. /**
  847. * Assignment operator.
  848. *
  849. * @param rhs the DecimalFormat object to be copied.
  850. * @stable ICU 2.0
  851. */
  852. DecimalFormat& operator=(const DecimalFormat& rhs);
  853. /**
  854. * Destructor.
  855. * @stable ICU 2.0
  856. */
  857. ~DecimalFormat() override;
  858. /**
  859. * Clone this Format object polymorphically. The caller owns the
  860. * result and should delete it when done.
  861. *
  862. * @return a polymorphic copy of this DecimalFormat.
  863. * @stable ICU 2.0
  864. */
  865. DecimalFormat* clone() const override;
  866. /**
  867. * Return true if the given Format objects are semantically equal.
  868. * Objects of different subclasses are considered unequal.
  869. *
  870. * @param other the object to be compared with.
  871. * @return true if the given Format objects are semantically equal.
  872. * @stable ICU 2.0
  873. */
  874. bool operator==(const Format& other) const override;
  875. using NumberFormat::format;
  876. /**
  877. * Format a double or long number using base-10 representation.
  878. *
  879. * @param number The value to be formatted.
  880. * @param appendTo Output parameter to receive result.
  881. * Result is appended to existing contents.
  882. * @param pos On input: an alignment field, if desired.
  883. * On output: the offsets of the alignment field.
  884. * @return Reference to 'appendTo' parameter.
  885. * @stable ICU 2.0
  886. */
  887. UnicodeString& format(double number, UnicodeString& appendTo, FieldPosition& pos) const override;
  888. #ifndef U_HIDE_INTERNAL_API
  889. /**
  890. * Format a double or long number using base-10 representation.
  891. *
  892. * @param number The value to be formatted.
  893. * @param appendTo Output parameter to receive result.
  894. * Result is appended to existing contents.
  895. * @param pos On input: an alignment field, if desired.
  896. * On output: the offsets of the alignment field.
  897. * @param status
  898. * @return Reference to 'appendTo' parameter.
  899. * @internal
  900. */
  901. UnicodeString& format(double number, UnicodeString& appendTo, FieldPosition& pos,
  902. UErrorCode& status) const override;
  903. #endif /* U_HIDE_INTERNAL_API */
  904. /**
  905. * Format a double or long number using base-10 representation.
  906. *
  907. * @param number The value to be formatted.
  908. * @param appendTo Output parameter to receive result.
  909. * Result is appended to existing contents.
  910. * @param posIter On return, can be used to iterate over positions
  911. * of fields generated by this format call.
  912. * Can be nullptr.
  913. * @param status Output param filled with success/failure status.
  914. * @return Reference to 'appendTo' parameter.
  915. * @stable ICU 4.4
  916. */
  917. UnicodeString& format(double number, UnicodeString& appendTo, FieldPositionIterator* posIter,
  918. UErrorCode& status) const override;
  919. /**
  920. * Format a long number using base-10 representation.
  921. *
  922. * @param number The value to be formatted.
  923. * @param appendTo Output parameter to receive result.
  924. * Result is appended to existing contents.
  925. * @param pos On input: an alignment field, if desired.
  926. * On output: the offsets of the alignment field.
  927. * @return Reference to 'appendTo' parameter.
  928. * @stable ICU 2.0
  929. */
  930. UnicodeString& format(int32_t number, UnicodeString& appendTo, FieldPosition& pos) const override;
  931. #ifndef U_HIDE_INTERNAL_API
  932. /**
  933. * Format a long number using base-10 representation.
  934. *
  935. * @param number The value to be formatted.
  936. * @param appendTo Output parameter to receive result.
  937. * Result is appended to existing contents.
  938. * @param pos On input: an alignment field, if desired.
  939. * On output: the offsets of the alignment field.
  940. * @param status Output param filled with success/failure status.
  941. * @return Reference to 'appendTo' parameter.
  942. * @internal
  943. */
  944. UnicodeString& format(int32_t number, UnicodeString& appendTo, FieldPosition& pos,
  945. UErrorCode& status) const override;
  946. #endif /* U_HIDE_INTERNAL_API */
  947. /**
  948. * Format a long number using base-10 representation.
  949. *
  950. * @param number The value to be formatted.
  951. * @param appendTo Output parameter to receive result.
  952. * Result is appended to existing contents.
  953. * @param posIter On return, can be used to iterate over positions
  954. * of fields generated by this format call.
  955. * Can be nullptr.
  956. * @param status Output param filled with success/failure status.
  957. * @return Reference to 'appendTo' parameter.
  958. * @stable ICU 4.4
  959. */
  960. UnicodeString& format(int32_t number, UnicodeString& appendTo, FieldPositionIterator* posIter,
  961. UErrorCode& status) const override;
  962. /**
  963. * Format an int64 number using base-10 representation.
  964. *
  965. * @param number The value to be formatted.
  966. * @param appendTo Output parameter to receive result.
  967. * Result is appended to existing contents.
  968. * @param pos On input: an alignment field, if desired.
  969. * On output: the offsets of the alignment field.
  970. * @return Reference to 'appendTo' parameter.
  971. * @stable ICU 2.8
  972. */
  973. UnicodeString& format(int64_t number, UnicodeString& appendTo, FieldPosition& pos) const override;
  974. #ifndef U_HIDE_INTERNAL_API
  975. /**
  976. * Format an int64 number using base-10 representation.
  977. *
  978. * @param number The value to be formatted.
  979. * @param appendTo Output parameter to receive result.
  980. * Result is appended to existing contents.
  981. * @param pos On input: an alignment field, if desired.
  982. * On output: the offsets of the alignment field.
  983. * @param status Output param filled with success/failure status.
  984. * @return Reference to 'appendTo' parameter.
  985. * @internal
  986. */
  987. UnicodeString& format(int64_t number, UnicodeString& appendTo, FieldPosition& pos,
  988. UErrorCode& status) const override;
  989. #endif /* U_HIDE_INTERNAL_API */
  990. /**
  991. * Format an int64 number using base-10 representation.
  992. *
  993. * @param number The value to be formatted.
  994. * @param appendTo Output parameter to receive result.
  995. * Result is appended to existing contents.
  996. * @param posIter On return, can be used to iterate over positions
  997. * of fields generated by this format call.
  998. * Can be nullptr.
  999. * @param status Output param filled with success/failure status.
  1000. * @return Reference to 'appendTo' parameter.
  1001. * @stable ICU 4.4
  1002. */
  1003. UnicodeString& format(int64_t number, UnicodeString& appendTo, FieldPositionIterator* posIter,
  1004. UErrorCode& status) const override;
  1005. /**
  1006. * Format a decimal number.
  1007. * The syntax of the unformatted number is a "numeric string"
  1008. * as defined in the Decimal Arithmetic Specification, available at
  1009. * http://speleotrove.com/decimal
  1010. *
  1011. * @param number The unformatted number, as a string.
  1012. * @param appendTo Output parameter to receive result.
  1013. * Result is appended to existing contents.
  1014. * @param posIter On return, can be used to iterate over positions
  1015. * of fields generated by this format call.
  1016. * Can be nullptr.
  1017. * @param status Output param filled with success/failure status.
  1018. * @return Reference to 'appendTo' parameter.
  1019. * @stable ICU 4.4
  1020. */
  1021. UnicodeString& format(StringPiece number, UnicodeString& appendTo, FieldPositionIterator* posIter,
  1022. UErrorCode& status) const override;
  1023. #ifndef U_HIDE_INTERNAL_API
  1024. /**
  1025. * Format a decimal number.
  1026. * The number is a DecimalQuantity wrapper onto a floating point decimal number.
  1027. * The default implementation in NumberFormat converts the decimal number
  1028. * to a double and formats that.
  1029. *
  1030. * @param number The number, a DecimalQuantity format Decimal Floating Point.
  1031. * @param appendTo Output parameter to receive result.
  1032. * Result is appended to existing contents.
  1033. * @param posIter On return, can be used to iterate over positions
  1034. * of fields generated by this format call.
  1035. * @param status Output param filled with success/failure status.
  1036. * @return Reference to 'appendTo' parameter.
  1037. * @internal
  1038. */
  1039. UnicodeString& format(const number::impl::DecimalQuantity& number, UnicodeString& appendTo,
  1040. FieldPositionIterator* posIter, UErrorCode& status) const override;
  1041. /**
  1042. * Format a decimal number.
  1043. * The number is a DecimalQuantity wrapper onto a floating point decimal number.
  1044. * The default implementation in NumberFormat converts the decimal number
  1045. * to a double and formats that.
  1046. *
  1047. * @param number The number, a DecimalQuantity format Decimal Floating Point.
  1048. * @param appendTo Output parameter to receive result.
  1049. * Result is appended to existing contents.
  1050. * @param pos On input: an alignment field, if desired.
  1051. * On output: the offsets of the alignment field.
  1052. * @param status Output param filled with success/failure status.
  1053. * @return Reference to 'appendTo' parameter.
  1054. * @internal
  1055. */
  1056. UnicodeString& format(const number::impl::DecimalQuantity& number, UnicodeString& appendTo,
  1057. FieldPosition& pos, UErrorCode& status) const override;
  1058. #endif // U_HIDE_INTERNAL_API
  1059. using NumberFormat::parse;
  1060. /**
  1061. * Parse the given string using this object's choices. The method
  1062. * does string comparisons to try to find an optimal match.
  1063. * If no object can be parsed, index is unchanged, and nullptr is
  1064. * returned. The result is returned as the most parsimonious
  1065. * type of Formattable that will accommodate all of the
  1066. * necessary precision. For example, if the result is exactly 12,
  1067. * it will be returned as a long. However, if it is 1.5, it will
  1068. * be returned as a double.
  1069. *
  1070. * @param text The text to be parsed.
  1071. * @param result Formattable to be set to the parse result.
  1072. * If parse fails, return contents are undefined.
  1073. * @param parsePosition The position to start parsing at on input.
  1074. * On output, moved to after the last successfully
  1075. * parse character. On parse failure, does not change.
  1076. * @see Formattable
  1077. * @stable ICU 2.0
  1078. */
  1079. void parse(const UnicodeString& text, Formattable& result,
  1080. ParsePosition& parsePosition) const override;
  1081. /**
  1082. * Parses text from the given string as a currency amount. Unlike
  1083. * the parse() method, this method will attempt to parse a generic
  1084. * currency name, searching for a match of this object's locale's
  1085. * currency display names, or for a 3-letter ISO currency code.
  1086. * This method will fail if this format is not a currency format,
  1087. * that is, if it does not contain the currency pattern symbol
  1088. * (U+00A4) in its prefix or suffix.
  1089. *
  1090. * @param text the string to parse
  1091. * @param pos input-output position; on input, the position within text
  1092. * to match; must have 0 <= pos.getIndex() < text.length();
  1093. * on output, the position after the last matched character.
  1094. * If the parse fails, the position in unchanged upon output.
  1095. * @return if parse succeeds, a pointer to a newly-created CurrencyAmount
  1096. * object (owned by the caller) containing information about
  1097. * the parsed currency; if parse fails, this is nullptr.
  1098. * @stable ICU 49
  1099. */
  1100. CurrencyAmount* parseCurrency(const UnicodeString& text, ParsePosition& pos) const override;
  1101. /**
  1102. * Returns the decimal format symbols, which is generally not changed
  1103. * by the programmer or user.
  1104. * @return desired DecimalFormatSymbols
  1105. * @see DecimalFormatSymbols
  1106. * @stable ICU 2.0
  1107. */
  1108. virtual const DecimalFormatSymbols* getDecimalFormatSymbols() const;
  1109. /**
  1110. * Sets the decimal format symbols, which is generally not changed
  1111. * by the programmer or user.
  1112. * @param symbolsToAdopt DecimalFormatSymbols to be adopted.
  1113. * @stable ICU 2.0
  1114. */
  1115. virtual void adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt);
  1116. /**
  1117. * Sets the decimal format symbols, which is generally not changed
  1118. * by the programmer or user.
  1119. * @param symbols DecimalFormatSymbols.
  1120. * @stable ICU 2.0
  1121. */
  1122. virtual void setDecimalFormatSymbols(const DecimalFormatSymbols& symbols);
  1123. /**
  1124. * Returns the currency plural format information,
  1125. * which is generally not changed by the programmer or user.
  1126. * @return desired CurrencyPluralInfo
  1127. * @stable ICU 4.2
  1128. */
  1129. virtual const CurrencyPluralInfo* getCurrencyPluralInfo() const;
  1130. /**
  1131. * Sets the currency plural format information,
  1132. * which is generally not changed by the programmer or user.
  1133. * @param toAdopt CurrencyPluralInfo to be adopted.
  1134. * @stable ICU 4.2
  1135. */
  1136. virtual void adoptCurrencyPluralInfo(CurrencyPluralInfo* toAdopt);
  1137. /**
  1138. * Sets the currency plural format information,
  1139. * which is generally not changed by the programmer or user.
  1140. * @param info Currency Plural Info.
  1141. * @stable ICU 4.2
  1142. */
  1143. virtual void setCurrencyPluralInfo(const CurrencyPluralInfo& info);
  1144. /**
  1145. * Get the positive prefix.
  1146. *
  1147. * @param result Output param which will receive the positive prefix.
  1148. * @return A reference to 'result'.
  1149. * Examples: +123, $123, sFr123
  1150. * @stable ICU 2.0
  1151. */
  1152. UnicodeString& getPositivePrefix(UnicodeString& result) const;
  1153. /**
  1154. * Set the positive prefix.
  1155. *
  1156. * @param newValue the new value of the the positive prefix to be set.
  1157. * Examples: +123, $123, sFr123
  1158. * @stable ICU 2.0
  1159. */
  1160. virtual void setPositivePrefix(const UnicodeString& newValue);
  1161. /**
  1162. * Get the negative prefix.
  1163. *
  1164. * @param result Output param which will receive the negative prefix.
  1165. * @return A reference to 'result'.
  1166. * Examples: -123, ($123) (with negative suffix), sFr-123
  1167. * @stable ICU 2.0
  1168. */
  1169. UnicodeString& getNegativePrefix(UnicodeString& result) const;
  1170. /**
  1171. * Set the negative prefix.
  1172. *
  1173. * @param newValue the new value of the the negative prefix to be set.
  1174. * Examples: -123, ($123) (with negative suffix), sFr-123
  1175. * @stable ICU 2.0
  1176. */
  1177. virtual void setNegativePrefix(const UnicodeString& newValue);
  1178. /**
  1179. * Get the positive suffix.
  1180. *
  1181. * @param result Output param which will receive the positive suffix.
  1182. * @return A reference to 'result'.
  1183. * Example: 123%
  1184. * @stable ICU 2.0
  1185. */
  1186. UnicodeString& getPositiveSuffix(UnicodeString& result) const;
  1187. /**
  1188. * Set the positive suffix.
  1189. *
  1190. * @param newValue the new value of the positive suffix to be set.
  1191. * Example: 123%
  1192. * @stable ICU 2.0
  1193. */
  1194. virtual void setPositiveSuffix(const UnicodeString& newValue);
  1195. /**
  1196. * Get the negative suffix.
  1197. *
  1198. * @param result Output param which will receive the negative suffix.
  1199. * @return A reference to 'result'.
  1200. * Examples: -123%, ($123) (with positive suffixes)
  1201. * @stable ICU 2.0
  1202. */
  1203. UnicodeString& getNegativeSuffix(UnicodeString& result) const;
  1204. /**
  1205. * Set the negative suffix.
  1206. *
  1207. * @param newValue the new value of the negative suffix to be set.
  1208. * Examples: 123%
  1209. * @stable ICU 2.0
  1210. */
  1211. virtual void setNegativeSuffix(const UnicodeString& newValue);
  1212. /**
  1213. * Whether to show the plus sign on positive (non-negative) numbers; for example, "+12"
  1214. *
  1215. * For more control over sign display, use NumberFormatter.
  1216. *
  1217. * @return Whether the sign is shown on positive numbers and zero.
  1218. * @stable ICU 64
  1219. */
  1220. UBool isSignAlwaysShown() const;
  1221. /**
  1222. * Set whether to show the plus sign on positive (non-negative) numbers; for example, "+12".
  1223. *
  1224. * For more control over sign display, use NumberFormatter.
  1225. *
  1226. * @param value true to always show a sign; false to hide the sign on positive numbers and zero.
  1227. * @stable ICU 64
  1228. */
  1229. void setSignAlwaysShown(UBool value);
  1230. /**
  1231. * Get the multiplier for use in percent, permill, etc.
  1232. * For a percentage, set the suffixes to have "%" and the multiplier to be 100.
  1233. * (For Arabic, use arabic percent symbol).
  1234. * For a permill, set the suffixes to have "\\u2031" and the multiplier to be 1000.
  1235. *
  1236. * The number may also be multiplied by a power of ten; see getMultiplierScale().
  1237. *
  1238. * @return the multiplier for use in percent, permill, etc.
  1239. * Examples: with 100, 1.23 -> "123", and "123" -> 1.23
  1240. * @stable ICU 2.0
  1241. */
  1242. int32_t getMultiplier() const;
  1243. /**
  1244. * Set the multiplier for use in percent, permill, etc.
  1245. * For a percentage, set the suffixes to have "%" and the multiplier to be 100.
  1246. * (For Arabic, use arabic percent symbol).
  1247. * For a permill, set the suffixes to have "\\u2031" and the multiplier to be 1000.
  1248. *
  1249. * This method only supports integer multipliers. To multiply by a non-integer, pair this
  1250. * method with setMultiplierScale().
  1251. *
  1252. * @param newValue the new value of the multiplier for use in percent, permill, etc.
  1253. * Examples: with 100, 1.23 -> "123", and "123" -> 1.23
  1254. * @stable ICU 2.0
  1255. */
  1256. virtual void setMultiplier(int32_t newValue);
  1257. /**
  1258. * Gets the power of ten by which number should be multiplied before formatting, which
  1259. * can be combined with setMultiplier() to multiply by any arbitrary decimal value.
  1260. *
  1261. * A multiplier scale of 2 corresponds to multiplication by 100, and a multiplier scale
  1262. * of -2 corresponds to multiplication by 0.01.
  1263. *
  1264. * This method is analogous to UNUM_SCALE in getAttribute.
  1265. *
  1266. * @return the current value of the power-of-ten multiplier.
  1267. * @stable ICU 62
  1268. */
  1269. int32_t getMultiplierScale() const;
  1270. /**
  1271. * Sets a power of ten by which number should be multiplied before formatting, which
  1272. * can be combined with setMultiplier() to multiply by any arbitrary decimal value.
  1273. *
  1274. * A multiplier scale of 2 corresponds to multiplication by 100, and a multiplier scale
  1275. * of -2 corresponds to multiplication by 0.01.
  1276. *
  1277. * For example, to multiply numbers by 0.5 before formatting, you can do:
  1278. *
  1279. * <pre>
  1280. * df.setMultiplier(5);
  1281. * df.setMultiplierScale(-1);
  1282. * </pre>
  1283. *
  1284. * This method is analogous to UNUM_SCALE in setAttribute.
  1285. *
  1286. * @param newValue the new value of the power-of-ten multiplier.
  1287. * @stable ICU 62
  1288. */
  1289. void setMultiplierScale(int32_t newValue);
  1290. /**
  1291. * Get the rounding increment.
  1292. * @return A positive rounding increment, or 0.0 if a custom rounding
  1293. * increment is not in effect.
  1294. * @see #setRoundingIncrement
  1295. * @see #getRoundingMode
  1296. * @see #setRoundingMode
  1297. * @stable ICU 2.0
  1298. */
  1299. virtual double getRoundingIncrement() const;
  1300. /**
  1301. * Set the rounding increment. In the absence of a rounding increment,
  1302. * numbers will be rounded to the number of digits displayed.
  1303. * @param newValue A positive rounding increment, or 0.0 to
  1304. * use the default rounding increment.
  1305. * Negative increments are equivalent to 0.0.
  1306. * @see #getRoundingIncrement
  1307. * @see #getRoundingMode
  1308. * @see #setRoundingMode
  1309. * @stable ICU 2.0
  1310. */
  1311. virtual void setRoundingIncrement(double newValue);
  1312. /**
  1313. * Get the rounding mode.
  1314. * @return A rounding mode
  1315. * @see #setRoundingIncrement
  1316. * @see #getRoundingIncrement
  1317. * @see #setRoundingMode
  1318. * @stable ICU 2.0
  1319. */
  1320. virtual ERoundingMode getRoundingMode() const override;
  1321. /**
  1322. * Set the rounding mode.
  1323. * @param roundingMode A rounding mode
  1324. * @see #setRoundingIncrement
  1325. * @see #getRoundingIncrement
  1326. * @see #getRoundingMode
  1327. * @stable ICU 2.0
  1328. */
  1329. virtual void setRoundingMode(ERoundingMode roundingMode) override;
  1330. /**
  1331. * Get the width to which the output of format() is padded.
  1332. * The width is counted in 16-bit code units.
  1333. * @return the format width, or zero if no padding is in effect
  1334. * @see #setFormatWidth
  1335. * @see #getPadCharacterString
  1336. * @see #setPadCharacter
  1337. * @see #getPadPosition
  1338. * @see #setPadPosition
  1339. * @stable ICU 2.0
  1340. */
  1341. virtual int32_t getFormatWidth() const;
  1342. /**
  1343. * Set the width to which the output of format() is padded.
  1344. * The width is counted in 16-bit code units.
  1345. * This method also controls whether padding is enabled.
  1346. * @param width the width to which to pad the result of
  1347. * format(), or zero to disable padding. A negative
  1348. * width is equivalent to 0.
  1349. * @see #getFormatWidth
  1350. * @see #getPadCharacterString
  1351. * @see #setPadCharacter
  1352. * @see #getPadPosition
  1353. * @see #setPadPosition
  1354. * @stable ICU 2.0
  1355. */
  1356. virtual void setFormatWidth(int32_t width);
  1357. /**
  1358. * Get the pad character used to pad to the format width. The
  1359. * default is ' '.
  1360. * @return a string containing the pad character. This will always
  1361. * have a length of one 32-bit code point.
  1362. * @see #setFormatWidth
  1363. * @see #getFormatWidth
  1364. * @see #setPadCharacter
  1365. * @see #getPadPosition
  1366. * @see #setPadPosition
  1367. * @stable ICU 2.0
  1368. */
  1369. virtual UnicodeString getPadCharacterString() const;
  1370. /**
  1371. * Set the character used to pad to the format width. If padding
  1372. * is not enabled, then this will take effect if padding is later
  1373. * enabled.
  1374. * @param padChar a string containing the pad character. If the string
  1375. * has length 0, then the pad character is set to ' '. Otherwise
  1376. * padChar.char32At(0) will be used as the pad character.
  1377. * @see #setFormatWidth
  1378. * @see #getFormatWidth
  1379. * @see #getPadCharacterString
  1380. * @see #getPadPosition
  1381. * @see #setPadPosition
  1382. * @stable ICU 2.0
  1383. */
  1384. virtual void setPadCharacter(const UnicodeString& padChar);
  1385. /**
  1386. * Get the position at which padding will take place. This is the location
  1387. * at which padding will be inserted if the result of format()
  1388. * is shorter than the format width.
  1389. * @return the pad position, one of kPadBeforePrefix,
  1390. * kPadAfterPrefix, kPadBeforeSuffix, or
  1391. * kPadAfterSuffix.
  1392. * @see #setFormatWidth
  1393. * @see #getFormatWidth
  1394. * @see #setPadCharacter
  1395. * @see #getPadCharacterString
  1396. * @see #setPadPosition
  1397. * @see #EPadPosition
  1398. * @stable ICU 2.0
  1399. */
  1400. virtual EPadPosition getPadPosition() const;
  1401. /**
  1402. * Set the position at which padding will take place. This is the location
  1403. * at which padding will be inserted if the result of format()
  1404. * is shorter than the format width. This has no effect unless padding is
  1405. * enabled.
  1406. * @param padPos the pad position, one of kPadBeforePrefix,
  1407. * kPadAfterPrefix, kPadBeforeSuffix, or
  1408. * kPadAfterSuffix.
  1409. * @see #setFormatWidth
  1410. * @see #getFormatWidth
  1411. * @see #setPadCharacter
  1412. * @see #getPadCharacterString
  1413. * @see #getPadPosition
  1414. * @see #EPadPosition
  1415. * @stable ICU 2.0
  1416. */
  1417. virtual void setPadPosition(EPadPosition padPos);
  1418. /**
  1419. * Return whether or not scientific notation is used.
  1420. * @return true if this object formats and parses scientific notation
  1421. * @see #setScientificNotation
  1422. * @see #getMinimumExponentDigits
  1423. * @see #setMinimumExponentDigits
  1424. * @see #isExponentSignAlwaysShown
  1425. * @see #setExponentSignAlwaysShown
  1426. * @stable ICU 2.0
  1427. */
  1428. virtual UBool isScientificNotation() const;
  1429. /**
  1430. * Set whether or not scientific notation is used. When scientific notation
  1431. * is used, the effective maximum number of integer digits is <= 8. If the
  1432. * maximum number of integer digits is set to more than 8, the effective
  1433. * maximum will be 1. This allows this call to generate a 'default' scientific
  1434. * number format without additional changes.
  1435. * @param useScientific true if this object formats and parses scientific
  1436. * notation
  1437. * @see #isScientificNotation
  1438. * @see #getMinimumExponentDigits
  1439. * @see #setMinimumExponentDigits
  1440. * @see #isExponentSignAlwaysShown
  1441. * @see #setExponentSignAlwaysShown
  1442. * @stable ICU 2.0
  1443. */
  1444. virtual void setScientificNotation(UBool useScientific);
  1445. /**
  1446. * Return the minimum exponent digits that will be shown.
  1447. * @return the minimum exponent digits that will be shown
  1448. * @see #setScientificNotation
  1449. * @see #isScientificNotation
  1450. * @see #setMinimumExponentDigits
  1451. * @see #isExponentSignAlwaysShown
  1452. * @see #setExponentSignAlwaysShown
  1453. * @stable ICU 2.0
  1454. */
  1455. virtual int8_t getMinimumExponentDigits() const;
  1456. /**
  1457. * Set the minimum exponent digits that will be shown. This has no
  1458. * effect unless scientific notation is in use.
  1459. * @param minExpDig a value >= 1 indicating the fewest exponent digits
  1460. * that will be shown. Values less than 1 will be treated as 1.
  1461. * @see #setScientificNotation
  1462. * @see #isScientificNotation
  1463. * @see #getMinimumExponentDigits
  1464. * @see #isExponentSignAlwaysShown
  1465. * @see #setExponentSignAlwaysShown
  1466. * @stable ICU 2.0
  1467. */
  1468. virtual void setMinimumExponentDigits(int8_t minExpDig);
  1469. /**
  1470. * Return whether the exponent sign is always shown.
  1471. * @return true if the exponent is always prefixed with either the
  1472. * localized minus sign or the localized plus sign, false if only negative
  1473. * exponents are prefixed with the localized minus sign.
  1474. * @see #setScientificNotation
  1475. * @see #isScientificNotation
  1476. * @see #setMinimumExponentDigits
  1477. * @see #getMinimumExponentDigits
  1478. * @see #setExponentSignAlwaysShown
  1479. * @stable ICU 2.0
  1480. */
  1481. virtual UBool isExponentSignAlwaysShown() const;
  1482. /**
  1483. * Set whether the exponent sign is always shown. This has no effect
  1484. * unless scientific notation is in use.
  1485. * @param expSignAlways true if the exponent is always prefixed with either
  1486. * the localized minus sign or the localized plus sign, false if only
  1487. * negative exponents are prefixed with the localized minus sign.
  1488. * @see #setScientificNotation
  1489. * @see #isScientificNotation
  1490. * @see #setMinimumExponentDigits
  1491. * @see #getMinimumExponentDigits
  1492. * @see #isExponentSignAlwaysShown
  1493. * @stable ICU 2.0
  1494. */
  1495. virtual void setExponentSignAlwaysShown(UBool expSignAlways);
  1496. /**
  1497. * Return the grouping size. Grouping size is the number of digits between
  1498. * grouping separators in the integer portion of a number. For example,
  1499. * in the number "123,456.78", the grouping size is 3.
  1500. *
  1501. * @return the grouping size.
  1502. * @see setGroupingSize
  1503. * @see NumberFormat::isGroupingUsed
  1504. * @see DecimalFormatSymbols::getGroupingSeparator
  1505. * @stable ICU 2.0
  1506. */
  1507. int32_t getGroupingSize() const;
  1508. /**
  1509. * Set the grouping size. Grouping size is the number of digits between
  1510. * grouping separators in the integer portion of a number. For example,
  1511. * in the number "123,456.78", the grouping size is 3.
  1512. *
  1513. * @param newValue the new value of the grouping size.
  1514. * @see getGroupingSize
  1515. * @see NumberFormat::setGroupingUsed
  1516. * @see DecimalFormatSymbols::setGroupingSeparator
  1517. * @stable ICU 2.0
  1518. */
  1519. virtual void setGroupingSize(int32_t newValue);
  1520. /**
  1521. * Return the secondary grouping size. In some locales one
  1522. * grouping interval is used for the least significant integer
  1523. * digits (the primary grouping size), and another is used for all
  1524. * others (the secondary grouping size). A formatter supporting a
  1525. * secondary grouping size will return a positive integer unequal
  1526. * to the primary grouping size returned by
  1527. * getGroupingSize(). For example, if the primary
  1528. * grouping size is 4, and the secondary grouping size is 2, then
  1529. * the number 123456789 formats as "1,23,45,6789", and the pattern
  1530. * appears as "#,##,###0".
  1531. * @return the secondary grouping size, or a value less than
  1532. * one if there is none
  1533. * @see setSecondaryGroupingSize
  1534. * @see NumberFormat::isGroupingUsed
  1535. * @see DecimalFormatSymbols::getGroupingSeparator
  1536. * @stable ICU 2.4
  1537. */
  1538. int32_t getSecondaryGroupingSize() const;
  1539. /**
  1540. * Set the secondary grouping size. If set to a value less than 1,
  1541. * then secondary grouping is turned off, and the primary grouping
  1542. * size is used for all intervals, not just the least significant.
  1543. *
  1544. * @param newValue the new value of the secondary grouping size.
  1545. * @see getSecondaryGroupingSize
  1546. * @see NumberFormat#setGroupingUsed
  1547. * @see DecimalFormatSymbols::setGroupingSeparator
  1548. * @stable ICU 2.4
  1549. */
  1550. virtual void setSecondaryGroupingSize(int32_t newValue);
  1551. /**
  1552. * Returns the minimum number of grouping digits.
  1553. * Grouping separators are output if there are at least this many
  1554. * digits to the left of the first (rightmost) grouping separator,
  1555. * that is, there are at least (minimum grouping + grouping size) integer digits.
  1556. * (Subject to isGroupingUsed().)
  1557. *
  1558. * For example, if this value is 2, and the grouping size is 3, then
  1559. * 9999 -> "9999" and 10000 -> "10,000"
  1560. *
  1561. * The default value for this attribute is 0.
  1562. * A value of 1, 0, or lower, means that the use of grouping separators
  1563. * only depends on the grouping size (and on isGroupingUsed()).
  1564. *
  1565. * NOTE: The CLDR data is used in NumberFormatter but not in DecimalFormat.
  1566. * This is for backwards compatibility reasons.
  1567. *
  1568. * For more control over grouping strategies, use NumberFormatter.
  1569. *
  1570. * @see setMinimumGroupingDigits
  1571. * @see getGroupingSize
  1572. * @stable ICU 64
  1573. */
  1574. int32_t getMinimumGroupingDigits() const;
  1575. /**
  1576. * Sets the minimum grouping digits. Setting the value to
  1577. * - 1: Turns off minimum grouping digits.
  1578. * - 0 or -1: The behavior is undefined.
  1579. * - UNUM_MINIMUM_GROUPING_DIGITS_AUTO: Display grouping using the default
  1580. * strategy for all locales.
  1581. * - UNUM_MINIMUM_GROUPING_DIGITS_MIN2: Display grouping using locale
  1582. * defaults, except do not show grouping on values smaller than 10000
  1583. * (such that there is a minimum of two digits before the first
  1584. * separator).
  1585. *
  1586. * For more control over grouping strategies, use NumberFormatter.
  1587. *
  1588. * @param newValue the new value of minimum grouping digits.
  1589. * @see getMinimumGroupingDigits
  1590. * @stable ICU 64
  1591. */
  1592. void setMinimumGroupingDigits(int32_t newValue);
  1593. /**
  1594. * Allows you to get the behavior of the decimal separator with integers.
  1595. * (The decimal separator will always appear with decimals.)
  1596. *
  1597. * @return true if the decimal separator always appear with decimals.
  1598. * Example: Decimal ON: 12345 -> 12345.; OFF: 12345 -> 12345
  1599. * @stable ICU 2.0
  1600. */
  1601. UBool isDecimalSeparatorAlwaysShown() const;
  1602. /**
  1603. * Allows you to set the behavior of the decimal separator with integers.
  1604. * (The decimal separator will always appear with decimals.)
  1605. *
  1606. * @param newValue set true if the decimal separator will always appear with decimals.
  1607. * Example: Decimal ON: 12345 -> 12345.; OFF: 12345 -> 12345
  1608. * @stable ICU 2.0
  1609. */
  1610. virtual void setDecimalSeparatorAlwaysShown(UBool newValue);
  1611. /**
  1612. * Allows you to get the parse behavior of the pattern decimal mark.
  1613. *
  1614. * @return true if input must contain a match to decimal mark in pattern
  1615. * @stable ICU 54
  1616. */
  1617. UBool isDecimalPatternMatchRequired() const;
  1618. /**
  1619. * Allows you to set the parse behavior of the pattern decimal mark.
  1620. *
  1621. * if true, the input must have a decimal mark if one was specified in the pattern. When
  1622. * false the decimal mark may be omitted from the input.
  1623. *
  1624. * @param newValue set true if input must contain a match to decimal mark in pattern
  1625. * @stable ICU 54
  1626. */
  1627. virtual void setDecimalPatternMatchRequired(UBool newValue);
  1628. /**
  1629. * Returns whether to ignore exponents when parsing.
  1630. *
  1631. * @return Whether to ignore exponents when parsing.
  1632. * @see #setParseNoExponent
  1633. * @stable ICU 64
  1634. */
  1635. UBool isParseNoExponent() const;
  1636. /**
  1637. * Specifies whether to stop parsing when an exponent separator is encountered. For
  1638. * example, parses "123E4" to 123 (with parse position 3) instead of 1230000 (with parse position
  1639. * 5).
  1640. *
  1641. * @param value true to prevent exponents from being parsed; false to allow them to be parsed.
  1642. * @stable ICU 64
  1643. */
  1644. void setParseNoExponent(UBool value);
  1645. /**
  1646. * Returns whether parsing is sensitive to case (lowercase/uppercase).
  1647. *
  1648. * @return Whether parsing is case-sensitive.
  1649. * @see #setParseCaseSensitive
  1650. * @stable ICU 64
  1651. */
  1652. UBool isParseCaseSensitive() const;
  1653. /**
  1654. * Whether to pay attention to case when parsing; default is to ignore case (perform
  1655. * case-folding). For example, "A" == "a" in case-insensitive but not case-sensitive mode.
  1656. *
  1657. * Currency symbols are never case-folded. For example, "us$1.00" will not parse in case-insensitive
  1658. * mode, even though "US$1.00" parses.
  1659. *
  1660. * @param value true to enable case-sensitive parsing (the default); false to force
  1661. * case-sensitive parsing behavior.
  1662. * @stable ICU 64
  1663. */
  1664. void setParseCaseSensitive(UBool value);
  1665. /**
  1666. * Returns whether truncation of high-order integer digits should result in an error.
  1667. * By default, setMaximumIntegerDigits truncates high-order digits silently.
  1668. *
  1669. * @return Whether an error code is set if high-order digits are truncated.
  1670. * @see setFormatFailIfMoreThanMaxDigits
  1671. * @stable ICU 64
  1672. */
  1673. UBool isFormatFailIfMoreThanMaxDigits() const;
  1674. /**
  1675. * Sets whether truncation of high-order integer digits should result in an error.
  1676. * By default, setMaximumIntegerDigits truncates high-order digits silently.
  1677. *
  1678. * @param value Whether to set an error code if high-order digits are truncated.
  1679. * @stable ICU 64
  1680. */
  1681. void setFormatFailIfMoreThanMaxDigits(UBool value);
  1682. /**
  1683. * Synthesizes a pattern string that represents the current state
  1684. * of this Format object.
  1685. *
  1686. * @param result Output param which will receive the pattern.
  1687. * Previous contents are deleted.
  1688. * @return A reference to 'result'.
  1689. * @see applyPattern
  1690. * @stable ICU 2.0
  1691. */
  1692. virtual UnicodeString& toPattern(UnicodeString& result) const;
  1693. /**
  1694. * Synthesizes a localized pattern string that represents the current
  1695. * state of this Format object.
  1696. *
  1697. * @param result Output param which will receive the localized pattern.
  1698. * Previous contents are deleted.
  1699. * @return A reference to 'result'.
  1700. * @see applyPattern
  1701. * @stable ICU 2.0
  1702. */
  1703. virtual UnicodeString& toLocalizedPattern(UnicodeString& result) const;
  1704. /**
  1705. * Apply the given pattern to this Format object. A pattern is a
  1706. * short-hand specification for the various formatting properties.
  1707. * These properties can also be changed individually through the
  1708. * various setter methods.
  1709. * <P>
  1710. * There is no limit to integer digits are set
  1711. * by this routine, since that is the typical end-user desire;
  1712. * use setMaximumInteger if you want to set a real value.
  1713. * For negative numbers, use a second pattern, separated by a semicolon
  1714. * <pre>
  1715. * . Example "#,#00.0#" -> 1,234.56
  1716. * </pre>
  1717. * This means a minimum of 2 integer digits, 1 fraction digit, and
  1718. * a maximum of 2 fraction digits.
  1719. * <pre>
  1720. * . Example: "#,#00.0#;(#,#00.0#)" for negatives in parentheses.
  1721. * </pre>
  1722. * In negative patterns, the minimum and maximum counts are ignored;
  1723. * these are presumed to be set in the positive pattern.
  1724. *
  1725. * @param pattern The pattern to be applied.
  1726. * @param parseError Struct to receive information on position
  1727. * of error if an error is encountered
  1728. * @param status Output param set to success/failure code on
  1729. * exit. If the pattern is invalid, this will be
  1730. * set to a failure result.
  1731. * @stable ICU 2.0
  1732. */
  1733. virtual void applyPattern(const UnicodeString& pattern, UParseError& parseError, UErrorCode& status);
  1734. /**
  1735. * Sets the pattern.
  1736. * @param pattern The pattern to be applied.
  1737. * @param status Output param set to success/failure code on
  1738. * exit. If the pattern is invalid, this will be
  1739. * set to a failure result.
  1740. * @stable ICU 2.0
  1741. */
  1742. virtual void applyPattern(const UnicodeString& pattern, UErrorCode& status);
  1743. /**
  1744. * Apply the given pattern to this Format object. The pattern
  1745. * is assumed to be in a localized notation. A pattern is a
  1746. * short-hand specification for the various formatting properties.
  1747. * These properties can also be changed individually through the
  1748. * various setter methods.
  1749. * <P>
  1750. * There is no limit to integer digits are set
  1751. * by this routine, since that is the typical end-user desire;
  1752. * use setMaximumInteger if you want to set a real value.
  1753. * For negative numbers, use a second pattern, separated by a semicolon
  1754. * <pre>
  1755. * . Example "#,#00.0#" -> 1,234.56
  1756. * </pre>
  1757. * This means a minimum of 2 integer digits, 1 fraction digit, and
  1758. * a maximum of 2 fraction digits.
  1759. *
  1760. * Example: "#,#00.0#;(#,#00.0#)" for negatives in parentheses.
  1761. *
  1762. * In negative patterns, the minimum and maximum counts are ignored;
  1763. * these are presumed to be set in the positive pattern.
  1764. *
  1765. * @param pattern The localized pattern to be applied.
  1766. * @param parseError Struct to receive information on position
  1767. * of error if an error is encountered
  1768. * @param status Output param set to success/failure code on
  1769. * exit. If the pattern is invalid, this will be
  1770. * set to a failure result.
  1771. * @stable ICU 2.0
  1772. */
  1773. virtual void applyLocalizedPattern(const UnicodeString& pattern, UParseError& parseError,
  1774. UErrorCode& status);
  1775. /**
  1776. * Apply the given pattern to this Format object.
  1777. *
  1778. * @param pattern The localized pattern to be applied.
  1779. * @param status Output param set to success/failure code on
  1780. * exit. If the pattern is invalid, this will be
  1781. * set to a failure result.
  1782. * @stable ICU 2.0
  1783. */
  1784. virtual void applyLocalizedPattern(const UnicodeString& pattern, UErrorCode& status);
  1785. /**
  1786. * Sets the maximum number of digits allowed in the integer portion of a
  1787. * number. This override limits the integer digit count to 309.
  1788. *
  1789. * @param newValue the new value of the maximum number of digits
  1790. * allowed in the integer portion of a number.
  1791. * @see NumberFormat#setMaximumIntegerDigits
  1792. * @stable ICU 2.0
  1793. */
  1794. void setMaximumIntegerDigits(int32_t newValue) override;
  1795. /**
  1796. * Sets the minimum number of digits allowed in the integer portion of a
  1797. * number. This override limits the integer digit count to 309.
  1798. *
  1799. * @param newValue the new value of the minimum number of digits
  1800. * allowed in the integer portion of a number.
  1801. * @see NumberFormat#setMinimumIntegerDigits
  1802. * @stable ICU 2.0
  1803. */
  1804. void setMinimumIntegerDigits(int32_t newValue) override;
  1805. /**
  1806. * Sets the maximum number of digits allowed in the fraction portion of a
  1807. * number. This override limits the fraction digit count to 340.
  1808. *
  1809. * @param newValue the new value of the maximum number of digits
  1810. * allowed in the fraction portion of a number.
  1811. * @see NumberFormat#setMaximumFractionDigits
  1812. * @stable ICU 2.0
  1813. */
  1814. void setMaximumFractionDigits(int32_t newValue) override;
  1815. /**
  1816. * Sets the minimum number of digits allowed in the fraction portion of a
  1817. * number. This override limits the fraction digit count to 340.
  1818. *
  1819. * @param newValue the new value of the minimum number of digits
  1820. * allowed in the fraction portion of a number.
  1821. * @see NumberFormat#setMinimumFractionDigits
  1822. * @stable ICU 2.0
  1823. */
  1824. void setMinimumFractionDigits(int32_t newValue) override;
  1825. /**
  1826. * Returns the minimum number of significant digits that will be
  1827. * displayed. This value has no effect unless areSignificantDigitsUsed()
  1828. * returns true.
  1829. * @return the fewest significant digits that will be shown
  1830. * @stable ICU 3.0
  1831. */
  1832. int32_t getMinimumSignificantDigits() const;
  1833. /**
  1834. * Returns the maximum number of significant digits that will be
  1835. * displayed. This value has no effect unless areSignificantDigitsUsed()
  1836. * returns true.
  1837. * @return the most significant digits that will be shown
  1838. * @stable ICU 3.0
  1839. */
  1840. int32_t getMaximumSignificantDigits() const;
  1841. /**
  1842. * Sets the minimum number of significant digits that will be
  1843. * displayed. If <code>min</code> is less than one then it is set
  1844. * to one. If the maximum significant digits count is less than
  1845. * <code>min</code>, then it is set to <code>min</code>.
  1846. * This function also enables the use of significant digits
  1847. * by this formatter - areSignificantDigitsUsed() will return true.
  1848. * @see #areSignificantDigitsUsed
  1849. * @param min the fewest significant digits to be shown
  1850. * @stable ICU 3.0
  1851. */
  1852. void setMinimumSignificantDigits(int32_t min);
  1853. /**
  1854. * Sets the maximum number of significant digits that will be
  1855. * displayed. If <code>max</code> is less than one then it is set
  1856. * to one. If the minimum significant digits count is greater
  1857. * than <code>max</code>, then it is set to <code>max</code>.
  1858. * This function also enables the use of significant digits
  1859. * by this formatter - areSignificantDigitsUsed() will return true.
  1860. * @see #areSignificantDigitsUsed
  1861. * @param max the most significant digits to be shown
  1862. * @stable ICU 3.0
  1863. */
  1864. void setMaximumSignificantDigits(int32_t max);
  1865. /**
  1866. * Returns true if significant digits are in use, or false if
  1867. * integer and fraction digit counts are in use.
  1868. * @return true if significant digits are in use
  1869. * @stable ICU 3.0
  1870. */
  1871. UBool areSignificantDigitsUsed() const;
  1872. /**
  1873. * Sets whether significant digits are in use, or integer and
  1874. * fraction digit counts are in use.
  1875. * @param useSignificantDigits true to use significant digits, or
  1876. * false to use integer and fraction digit counts
  1877. * @stable ICU 3.0
  1878. */
  1879. void setSignificantDigitsUsed(UBool useSignificantDigits);
  1880. /**
  1881. * Sets the currency used to display currency
  1882. * amounts. This takes effect immediately, if this format is a
  1883. * currency format. If this format is not a currency format, then
  1884. * the currency is used if and when this object becomes a
  1885. * currency format through the application of a new pattern.
  1886. * @param theCurrency a 3-letter ISO code indicating new currency
  1887. * to use. It need not be null-terminated. May be the empty
  1888. * string or nullptr to indicate no currency.
  1889. * @param ec input-output error code
  1890. * @stable ICU 3.0
  1891. */
  1892. void setCurrency(const char16_t* theCurrency, UErrorCode& ec) override;
  1893. #ifndef U_FORCE_HIDE_DEPRECATED_API
  1894. /**
  1895. * Sets the currency used to display currency amounts. See
  1896. * setCurrency(const char16_t*, UErrorCode&).
  1897. * @deprecated ICU 3.0. Use setCurrency(const char16_t*, UErrorCode&).
  1898. */
  1899. virtual void setCurrency(const char16_t* theCurrency);
  1900. #endif // U_FORCE_HIDE_DEPRECATED_API
  1901. /**
  1902. * Sets the `Currency Usage` object used to display currency.
  1903. * This takes effect immediately, if this format is a
  1904. * currency format.
  1905. * @param newUsage new currency usage object to use.
  1906. * @param ec input-output error code
  1907. * @stable ICU 54
  1908. */
  1909. void setCurrencyUsage(UCurrencyUsage newUsage, UErrorCode* ec);
  1910. /**
  1911. * Returns the `Currency Usage` object used to display currency
  1912. * @stable ICU 54
  1913. */
  1914. UCurrencyUsage getCurrencyUsage() const;
  1915. #ifndef U_HIDE_INTERNAL_API
  1916. /**
  1917. * Format a number and save it into the given DecimalQuantity.
  1918. * Internal, not intended for public use.
  1919. * @internal
  1920. */
  1921. void formatToDecimalQuantity(double number, number::impl::DecimalQuantity& output,
  1922. UErrorCode& status) const;
  1923. /**
  1924. * Get a DecimalQuantity corresponding to a formattable as it would be
  1925. * formatted by this DecimalFormat.
  1926. * Internal, not intended for public use.
  1927. * @internal
  1928. */
  1929. void formatToDecimalQuantity(const Formattable& number, number::impl::DecimalQuantity& output,
  1930. UErrorCode& status) const;
  1931. #endif /* U_HIDE_INTERNAL_API */
  1932. /**
  1933. * Converts this DecimalFormat to a (Localized)NumberFormatter. Starting
  1934. * in ICU 60, NumberFormatter is the recommended way to format numbers.
  1935. * You can use the returned LocalizedNumberFormatter to format numbers and
  1936. * get a FormattedNumber, which contains a string as well as additional
  1937. * annotations about the formatted value.
  1938. *
  1939. * If a memory allocation failure occurs, the return value of this method
  1940. * might be null. If you are concerned about correct recovery from
  1941. * out-of-memory situations, use this pattern:
  1942. *
  1943. * <pre>
  1944. * FormattedNumber result;
  1945. * if (auto* ptr = df->toNumberFormatter(status)) {
  1946. * result = ptr->formatDouble(123, status);
  1947. * }
  1948. * </pre>
  1949. *
  1950. * If you are not concerned about out-of-memory situations, or if your
  1951. * environment throws exceptions when memory allocation failure occurs,
  1952. * you can chain the methods, like this:
  1953. *
  1954. * <pre>
  1955. * FormattedNumber result = df
  1956. * ->toNumberFormatter(status)
  1957. * ->formatDouble(123, status);
  1958. * </pre>
  1959. *
  1960. * NOTE: The returned LocalizedNumberFormatter is owned by this DecimalFormat.
  1961. * If a non-const method is called on the DecimalFormat, or if the DecimalFormat
  1962. * is deleted, the object becomes invalid. If you plan to keep the return value
  1963. * beyond the lifetime of the DecimalFormat, copy it to a local variable:
  1964. *
  1965. * <pre>
  1966. * LocalizedNumberFormatter lnf;
  1967. * if (auto* ptr = df->toNumberFormatter(status)) {
  1968. * lnf = *ptr;
  1969. * }
  1970. * </pre>
  1971. *
  1972. * @param status Set on failure, like U_MEMORY_ALLOCATION_ERROR.
  1973. * @return A pointer to an internal object, or nullptr on failure.
  1974. * Do not delete the return value!
  1975. * @stable ICU 64
  1976. */
  1977. const number::LocalizedNumberFormatter* toNumberFormatter(UErrorCode& status) const;
  1978. /**
  1979. * Return the class ID for this class. This is useful only for
  1980. * comparing to a return value from getDynamicClassID(). For example:
  1981. * <pre>
  1982. * . Base* polymorphic_pointer = createPolymorphicObject();
  1983. * . if (polymorphic_pointer->getDynamicClassID() ==
  1984. * . Derived::getStaticClassID()) ...
  1985. * </pre>
  1986. * @return The class ID for all objects of this class.
  1987. * @stable ICU 2.0
  1988. */
  1989. static UClassID U_EXPORT2 getStaticClassID();
  1990. /**
  1991. * Returns a unique class ID POLYMORPHICALLY. Pure virtual override.
  1992. * This method is to implement a simple version of RTTI, since not all
  1993. * C++ compilers support genuine RTTI. Polymorphic operator==() and
  1994. * clone() methods call this method.
  1995. *
  1996. * @return The class ID for this object. All objects of a
  1997. * given class have the same class ID. Objects of
  1998. * other classes have different class IDs.
  1999. * @stable ICU 2.0
  2000. */
  2001. UClassID getDynamicClassID() const override;
  2002. private:
  2003. /** Rebuilds the formatter object from the property bag. */
  2004. void touch(UErrorCode& status);
  2005. /** Rebuilds the formatter object, ignoring any error code. */
  2006. void touchNoError();
  2007. /**
  2008. * Updates the property bag with settings from the given pattern.
  2009. *
  2010. * @param pattern The pattern string to parse.
  2011. * @param ignoreRounding Whether to leave out rounding information (minFrac, maxFrac, and rounding
  2012. * increment) when parsing the pattern. This may be desirable if a custom rounding mode, such
  2013. * as CurrencyUsage, is to be used instead. One of {@link
  2014. * PatternStringParser#IGNORE_ROUNDING_ALWAYS}, {@link PatternStringParser#IGNORE_ROUNDING_IF_CURRENCY},
  2015. * or {@link PatternStringParser#IGNORE_ROUNDING_NEVER}.
  2016. * @see PatternAndPropertyUtils#parseToExistingProperties
  2017. */
  2018. void setPropertiesFromPattern(const UnicodeString& pattern, int32_t ignoreRounding,
  2019. UErrorCode& status);
  2020. const numparse::impl::NumberParserImpl* getParser(UErrorCode& status) const;
  2021. const numparse::impl::NumberParserImpl* getCurrencyParser(UErrorCode& status) const;
  2022. static void fieldPositionHelper(
  2023. const number::impl::UFormattedNumberData& formatted,
  2024. FieldPosition& fieldPosition,
  2025. int32_t offset,
  2026. UErrorCode& status);
  2027. static void fieldPositionIteratorHelper(
  2028. const number::impl::UFormattedNumberData& formatted,
  2029. FieldPositionIterator* fpi,
  2030. int32_t offset,
  2031. UErrorCode& status);
  2032. void setupFastFormat();
  2033. bool fastFormatDouble(double input, UnicodeString& output) const;
  2034. bool fastFormatInt64(int64_t input, UnicodeString& output) const;
  2035. void doFastFormatInt32(int32_t input, bool isNegative, UnicodeString& output) const;
  2036. //=====================================================================================//
  2037. // INSTANCE FIELDS //
  2038. //=====================================================================================//
  2039. // One instance field for the implementation, keep all fields inside of an implementation
  2040. // class defined in number_mapper.h
  2041. number::impl::DecimalFormatFields* fields = nullptr;
  2042. // Allow child class CompactDecimalFormat to access fProperties:
  2043. friend class CompactDecimalFormat;
  2044. // Allow MeasureFormat to use fieldPositionHelper:
  2045. friend class MeasureFormat;
  2046. };
  2047. U_NAMESPACE_END
  2048. #endif /* #if !UCONFIG_NO_FORMATTING */
  2049. #endif /* U_SHOW_CPLUSPLUS_API */
  2050. #endif // _DECIMFMT
  2051. //eof