decimfmt.h 88 KB

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