API.html 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  6. <meta name="Author" content="M Mclaughlin">
  7. <title>bignumber.js API</title>
  8. <style>
  9. html{font-family:sans-serif;font-size:100%}
  10. body{font-family:"Helvetica Neue",Helvetica,Arial,sans-serif;font-size:14px;
  11. line-height:1.65em;background:#fefff5;color:#000;min-height:100%;margin:0}
  12. .nav{background:#fff;position:fixed;top:0;bottom:0;left:0;width:180px;
  13. overflow-y:auto;padding:15px 0 30px 20px}
  14. div.container{width:600px;margin:50px 0 50px 240px}
  15. p{margin:0 0 1em;width:600px}
  16. pre,ul{margin:1em 0}
  17. h1,h2,h3,h4,h5{margin:0;padding:1.5em 0 0}
  18. h1,h2{padding:.75em 0}
  19. h1{font-size:2.5em;color:#fa6900}
  20. h2{font-size:2.25em;color:#fa6900}
  21. h3{font-size:1.75em;color:#69d2e7}
  22. h4{font-size:1.75em;color:#fa6900;padding-bottom:.75em}
  23. h5{font-size:1.2em;padding-bottom:.3em}
  24. h6{font-size:1.1em;margin:0;padding:0.5em 0}
  25. dd dt{font-size:1.2em}
  26. dt{padding-top:.5em}
  27. dd{padding-top:.35em}
  28. b{font-weight:700}
  29. a,a:visited{color:#444;text-decoration:none}
  30. a:active,a:hover{outline:0;color:#000}
  31. .nav a:hover{text-decoration:underline}
  32. .nav a,.nav b,.nav a:visited{display:block;color:#fa6900;font-weight:700;
  33. margin-top:15px}
  34. .nav b{color:#69d2e7;margin-top:20px;cursor:default;width:auto}
  35. ul{list-style-type:none;padding:0 0 0 20px}
  36. .nav ul{line-height:14px;padding-left:0;margin:5px 0 0}
  37. .nav ul a,.nav ul a:visited{display:inline;color:#000;font-family:Verdana,
  38. Geneva,sans-serif;font-size:11px;font-weight:400;margin:0}
  39. .inset,ul.inset{margin-left:20px}
  40. code.inset{font-size:.9em}
  41. .nav li{cursor:pointer;width:auto;margin:0 0 3px}
  42. span.alias{font-style:italic;margin-left:20px}
  43. table{border-collapse:collapse;border-spacing:0;border:2px solid #a7dbd8;
  44. margin:1.75em 0;padding:0}
  45. td,th{text-align:left;margin:0;padding:2px 5px;border:1px dotted #a7dbd8}
  46. th{border-top:2px solid #a7dbd8;border-bottom:2px solid #a7dbd8;color:#f38630}
  47. pre{background:#fff;white-space:pre-wrap;word-wrap:break-word;
  48. border-left:5px solid #a7dbd8;padding:1px 0 1px 15px;margin:1.2em 0}
  49. code,pre{font-family:Monaco,Consolas,"Lucida Console",monospace;
  50. font-weight:400}
  51. .end{margin-bottom:25px}
  52. .nav-title{color:#fa6900}
  53. .centre{text-align:center}
  54. .error-table{font-size:13px;width:100%}
  55. </style>
  56. </head>
  57. <body>
  58. <div class="nav">
  59. <a class='nav-title' href="#">bignumber.js</a>
  60. <b> CONSTRUCTOR </b>
  61. <ul>
  62. <li><a href="#bignumber">BigNumber</a></li>
  63. </ul>
  64. <a href="#methods">Methods</a>
  65. <ul>
  66. <li><a href="#config">config</a></li>
  67. <li>
  68. <ul class="inset">
  69. <li><a href="#decimal-places">DECIMAL_PLACES</a></li>
  70. <li><a href="#rounding-mode" >ROUNDING_MODE</a></li>
  71. <li><a href="#exponential-at">EXPONENTIAL_AT</a></li>
  72. <li><a href="#range" >RANGE</a></li>
  73. <li><a href="#errors" >ERRORS</a></li>
  74. <li><a href="#format" >FORMAT</a></li>
  75. </ul>
  76. </li>
  77. </ul>
  78. <a href="#constructor-properties">Properties</a>
  79. <ul>
  80. <li><a href="#round-up" >ROUND_UP</a></li>
  81. <li><a href="#round-down" >ROUND_DOWN</a></li>
  82. <li><a href="#round-ceil" >ROUND_CEIL</a></li>
  83. <li><a href="#round-floor" >ROUND_FLOOR</a></li>
  84. <li><a href="#round-half-up" >ROUND_HALF_UP</a></li>
  85. <li><a href="#round-half-down" >ROUND_HALF_DOWN</a></li>
  86. <li><a href="#round-half-even" >ROUND_HALF_EVEN</a></li>
  87. <li><a href="#round-half-ceil" >ROUND_HALF_CEIL</a></li>
  88. <li><a href="#round-half-floor">ROUND_HALF_FLOOR</a></li>
  89. </ul>
  90. <b> INSTANCE </b>
  91. <a href="#prototype-methods">Methods</a>
  92. <ul>
  93. <li><a href="#abs" >absoluteValue</a></li>
  94. <li><a href="#ceil" >ceil</a></li>
  95. <li><a href="#cmp" >comparedTo</a></li>
  96. <li><a href="#dp" >decimalPlaces</a></li>
  97. <li><a href="#div" >dividedBy</a></li>
  98. <li><a href="#divInt" >dividedToIntegerBy</a></li>
  99. <li><a href="#eq" >equals</a></li>
  100. <li><a href="#floor" >floor</a></li>
  101. <li><a href="#gt" >greaterThan</a></li>
  102. <li><a href="#gte" >greaterThanOrEqualTo</a></li>
  103. <li><a href="#isF" >isFinite</a></li>
  104. <li><a href="#isInt" >isInteger</a></li>
  105. <li><a href="#isNaN" >isNaN</a></li>
  106. <li><a href="#isNeg" >isNegative</a></li>
  107. <li><a href="#isZ" >isZero</a></li>
  108. <li><a href="#lt" >lessThan</a></li>
  109. <li><a href="#lte" >lessThanOrEqualTo</a></li>
  110. <li><a href="#minus" >minus</a></li>
  111. <li><a href="#mod" >modulo</a></li>
  112. <li><a href="#neg" >negated</a></li>
  113. <li><a href="#plus" >plus</a></li>
  114. <li><a href="#round" >round</a></li>
  115. <li><a href="#sqrt" >squareRoot</a></li>
  116. <li><a href="#times" >times</a></li>
  117. <li><a href="#toE" >toExponential</a></li>
  118. <li><a href="#toF" >toFixed</a></li>
  119. <li><a href="#toFo" >toFormat</a></li>
  120. <li><a href="#toFr" >toFraction</a></li>
  121. <li><a href="#toJSON" >toJSON</a></li>
  122. <li><a href="#toN" >toNumber</a></li>
  123. <li><a href="#pow" >toPower</a></li>
  124. <li><a href="#toP" >toPrecision</a></li>
  125. <li><a href="#toS" >toString</a></li>
  126. <li><a href="#valueOf">valueOf</a></li>
  127. </ul>
  128. <a href="#instance-properties">Properties</a>
  129. <ul>
  130. <li><a href="#coefficient">c : coefficient</a></li>
  131. <li><a href="#exponent" >e : exponent</a></li>
  132. <li><a href="#sign" >s : sign</a></li>
  133. </ul>
  134. <a href="#zero-nan-infinity">Zero, NaN &amp; Infinity</a>
  135. <a href="#Errors">Errors</a>
  136. <a class='end' href="#faq">FAQ</a>
  137. </div>
  138. <div class="container">
  139. <h1>bignumber.js</h1>
  140. <p>A JavaScript library for arbitrary-precision arithmetic.</p>
  141. <p>
  142. <a href="https://github.com/MikeMcl/bignumber.js">Hosted on GitHub</a>.
  143. </p>
  144. <h2>API</h2>
  145. <p>
  146. In all examples below, <code>var</code> and semicolons are not shown, and
  147. if a commented-out value is in quotes it means <code>toString</code> has
  148. been called on the preceding expression.
  149. </p>
  150. <h3>CONSTRUCTOR</h3>
  151. <h5 id="bignumber">
  152. BigNumber<code class='inset'>BigNumber(value [, base]) &rArr;
  153. <i>BigNumber</i>
  154. </code>
  155. </h5>
  156. <dl>
  157. <dt><code>value</code></dt>
  158. <dd>
  159. <i>number|string|BigNumber</i> : See <a href='#range'>RANGE</a> for
  160. range.
  161. </dd>
  162. <dd>
  163. A numeric value.
  164. </dd>
  165. <dd>
  166. Legitimate values include <code>&plusmn;0</code>,
  167. <code>&plusmn;Infinity</code> and <code>NaN</code>.
  168. </dd>
  169. <dd>
  170. Values of type <em>number</em> with more than 15 significant digits are
  171. considered invalid as calling <code>toString</code> or
  172. <code>valueOf</code> on such numbers may not result in the intended
  173. value.
  174. <pre>console.log( 823456789123456.3 ); // 823456789123456.2</pre>
  175. </dd>
  176. <dd>
  177. There is no limit to the number of digits of a value of type
  178. <em>string</em> (other than that of JavaScript's maximum array size).
  179. </dd>
  180. <dd>
  181. Decimal string values may be in exponential, as well as normal
  182. (fixed-point) notation. Non-decimal values must be in normal
  183. notation.
  184. </dd>
  185. <dd>
  186. String values in hexadecimal literal form, e.g. <code>'0xff'</code>, are
  187. invalid, and string values in octal literal form will be interpreted as
  188. decimals, e.g. <code>'011'</code> is interpreted as 11, not 9.
  189. </dd>
  190. <dd>Values in any base may have fraction digits.</dd>
  191. <dd>
  192. For bases from 10 to 36, lower and/or upper case letters can be used to
  193. represent values from 10 to 35. For bases above 36, <code>a-z</code>
  194. represents values from 10 to 35, <code>A-Z</code> from 36 to 61, and
  195. <code>$</code> and <code>_</code> represent 62 and 63 respectively <i>
  196. (this can be changed by editing the <code>DIGITS</code> variable near
  197. the top of the source file).</i>
  198. </dd>
  199. </dl>
  200. <dl>
  201. <dt><code>base</code></dt>
  202. <dd>
  203. <i>number</i> : integer, <code>2</code> to <code>64</code> inclusive
  204. </dd>
  205. <dd>The base of <code>value</code>.</dd>
  206. <dd>If <code>base</code> is omitted, or is <code>null</code> or undefined,
  207. base 10 is assumed.</dd>
  208. </dl>
  209. <p>Returns a new instance of a BigNumber object.</p>
  210. <p>
  211. If a base is specified, the value is rounded according to
  212. the current <a href='#decimal-places'><code>DECIMAL_PLACES</code></a> and
  213. <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> settings.
  214. Usefully, this means the decimal places of a decimal value
  215. passed to the constructor can be limited by explicitly specifying base 10.
  216. </p>
  217. <p>
  218. See <a href='#Errors'>Errors</a> for the treatment of an invalid
  219. <code>value</code> or <code>base</code>.
  220. </p>
  221. <pre>
  222. x = new BigNumber(9) // '9'
  223. y = new BigNumber(x) // '9'
  224. BigNumber(435.345) // 'new' is optional
  225. new BigNumber('5032485723458348569331745.33434346346912144534543')
  226. new BigNumber('4.321e+4') // '43210'
  227. new BigNumber('-735.0918e-430') // '-7.350918e-428'
  228. new BigNumber(Infinity) // 'Infinity'
  229. new BigNumber(NaN) // 'NaN'
  230. new BigNumber('.5') // '0.5'
  231. new BigNumber('+2') // '2'
  232. new BigNumber(-10110100.1, 2) // '-180.5'
  233. new BigNumber('123412421.234324', 5) // '607236.557696'
  234. new BigNumber('ff.8', 16) // '255.5'
  235. new BigNumber(9, 2)
  236. // Throws 'not a base 2 number' if ERRORS is true, otherwise 'NaN'
  237. new BigNumber(96517860459076817.4395)
  238. // Throws 'number type has more than 15 significant digits'
  239. // if ERRORS is true, otherwise '96517860459076820'
  240. new BigNumber('blurgh')
  241. // Throws 'not a number' if ERRORS is true, otherwise 'NaN'
  242. BigNumber.config({DECIMAL_PLACES: 5})
  243. new BigNumber(1.23456789) // '1.23456789'
  244. new BigNumber(1.23456789, 10) // '1.23457'</pre>
  245. <h4 id="methods">Methods</h4>
  246. <p>
  247. The <code>BigNumber</code> constructor has one added method,
  248. <code>config</code>, which configures the library-wide settings for
  249. arithmetic, formatting and errors.
  250. </p>
  251. <h5 id="config">
  252. config<code class='inset'>config([settings]) &rArr; <i>object</i></code>
  253. </h5>
  254. <i>
  255. Note: the settings can also be supplied as an argument list, see below.
  256. </i>
  257. <dl>
  258. <dt><code>settings</code></dt>
  259. <dd><i>object</i></dd>
  260. <dd>
  261. An object that contains some or all of the following properties:
  262. <dl>
  263. <dt id="decimal-places"><code><b>DECIMAL_PLACES</b></code></dt>
  264. <dd>
  265. <i>number</i> : integer, <code>0</code> to <code>1e+9</code>
  266. inclusive<br />
  267. Default value: <code>20</code>
  268. </dd>
  269. <dd>
  270. The <u>maximum</u> number of decimal places of the results of
  271. division, square root and base conversion operations, and power
  272. operations with negative exponents.<br />
  273. I.e. aside from the base conversion which may be involved with any
  274. method that accepts a base argument, the value is only relevant to
  275. the <code>dividedBy</code>, <code>squareRoot</code> and
  276. <code>toPower</code> methods.
  277. </dd>
  278. <dd>
  279. <pre>BigNumber.config({ DECIMAL_PLACES: 5 })
  280. BigNumber.config(5) // equivalent</pre>
  281. </dd>
  282. <dt id="rounding-mode"><code><b>ROUNDING_MODE</b></code></dt>
  283. <dd>
  284. <i>number</i> : integer, <code>0</code> to <code>8</code>
  285. inclusive<br />
  286. Default value: <code>4</code>
  287. <a href="#h-up">(<code>ROUND_HALF_UP</code>)</a>
  288. </dd>
  289. <dd>
  290. The rounding mode used in the above operations and by
  291. <a href='#round'><code>round</code></a>,
  292. <a href='#toE'><code>toExponential</code></a>,
  293. <a href='#toF'><code>toFixed</code></a>,
  294. <a href='#toFo'><code>toFormat</code></a> and
  295. <a href='#toP'><code>toPrecision</code></a>.
  296. </dd>
  297. <dd>
  298. The modes are available as enumerated properties of the BigNumber
  299. constructor.
  300. </dd>
  301. <dd>
  302. <pre>BigNumber.config({ ROUNDING_MODE: 0 })
  303. BigNumber.config(null, BigNumber.ROUND_UP) // equivalent</pre>
  304. </dd>
  305. <dt id="exponential-at"><code><b>EXPONENTIAL_AT</b></code></dt>
  306. <dd>
  307. <i>number</i> : integer, magnitude <code>0</code> to
  308. <code>1e+9</code> inclusive, or<br />
  309. <i>number</i>[] : [ integer -1e+9 to 0 inclusive, integer 0 to 1e+9
  310. inclusive ]<br />
  311. Default value: <code>[-7, 20]</code>
  312. </dd>
  313. <dd>
  314. The exponent value(s) at which <code>toString</code> returns
  315. exponential notation.
  316. </dd>
  317. <dd>
  318. If a single number is assigned, the value is the exponent magnitude.
  319. <br />
  320. If an array of two numbers is assigned then the first number is the
  321. negative exponent value at and beneath which exponential notation is
  322. used, and the second number is the positive exponent value at and
  323. above which the same.
  324. </dd>
  325. <dd>
  326. For example, to emulate JavaScript numbers in terms of the exponent
  327. values at which they begin to use exponential notation, use
  328. <code>[-7, 20]</code>.
  329. </dd>
  330. <dd>
  331. <pre>BigNumber.config({ EXPONENTIAL_AT: 2 })
  332. new BigNumber(12.3) // '12.3' e is only 1
  333. new BigNumber(123) // '1.23e+2'
  334. new BigNumber(0.123) // '0.123' e is only -1
  335. new BigNumber(0.0123) // '1.23e-2'
  336. BigNumber.config({ EXPONENTIAL_AT: [-7, 20] })
  337. new BigNumber(123456789) // '123456789' e is only 8
  338. new BigNumber(0.000000123) // '1.23e-7'
  339. // Almost never return exponential notation:
  340. BigNumber.config({ EXPONENTIAL_AT: 1e+9 })
  341. // Always return exponential notation:
  342. BigNumber.config({ EXPONENTIAL_AT: 0 })</pre>
  343. </dd>
  344. <dd>
  345. Regardless of the value of <code>EXPONENTIAL_AT</code>, the
  346. <code>toFixed</code> method will always return a value in
  347. normal notation and the <code>toExponential</code> method will
  348. always return a value in exponential form.
  349. </dd>
  350. <dd>
  351. Calling <code>toString</code> with a base argument, e.g.
  352. <code>toString(10)</code>, will also always return normal notation.
  353. </dd>
  354. <dt id="range"><code><b>RANGE</b></code></dt>
  355. <dd>
  356. <i>number</i> : integer, magnitude <code>1</code> to
  357. <code>1e+9</code> inclusive, or<br />
  358. <i>number</i>[] : [ integer -1e+9 to -1 inclusive, integer 1 to 1e+9
  359. inclusive ]<br />
  360. Default value: <code>[-1e+9, 1e+9]</code>
  361. </dd>
  362. <dd>
  363. The exponent value(s) beyond which overflow to Infinity and
  364. underflow to zero occurs.
  365. </dd>
  366. <dd>
  367. If a single number is assigned, it is the maximum exponent
  368. magnitude: values wth a positive exponent of greater magnitude
  369. become Infinity and those with a negative exponent of
  370. greater magnitude become zero.
  371. <dd>
  372. If an array of two numbers is assigned then the first number is the
  373. negative exponent limit and the second number is the positive
  374. exponent limit.
  375. </dd>
  376. <dd>
  377. For example, to emulate JavaScript numbers in terms of the exponent
  378. values at which they become zero and Infinity, use
  379. <code>[-324, 308]</code>.
  380. </dd>
  381. <dd>
  382. <pre>BigNumber.config({ RANGE: 500 })
  383. BigNumber.config().RANGE // [ -500, 500 ]
  384. new BigNumber('9.999e499') // '9.999e+499'
  385. new BigNumber('1e500') // 'Infinity'
  386. new BigNumber('1e-499') // '1e-499'
  387. new BigNumber('1e-500') // '0'
  388. BigNumber.config({ RANGE: [-3, 4] })
  389. new BigNumber(99999) // '99999' e is only 4
  390. new BigNumber(100000) // 'Infinity' e is 5
  391. new BigNumber(0.001) // '0.01' e is only -3
  392. new BigNumber(0.0001) // '0' e is -4</pre>
  393. </dd>
  394. <dd>
  395. The largest possible magnitude of a finite BigNumber is<br />
  396. 9.999...e+1000000000<br />
  397. The smallest possible magnitude of a non-zero BigNumber is<br />
  398. 1e-1000000000
  399. </dd>
  400. <dt id="errors"><code><b>ERRORS</b></code></dt>
  401. <dd>
  402. <i>boolean/number</i> : <code>true, false, 1 or 0</code><br />
  403. Default value: <code>true</code>
  404. </dd>
  405. <dd>
  406. The value that determines whether BigNumber Errors are thrown.<br />
  407. If <code>ERRORS</code> is false, this library will not throw errors.
  408. </dd>
  409. <dd>
  410. See <a href='#Errors'>Errors</a>.
  411. </dd>
  412. <dd>
  413. <pre>BigNumber.config({ ERRORS: false })</pre>
  414. </dd>
  415. <dt id="format"><code><b>FORMAT</b></code></dt>
  416. <dd><i>object</i></dd>
  417. <dd>
  418. The <code>FORMAT</code> object configures the format of the string
  419. returned by the <a href='#toFo'><code>toFormat</code></a> method.
  420. </dd>
  421. <dd>
  422. The example below shows the properties of the <code>FORMAT</code>
  423. object that are recognised, and their default values.
  424. </dd>
  425. <dd>
  426. Unlike when changing other settings, the values of the properties
  427. of the <code>FORMAT</code> object will not be checked for validity.
  428. The existing <code>FORMAT</code> object will simply be replaced by
  429. the object that is passed in. Note that all the properties shown
  430. below do not have to be included.
  431. </dd>
  432. <dd>
  433. See <a href='#toFo'><code>toFormat</code></a> for examples of usage.
  434. </dd>
  435. <dd><pre>
  436. BigNumber.config({
  437. FORMAT: {
  438. // the decimal separator
  439. decimalSeparator : '.',
  440. // the grouping separator of the integer part
  441. groupSeparator : ',',
  442. // the primary grouping size of the integer part
  443. groupSize : 3,
  444. // the secondary grouping size of the integer part
  445. secondaryGroupSize : 0,
  446. // the grouping separator of the fraction part
  447. fractionGroupSeparator : ' ',
  448. // the grouping size of the fraction part
  449. fractionGroupSize : 0
  450. }
  451. });</pre></dd>
  452. </dl>
  453. </dd>
  454. </dl>
  455. <p>
  456. <br />Returns an object with the above properties and their current
  457. values.
  458. </p>
  459. <p>
  460. If the value to be assigned to any of the above properties is
  461. <code>null</code> or undefined it is ignored. See
  462. <a href='#Errors'>Errors</a> for the treatment of invalid values.
  463. </p>
  464. <pre>
  465. BigNumber.config({
  466. DECIMAL_PLACES: 40,
  467. ROUNDING_MODE: BigNumber.ROUND_HALF_CEIL,
  468. EXPONENTIAL_AT: [-10, 20],
  469. RANGE: [-500, 500],
  470. ERRORS: true
  471. });
  472. // Alternatively but equivalently:
  473. BigNumber.config( 40, 7, [-10, 20], 500, 1 )
  474. obj = BigNumber.config();
  475. obj.ERRORS // true
  476. obj.RANGE // [-500, 500]</pre>
  477. <h4 id="constructor-properties">Properties</h4>
  478. <p>
  479. The library's enumerated rounding modes are stored as properties of the
  480. constructor.<br />
  481. They are not referenced internally by the library itself.
  482. </p>
  483. <p>
  484. Rounding modes 0 to 6 (inclusive) are the same as those of Java's
  485. BigDecimal class.
  486. </p>
  487. <table>
  488. <tr>
  489. <th>Property</th>
  490. <th>Value</th>
  491. <th>Description</th>
  492. </tr>
  493. <tr>
  494. <td id="round-up"><b>ROUND_UP</b></td>
  495. <td class='centre'>0</td>
  496. <td>Rounds away from zero</td>
  497. </tr>
  498. <tr>
  499. <td id="round-down"><b>ROUND_DOWN</b></td>
  500. <td class='centre'>1</td>
  501. <td>Rounds towards zero</td>
  502. </tr>
  503. <tr>
  504. <td id="round-ceil"><b>ROUND_CEIL</b></td>
  505. <td class='centre'>2</td>
  506. <td>Rounds towards Infinity</td>
  507. </tr>
  508. <tr>
  509. <td id="round-floor"><b>ROUND_FLOOR</b></td>
  510. <td class='centre'>3</td>
  511. <td>Rounds towards -Infinity</td>
  512. </tr>
  513. <tr>
  514. <td id="round-half-up"><b>ROUND_HALF_UP</b></td>
  515. <td class='centre'>4</td>
  516. <td>
  517. Rounds towards nearest neighbour.<br />
  518. If equidistant, rounds away from zero
  519. </td>
  520. </tr>
  521. <tr>
  522. <td id="round-half-down"><b>ROUND_HALF_DOWN</b></td>
  523. <td class='centre'>5</td>
  524. <td>
  525. Rounds towards nearest neighbour.<br />
  526. If equidistant, rounds towards zero
  527. </td>
  528. </tr>
  529. <tr>
  530. <td id="round-half-even"><b>ROUND_HALF_EVEN</b></td>
  531. <td class='centre'>6</td>
  532. <td>
  533. Rounds towards nearest neighbour.<br />
  534. If equidistant, rounds towards even neighbour
  535. </td>
  536. </tr>
  537. <tr>
  538. <td id="round-half-ceil"><b>ROUND_HALF_CEIL</b></td>
  539. <td class='centre'>7</td>
  540. <td>
  541. Rounds towards nearest neighbour.<br />
  542. If equidistant, rounds towards Infinity
  543. </td>
  544. </tr>
  545. <tr>
  546. <td id="round-half-floor"><b>ROUND_HALF_FLOOR</b></td>
  547. <td class='centre'>8</td>
  548. <td>
  549. Rounds towards nearest neighbour.<br />
  550. If equidistant, rounds towards -Infinity
  551. </td>
  552. </tr>
  553. </table>
  554. <pre>
  555. BigNumber.config({ ROUNDING_MODE: BigNumber.ROUND_CEIL })
  556. BigNumber.config({ ROUNDING_MODE: 2 }) // equivalent</pre>
  557. <h3>INSTANCE</h3>
  558. <h4 id="prototype-methods">Methods</h4>
  559. <p>
  560. The methods inherited by a BigNumber instance from its constructor's
  561. prototype object.
  562. </p>
  563. <p>
  564. A BigNumber is immutable in the sense that it is not changed by its
  565. methods.
  566. </p>
  567. <p>
  568. The treatment of &plusmn;<code>0</code>, &plusmn;<code>Infinity</code> and
  569. <code>NaN</code> is consistent with how JavaScript treats these values.
  570. </p>
  571. <p>
  572. Many method names have a shorter alias.<br />
  573. Internally, the library always uses the shorter method names.
  574. </p>
  575. <h5 id="abs">
  576. absoluteValue<code class='inset'>.abs() &rArr; <i>BigNumber</i></code>
  577. </h5>
  578. <p>
  579. Returns a BigNumber whose value is the absolute value, i.e. the magnitude,
  580. of this BigNumber.
  581. </p>
  582. <pre>
  583. x = new BigNumber(-0.8)
  584. y = x.absoluteValue() // '0.8'
  585. z = y.abs() // '0.8'</pre>
  586. <h5 id="ceil">
  587. ceil<code class='inset'>.ceil() &rArr; <i>BigNumber</i></code>
  588. </h5>
  589. <p>
  590. Returns a BigNumber whose value is the value of this BigNumber rounded to
  591. a whole number in the direction of Infinity.
  592. </p>
  593. <pre>
  594. x = new BigNumber(1.3)
  595. x.ceil() // '2'
  596. y = new BigNumber(-1.8)
  597. y.ceil() // '-1'</pre>
  598. <h5 id="cmp">
  599. comparedTo<code class='inset'>.cmp(n [, base]) &rArr; <i>number</i></code>
  600. </h5>
  601. <p>
  602. <code>n</code> : <i>number|string|BigNumber</i><br />
  603. <code>base</code> : <i>number</i><br />
  604. <i>
  605. See <a href="#bignumber">constructor</a> for further parameter details.
  606. </i>
  607. </p>
  608. <table>
  609. <tr>
  610. <th>Returns</th>
  611. <th colspan=2>&nbsp;</th>
  612. </tr>
  613. <tr>
  614. <td class='centre'>1</td>
  615. <td>
  616. If the value of this BigNumber is greater than the value of
  617. <code>n</code>
  618. </td>
  619. </tr>
  620. <tr>
  621. <td class='centre'>-1</td>
  622. <td>
  623. If the value of this BigNumber is less than the value of
  624. <code>n</code>
  625. </td>
  626. </tr>
  627. <tr>
  628. <td class='centre'>0</td>
  629. <td>If this BigNumber and <code>n</code> have the same value</td>
  630. </tr>
  631. <tr>
  632. <td class='centre'>null</td>
  633. <td>
  634. if the value of either this BigNumber or <code>n</code> is
  635. <code>NaN</code>
  636. </td>
  637. </tr>
  638. </table>
  639. <pre>
  640. x = new BigNumber(Infinity)
  641. y = new BigNumber(5)
  642. x.comparedTo(y) // 1
  643. x.comparedTo(x.minus(1)) // 0
  644. y.cmp(NaN) // null
  645. y.cmp('110', 2) // -1</pre>
  646. <h5 id="dp">
  647. decimalPlaces<code class='inset'>.dp() &rArr; <i>number</i></code>
  648. </h5>
  649. <p>
  650. Return the number of decimal places of the value of this BigNumber, or
  651. <code>null</code> if the value of this BigNumber is
  652. <code>&plusmn;Infinity</code> or <code>NaN</code>.
  653. </p>
  654. <pre>
  655. x = new BigNumber(123.45)
  656. x.decimalPlaces() // 2
  657. y = new BigNumber('9.9e-101')
  658. y.dp() // 102</pre>
  659. <h5 id="div">
  660. dividedBy<code class='inset'>.div(n [, base]) &rArr;
  661. <i>BigNumber</i></code>
  662. </h5>
  663. <p>
  664. <code>n</code> : <i>number|string|BigNumber</i><br />
  665. <code>base</code> : <i>number</i><br />
  666. <i>
  667. See <a href="#bignumber">constructor</a> for further parameter details.
  668. </i>
  669. </p>
  670. <p>
  671. Returns a BigNumber whose value is the value of this BigNumber divided by
  672. <code>n</code>, rounded according to the current
  673. <a href='#decimal-places'><code>DECIMAL_PLACES</code></a> and
  674. <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> settings.
  675. </p>
  676. <pre>
  677. x = new BigNumber(355)
  678. y = new BigNumber(113)
  679. x.dividedBy(y) // '3.14159292035398230088'
  680. x.div(5) // '71'
  681. x.div(47, 16) // '5'</pre>
  682. <h5 id="divInt">
  683. dividedToIntegerBy<code class='inset'>.divToInt(n [, base]) &rArr;
  684. <i>BigNumber</i></code>
  685. </h5>
  686. <p>
  687. <code>n</code> : <i>number|string|BigNumber</i><br />
  688. <code>base</code> : <i>number</i><br />
  689. <i>
  690. See <a href="#bignumber">constructor</a> for further parameter details.
  691. </i>
  692. </p>
  693. <p>
  694. Return a BigNumber whose value is the integer part (truncating towards
  695. zero) of dividing the value of this BigNumber by <code>n</code>.
  696. </p>
  697. <pre>
  698. x = new BigNumber(5)
  699. y = new BigNumber(3)
  700. x.dividedToIntegerBy(y) // '1'
  701. x.divToInt(0.7) // '7'
  702. x.divToInt('0.f', 16) // '5'</pre>
  703. <h5 id="eq">
  704. equals<code class='inset'>.eq(n [, base]) &rArr; <i>boolean</i></code>
  705. </h5>
  706. <p>
  707. <code>n</code> : <i>number|string|BigNumber</i><br />
  708. <code>base</code> : <i>number</i><br />
  709. <i>
  710. See <a href="#bignumber">constructor</a> for further parameter details.
  711. </i>
  712. </p>
  713. <p>
  714. Returns <code>true</code> if the value of this BigNumber equals the value
  715. of <code>n</code>, otherwise returns <code>false</code>.<br />
  716. As with JavaScript, NaN does not equal NaN.
  717. <br />Note : This method uses the <code>comparedTo</code> method
  718. internally.
  719. </p>
  720. <pre>
  721. 0 === 1e-324 // true
  722. x = new BigNumber(0)
  723. x.equals('1e-324') // false
  724. BigNumber(-0).eq(x) // true ( -0 === 0 )
  725. BigNumber(255).eq('ff', 16) // true
  726. y = new BigNumber(NaN)
  727. y.equals(NaN) // false</pre>
  728. <h5 id="floor">
  729. floor<code class='inset'>.floor() &rArr;
  730. <i>BigNumber</i></code>
  731. </h5>
  732. <p>
  733. Returns a BigNumber whose value is the value of this BigNumber rounded to
  734. a whole number in the direction of -Infinity.
  735. </p>
  736. <pre>
  737. x = new BigNumber(1.8)
  738. x.floor() // '1'
  739. y = new BigNumber(-1.3)
  740. y.floor() // '-2'</pre>
  741. <h5 id="gt">
  742. greaterThan<code class='inset'>.gt(n [, base]) &rArr;
  743. <i>boolean</i></code>
  744. </h5>
  745. <p>
  746. <code>n</code> : <i>number|string|BigNumber</i><br />
  747. <code>base</code> : <i>number</i><br />
  748. <i>
  749. See <a href="#bignumber">constructor</a> for further parameter details.
  750. </i>
  751. </p>
  752. <p>
  753. Returns <code>true</code> if the value of this BigNumber is greater than
  754. the value of <code>n</code>, otherwise returns <code>false</code>.<br />
  755. Note : This method uses the <code>comparedTo</code> method internally.
  756. </p>
  757. <pre>
  758. 0.1 &gt; (0.3 - 0.2) // true
  759. x = new BigNumber(0.1)
  760. x.greaterThan(BigNumber(0.3).minus(0.2)) // false
  761. BigNumber(0).gt(x) // false
  762. BigNumber(11, 3).gt(11.1, 2) // true</pre>
  763. <h5 id="gte">
  764. greaterThanOrEqualTo<code class='inset'>.gte(n [, base]) &rArr;
  765. <i>boolean</i></code>
  766. </h5>
  767. <p>
  768. <code>n</code> : <i>number|string|BigNumber</i><br />
  769. <code>base</code> : <i>number</i><br />
  770. <i>
  771. See <a href="#bignumber">constructor</a> for further parameter details.
  772. </i>
  773. </p>
  774. <p>
  775. Returns <code>true</code> if the value of this BigNumber is greater than
  776. or equal to the value of <code>n</code>, otherwise returns
  777. <code>false</code>.<br />
  778. Note : This method uses the <code>comparedTo</code> method internally.
  779. </p>
  780. <pre>
  781. (0.3 - 0.2) &gt;= 0.1 // false
  782. x = new BigNumber(0.3).minus(0.2)
  783. x.greaterThanOrEqualTo(0.1) // true
  784. BigNumber(1).gte(x) // true
  785. BigNumber(10, 18).gte('i', 36) // true</pre>
  786. <h5 id="isF">
  787. isFinite<code class='inset'>.isFinite() &rArr; <i>boolean</i></code>
  788. </h5>
  789. <p>
  790. Returns <code>true</code> if the value of this BigNumber is a finite
  791. number, otherwise returns <code>false</code>.<br />
  792. The only possible non-finite values of a BigNumber are NaN, Infinity and
  793. -Infinity.
  794. </p>
  795. <pre>
  796. x = new BigNumber(1)
  797. x.isFinite() // true
  798. y = new BigNumber(Infinity)
  799. y.isFinite() // false</pre>
  800. <p>
  801. Note: The native method <code>isFinite()</code> can be used if
  802. <code>n &lt;= Number.MAX_VALUE</code>.
  803. </p>
  804. <h5 id="isInt">
  805. isInteger<code class='inset'>.isInt() &rArr; <i>boolean</i></code>
  806. </h5>
  807. <p>
  808. Returns <code>true</code> if the value of this BigNumber is a whole
  809. number, otherwise returns <code>false</code>.
  810. </p>
  811. <pre>
  812. x = new BigNumber(1)
  813. x.isInteger() // true
  814. y = new BigNumber(123.456)
  815. y.isInt() // false</pre>
  816. <h5 id="isNaN">
  817. isNaN<code class='inset'>.isNaN() &rArr; <i>boolean</i></code>
  818. </h5>
  819. <p>
  820. Returns <code>true</code> if the value of this BigNumber is NaN, otherwise
  821. returns <code>false</code>.<br />
  822. </p>
  823. <pre>
  824. x = new BigNumber(NaN)
  825. x.isNaN() // true
  826. y = new BigNumber('Infinity')
  827. y.isNaN() // false</pre>
  828. <p>
  829. Note: The native method <code>isNaN()</code> can also be used.
  830. </p>
  831. <h5 id="isNeg">
  832. isNegative<code class='inset'>.isNeg() &rArr; <i>boolean</i></code>
  833. </h5>
  834. <p>
  835. Returns <code>true</code> if the value of this BigNumber is negative,
  836. otherwise returns <code>false</code>.<br />
  837. </p>
  838. <pre>
  839. x = new BigNumber(-0)
  840. x.isNegative() // true
  841. y = new BigNumber(2)
  842. y.isNeg // false</pre>
  843. <p>
  844. Note: <code>n &lt; 0</code> can be used if
  845. <code>n &lt;= -Number.MIN_VALUE</code>.
  846. </p>
  847. <h5 id="isZ">
  848. isZero<code class='inset'>.isZero() &rArr; <i>boolean</i></code>
  849. </h5>
  850. <p>
  851. Returns <code>true</code> if the value of this BigNumber is zero or minus
  852. zero, otherwise returns <code>false</code>.<br />
  853. </p>
  854. <pre>
  855. x = new BigNumber(-0)
  856. x.isZero() && x.isNeg() // true
  857. y = new BigNumber(Infinity)
  858. y.isZero() // false</pre>
  859. <p>
  860. Note: <code>n == 0</code> can be used if
  861. <code>n &gt;= Number.MIN_VALUE</code>.
  862. </p>
  863. <h5 id="lt">
  864. lessThan<code class='inset'>.lt(n [, base]) &rArr; <i>boolean</i></code>
  865. </h5>
  866. <p>
  867. <code>n</code> : <i>number|string|BigNumber</i><br />
  868. <code>base</code> : <i>number</i><br />
  869. <i>
  870. See <a href="#bignumber">constructor</a> for further parameter details.
  871. </i>
  872. </p>
  873. <p>
  874. Returns <code>true</code> if the value of this BigNumber is less than the
  875. value of <code>n</code>, otherwise returns <code>false</code>.<br />
  876. Note : This method uses the <code>comparedTo</code> method internally.
  877. </p>
  878. <pre>
  879. (0.3 - 0.2) &lt; 0.1 // true
  880. x = new BigNumber(0.3).minus(0.2)
  881. x.lessThan(0.1) // false
  882. BigNumber(0).lt(x) // true
  883. BigNumber(11.1, 2).lt(11, 3) // true</pre>
  884. <h5 id="lte">
  885. lessThanOrEqualTo<code class='inset'>.lte(n [, base]) &rArr;
  886. <i>boolean</i></code>
  887. </h5>
  888. <p>
  889. <code>n</code> : <i>number|string|BigNumber</i><br />
  890. <code>base</code> : <i>number</i><br />
  891. <i>
  892. See <a href="#bignumber">constructor</a> for further parameter details.
  893. </i>
  894. </p>
  895. <p>
  896. Returns <code>true</code> if the value of this BigNumber is less than or
  897. equal to the value of <code>n</code>, otherwise returns
  898. <code>false</code>.<br />
  899. Note : This method uses the <code>comparedTo</code> method internally.
  900. </p>
  901. <pre>
  902. 0.1 &lt;= (0.3 - 0.2) // false
  903. x = new BigNumber(0.1)
  904. x.lessThanOrEqualTo(BigNumber(0.3).minus(0.2)) // true
  905. BigNumber(-1).lte(x) // true
  906. BigNumber(10, 18).lte('i', 36) // true</pre>
  907. <h5 id="minus">
  908. minus<code class='inset'>.minus(n [, base]) &rArr; <i>BigNumber</i></code>
  909. </h5>
  910. <p>
  911. <code>n</code> : <i>number|string|BigNumber</i><br />
  912. <code>base</code> : <i>number</i><br />
  913. <i>
  914. See <a href="#bignumber">constructor</a> for further parameter details.
  915. </i>
  916. </p>
  917. <p>
  918. Returns a BigNumber whose value is the value of this BigNumber minus
  919. <code>n</code>.
  920. </p>
  921. <pre>
  922. 0.3 - 0.1 // 0.19999999999999998
  923. x = new BigNumber(0.3)
  924. x.minus(0.1) // '0.2'
  925. x.minus(0.6, 20) // '0'</pre>
  926. <h5 id="mod">
  927. modulo<code class='inset'>.mod(n [, base]) &rArr; <i>BigNumber</i></code>
  928. </h5>
  929. <p>
  930. <code>n</code> : <i>number|string|BigNumber</i><br />
  931. <code>base</code> : <i>number</i><br />
  932. <i>
  933. See <a href="#bignumber">constructor</a> for further parameter details.
  934. </i>
  935. </p>
  936. <p>
  937. Returns a BigNumber whose value is the value of this BigNumber modulo
  938. <code>n</code>, i.e. the integer remainder of dividing this BigNumber by
  939. <code>n</code>.
  940. </p>
  941. <p>
  942. The result will have the same sign as this BigNumber, and it will match
  943. that of JavaScript's % operator (within the limits of its precision) and
  944. BigDecimal's remainder method.
  945. </p>
  946. <pre>
  947. 1 % 0.9 // 0.09999999999999998
  948. x = new BigNumber(1)
  949. x.modulo(0.9) // '0.1'
  950. y = new BigNumber(33)
  951. y.mod('a', 33) // '3'</pre>
  952. <h5 id="neg">
  953. negated<code class='inset'>.neg() &rArr; <i>BigNumber</i></code>
  954. </h5>
  955. <p>
  956. Returns a BigNumber whose value is the value of this BigNumber negated,
  957. i.e. multiplied by -1.
  958. </p>
  959. <pre>
  960. x = new BigNumber(1.8)
  961. x.negated() // '-1.8'
  962. y = new BigNumber(-1.3)
  963. y.neg() // '1.3'</pre>
  964. <h5 id="plus">
  965. plus<code class='inset'>.plus(n [, base]) &rArr; <i>BigNumber</i></code>
  966. </h5>
  967. <p>
  968. <code>n</code> : <i>number|string|BigNumber</i><br />
  969. <code>base</code> : <i>number</i><br />
  970. <i>
  971. See <a href="#bignumber">constructor</a> for further parameter details.
  972. </i>
  973. </p>
  974. <p>
  975. Returns a BigNumber whose value is the value of this BigNumber plus
  976. <code>n</code>.
  977. </p>
  978. <pre>
  979. 0.1 + 0.2 // 0.30000000000000004
  980. x = new BigNumber(0.1)
  981. y = x.plus(0.2) // '0.3'
  982. BigNumber(0.7).plus(x).plus(y) // '1'
  983. x.plus('0.1', 8) // '0.225'</pre>
  984. <h5 id="round">
  985. round
  986. <code class='inset'>.round([dp [, rm]]) &rArr; <i>BigNumber</i></code>
  987. </h5>
  988. <p>
  989. <code>dp</code> : <i>number</i> : integer, 0 to 1e+9 inclusive<br />
  990. <code>rm</code> : <i>number</i> : integer, 0 to 8 inclusive
  991. </p>
  992. <p>
  993. Returns a BigNumber whose value is the value of this BigNumber rounded by
  994. rounding mode <code>rm</code> to a maximum of <code>dp</code> decimal
  995. places.
  996. </p>
  997. <p>
  998. if <code>dp</code> is omitted, or is <code>null</code> or undefined, the
  999. return value is <code>n</code> rounded to a whole number.
  1000. </p>
  1001. <p>
  1002. if <code>rm</code> is omitted, or is <code>null</code> or undefined, the
  1003. current <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> setting is
  1004. used.
  1005. </p>
  1006. <p>
  1007. See <a href='#Errors'>Errors</a> for the treatment of other non-integer or
  1008. out of range <code>dp</code> or <code>rm</code> values.
  1009. </p>
  1010. <pre>
  1011. x = 1234.56
  1012. Math.round(x) // 1235
  1013. y = new BigNumber(x)
  1014. y.round() // '1235'
  1015. y.round(1) // '1234.6'
  1016. y.round(2) // '1234.56'
  1017. y.round(10) // '1234.56'
  1018. y.round(0, 1) // '1234'
  1019. y.round(0, 6) // '1235'
  1020. y.round(1, 1) // '1234.5'
  1021. y.round(1, BigNumber.ROUND_HALF_EVEN) // '1234.6'
  1022. y // '1234.56'</pre>
  1023. <h5 id="sqrt">
  1024. squareRoot<code class='inset'>.sqrt() &rArr; <i>BigNumber</i></code>
  1025. </h5>
  1026. <p>
  1027. Returns a BigNumber whose value is the square root of this BigNumber,
  1028. correctly rounded according to the current
  1029. <a href='#decimal-places'><code>DECIMAL_PLACES</code></a> and
  1030. <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> settings.
  1031. </p>
  1032. <pre>
  1033. x = new BigNumber(16)
  1034. x.squareRoot() // '4'
  1035. y = new BigNumber(3)
  1036. y.sqrt() // '1.73205080756887729353'</pre>
  1037. <h5 id="times">
  1038. times<code class='inset'>.times(n [, base]) &rArr; <i>BigNumber</i></code>
  1039. </h5>
  1040. <p>
  1041. <code>n</code> : <i>number|string|BigNumber</i><br />
  1042. <code>base</code> : <i>number</i><br />
  1043. <i>
  1044. See <a href="#bignumber">constructor</a> for further parameter details.
  1045. </i>
  1046. </p>
  1047. <p>
  1048. Returns a BigNumber whose value is the value of this BigNumber times
  1049. <code>n</code>.
  1050. </p>
  1051. <pre>
  1052. 0.6 * 3 // 1.7999999999999998
  1053. x = new BigNumber(0.6)
  1054. y = x.times(3) // '1.8'
  1055. BigNumber('7e+500').times(y) // '1.26e+501'
  1056. x.times('-a', 16) // '-6'</pre>
  1057. <h5 id="toE">
  1058. toExponential<code class='inset'>.toExponential([dp]) &rArr; <i>string</i></code>
  1059. </h5>
  1060. <p>
  1061. <code>dp</code> : <i>number</i> : integer, 0 to 1e+9 inclusive
  1062. </p>
  1063. <p>
  1064. Returns a string representing the value of this BigNumber in exponential
  1065. notation to the specified decimal places, i.e with one digit before the
  1066. decimal point and <code>dp</code> digits after it. If rounding
  1067. is necessary, the current
  1068. <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> is used.
  1069. </p>
  1070. <p>
  1071. If the value of this BigNumber in exponential notation has fewer fraction
  1072. digits then is specified by <code>dp</code>, the return value will be
  1073. appended with zeros accordingly.
  1074. </p>
  1075. <p>
  1076. If <code>dp</code> is omitted, or is <code>null</code> or undefined, the
  1077. number of digits after the decimal point defaults to the minimum number of
  1078. digits necessary to represent the value exactly.
  1079. </p>
  1080. <p>
  1081. See <a href='#Errors'>Errors</a> for the treatment of other
  1082. non-integer or out of range <code>dp</code> values.
  1083. </p>
  1084. <pre>
  1085. x = 45.6
  1086. y = new BigNumber(x)
  1087. x.toExponential() // '4.56e+1'
  1088. y.toExponential() // '4.56e+1'
  1089. x.toExponential(0) // '5e+1'
  1090. y.toExponential(0) // '5e+1'
  1091. x.toExponential(1) // '4.6e+1'
  1092. y.toExponential(1) // '4.6e+1'
  1093. x.toExponential(3) // '4.560e+1'
  1094. y.toExponential(3) // '4.560e+1'</pre>
  1095. <h5 id="toF">
  1096. toFixed<code class='inset'>.toFixed([dp]) &rArr; <i>string</i></code>
  1097. </h5>
  1098. <p>
  1099. <code>dp</code> : <i>number</i> : integer, 0 to 1e+9 inclusive
  1100. </p>
  1101. <p>
  1102. Returns a string representing the value of this BigNumber in normal
  1103. notation to the specified fixed number of decimal places, i.e. with
  1104. <code>dp</code> digits after the decimal point. If rounding is necessary,
  1105. the current <a href='#rounding-mode'><code>ROUNDING_MODE</code></a>
  1106. setting is used.
  1107. </p>
  1108. <p>
  1109. If the value of this BigNumber in normal notation has fewer fraction
  1110. digits then is specified by <code>dp</code>, the return value will be
  1111. appended with zeros accordingly.
  1112. </p>
  1113. <p>
  1114. Unlike <code>Number.prototype.toFixed</code>, which returns
  1115. exponential notation if a number is greater or equal to 10<sup>21</sup>,
  1116. this method will always return normal notation.
  1117. </p>
  1118. <p>
  1119. If <code>dp</code> is omitted, or is <code>null</code> or undefined, then
  1120. the return value is the same as <code>n.toString()</code>. This is also
  1121. unlike <code>Number.prototype.toFixed</code>, which returns the value to
  1122. zero decimal places.
  1123. </p>
  1124. <p>
  1125. See <a href='#Errors'>Errors</a> for the treatment of other
  1126. non-integer or out of range <code>dp</code> values.
  1127. </p>
  1128. <pre>
  1129. x = 45.6
  1130. y = new BigNumber(x)
  1131. x.toFixed() // '46'
  1132. y.toFixed() // '45.6'
  1133. y.toFixed(0) // '46'
  1134. x.toFixed(3) // '45.600'
  1135. y.toFixed(3) // '45.600'</pre>
  1136. <h5 id="toFo">
  1137. toFormat<code class='inset'>.toFormat([dp]) &rArr; <i>string</i></code>
  1138. </h5>
  1139. <p>
  1140. <code>dp</code> : <i>number</i> : integer, 0 to 1e+9 inclusive
  1141. </p>
  1142. <p>
  1143. Returns a string representing the value of this BigNumber in normal
  1144. notation rounded to <code>dp</code> decimal places using the current
  1145. <a href='#rounding-mode'><code>ROUNDING_MODE</code></a>, and formatted
  1146. according to the properties of the
  1147. <a href='#format'><code>FORMAT</code></a> object.
  1148. </p>
  1149. <p>
  1150. See the examples below for the properties of the
  1151. <a href='#format'><code>FORMAT</code></a> object, their types and their
  1152. usage.
  1153. </p>
  1154. <p>
  1155. If <code>dp</code> is omitted or is <code>null</code> or undefined, then
  1156. the return value is not rounded to a fixed number of decimal places.
  1157. </p>
  1158. <pre>
  1159. format = {
  1160. decimalSeparator: '.',
  1161. groupSeparator: ',',
  1162. groupSize: 3,
  1163. secondaryGroupSize: 0,
  1164. fractionGroupSeparator: ' ',
  1165. fractionGroupSize: 0
  1166. }
  1167. BigNumber.config({ FORMAT: format })
  1168. x = new BigNumber('123456789.123456789')
  1169. x.toFormat() // '123,456,789.123456789'
  1170. x.toFormat(1) // '123,456,789.1'
  1171. // If a reference to the object assigned to FORMAT has been retained,
  1172. // the format properties can be changed directly
  1173. format.groupSeparator = ' '
  1174. format.fractionGroupSize = 5
  1175. x.toFormat() // '123 456 789.12345 6789'
  1176. BigNumber.config({
  1177. FORMAT: {
  1178. decimalSeparator = ',',
  1179. groupSeparator = '.',
  1180. groupSize = 3,
  1181. secondaryGroupSize = 2
  1182. }
  1183. })
  1184. x.toFormat(6) // '12.34.56.789,123'</pre>
  1185. <h5 id="toFr">
  1186. toFraction<code class='inset'>.toFraction([max_denominator]) &rArr;
  1187. <i>[string, string]</i></code>
  1188. </h5>
  1189. <p>
  1190. <code>max_denominator</code> : <i>number|string|BigNumber</i> :
  1191. integer &gt;= 1 and &lt; Infinity
  1192. </p>
  1193. <p>
  1194. Returns a string array representing the value of this BigNumber as a
  1195. simple fraction with an integer numerator and an integer denominator. The
  1196. denominator will be a positive non-zero value less than or equal to
  1197. <code>max_denominator</code>.
  1198. </p>
  1199. <p>
  1200. If a maximum denominator is not specified, or is <code>null</code> or
  1201. undefined, the denominator will be the lowest value necessary to represent
  1202. the number exactly.
  1203. </p>
  1204. <p>
  1205. See <a href='#Errors'>Errors</a> for the treatment of other non-integer or
  1206. out of range <code>max_denominator</code> values.
  1207. </p>
  1208. <pre>
  1209. x = new BigNumber(1.75)
  1210. x.toFraction() // '7, 4'
  1211. pi = new BigNumber('3.14159265358')
  1212. pi.toFraction() // '157079632679,50000000000'
  1213. pi.toFraction(100000) // '312689, 99532'
  1214. pi.toFraction(10000) // '355, 113'
  1215. pi.toFraction(100) // '311, 99'
  1216. pi.toFraction(10) // '22, 7'
  1217. pi.toFraction(1) // '3, 1'</pre>
  1218. <h5 id="toJSON">
  1219. toJSON<code class='inset'>.toJSON() &rArr; <i>string</i></code>
  1220. </h5>
  1221. <p>
  1222. As <code>toString</code>.
  1223. </p>
  1224. <pre>
  1225. x = new BigNumber('123.4e+567')
  1226. str = JSON.stringify(x)</pre>
  1227. <h5 id="toN">
  1228. toNumber<code class='inset'>.toNumber() &rArr; <i>number</i></code>
  1229. </h5>
  1230. <p>Returns the value of this BigNumber as a number primitive.</p>
  1231. <p>
  1232. Type coercion with, for example, JavaScript's unary plus operator can
  1233. alternatively be used, but then a BigNumber with the value minus zero will
  1234. convert to positive zero.
  1235. </p>
  1236. <pre>
  1237. x = new BigNumber(456.789)
  1238. x.toNumber() // 456.789
  1239. +x // 456.789
  1240. y = new BigNumber('45987349857634085409857349856430985')
  1241. y.toNumber() // 4.598734985763409e+34
  1242. z = new BigNumber(-0)
  1243. 1 / +z // Infinity
  1244. 1 / z.toNumber() // -Infinity</pre>
  1245. <h5 id="pow">
  1246. toPower<code class='inset'>.pow(n) &rArr; <i>BigNumber</i></code>
  1247. </h5>
  1248. <p>
  1249. <code>n</code> : <i>number</i> : integer, -1e+6 to 1e+6 inclusive
  1250. </p>
  1251. <p>
  1252. Returns a BigNumber whose value is the value of this BigNumber raised to
  1253. the power <code>n</code>.
  1254. </p>
  1255. <p>
  1256. If <code>n</code> is negative the result is rounded according to the
  1257. current <a href='#decimal-places'><code>DECIMAL_PLACES</code></a> and
  1258. <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> settings.
  1259. </p>
  1260. <p>
  1261. If <code>n</code> is not an integer or is out of range:
  1262. </p>
  1263. <p class='inset'>
  1264. If <code>ERRORS</code> is <code>true</code> a BigNumber
  1265. Error is thrown,<br />
  1266. else if <code>n</code> is greater than 1e+6, it is interpreted as
  1267. <code>Infinity</code>;<br />
  1268. else if <code>n</code> is less than -1e+6, it is interpreted as
  1269. <code>-Infinity</code>;<br />
  1270. else if <code>n</code> is otherwise a number, it is truncated to an
  1271. integer;<br />
  1272. else it is interpreted as <code>NaN</code>.
  1273. </p>
  1274. <p>
  1275. Note: High value exponents may cause this method to be slow to return.
  1276. </p>
  1277. <pre>
  1278. Math.pow(0.7, 2) // 0.48999999999999994
  1279. x = new BigNumber(0.7)
  1280. x.toPower(2) // '0.49'
  1281. BigNumber(3).pow(-2) // '0.11111111111111111111'
  1282. new BigNumber(2).toPower(100000).toString().length // 30111</pre>
  1283. <h5 id="toP">
  1284. toPrecision<code class='inset'>.toPrecision([sd]) &rArr;
  1285. <i>string</i></code>
  1286. </h5>
  1287. <p>
  1288. <code>sd</code> : <i>number</i> : integer, 1 to 1e+9 inclusive
  1289. </p>
  1290. <p>
  1291. Returns a string representing the value of this BigNumber to
  1292. <code>sd</code> significant digits. If rounding is necessary, the
  1293. current <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> setting
  1294. is used.
  1295. </p>
  1296. <p>
  1297. If <code>sd</code> is less than the number of digits necessary to
  1298. represent the integer part of the value in normal notation, then
  1299. exponential notation is used.
  1300. </p>
  1301. <p>
  1302. If <code>sd</code> is omitted, or is <code>null</code> or undefined,
  1303. then the return value is the same as <code>n.toString()</code>.
  1304. </p>
  1305. <p>
  1306. See <a href='#Errors'>Errors</a> for the treatment of other
  1307. non-integer or out of range <code>sd</code> values.
  1308. </p>
  1309. <pre>
  1310. x = 45.6
  1311. y = new BigNumber(x)
  1312. x.toPrecision() // '45.6'
  1313. y.toPrecision() // '45.6'
  1314. x.toPrecision(1) // '5e+1'
  1315. y.toPrecision(1) // '5e+1'
  1316. x.toPrecision(5) // '45.600'
  1317. y.toPrecision(5) // '45.600'</pre>
  1318. <h5 id="toS">
  1319. toString<code class='inset'>.toString([base]) &rArr; <i>string</i></code>
  1320. </h5>
  1321. <p>
  1322. <code>base</code> : <i>number</i> : integer, 2 to 64 inclusive
  1323. </p>
  1324. <p>
  1325. Returns a string representing the value of this BigNumber in the specified
  1326. base, or base 10 if <code>base</code> is omitted. For bases above 10,
  1327. values from 10 to 35 are represented by <code>a-z</code> (as with
  1328. <code>Number.toString</code>), 36 to 61 by <code>A-Z</code>, and 62 and 63
  1329. by <code>$</code> and <code>_</code> respectively.
  1330. </p>
  1331. <p>
  1332. If a base is specified the value is rounded according to the current
  1333. <a href='#decimal-places'><code>DECIMAL_PLACES</code></a>
  1334. and <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> settings.
  1335. </p>
  1336. <p>
  1337. If a base is not specified, and this BigNumber has a positive
  1338. exponent that is equal to or greater than the positive component of the
  1339. current <a href="#exponential-at"><code>EXPONENTIAL_AT</code></a> setting,
  1340. or a negative exponent equal to or less than the negative component of the
  1341. setting, then exponential notation is returned.
  1342. </p>
  1343. <p>
  1344. If <code>base</code> is <code>null</code> or undefined it is ignored.
  1345. <br />
  1346. See <a href='#Errors'>Errors</a> for the treatment of other non-integer or
  1347. out of range <code>base</code> values.
  1348. </p>
  1349. <pre>
  1350. x = new BigNumber(750000)
  1351. x.toString() // '750000'
  1352. BigNumber.config({ EXPONENTIAL_AT: 5 })
  1353. x.toString() // '7.5e+5'
  1354. y = new BigNumber(362.875)
  1355. y.toString(2) // '101101010.111'
  1356. y.toString(9) // '442.77777777777777777778'
  1357. y.toString(32) // 'ba.s'
  1358. BigNumber.config({ DECIMAL_PLACES: 4 });
  1359. z = new BigNumber('1.23456789')
  1360. z.toString() // '1.23456789'
  1361. z.toString(10) // '1.2346'</pre>
  1362. <h5 id="valueOf">
  1363. valueOf<code class='inset'>.valueOf() &rArr; <i>string</i></code>
  1364. </h5>
  1365. <p>
  1366. As <code>toString</code>, but does not accept a base argument.
  1367. </p>
  1368. <pre>
  1369. x = new BigNumber('1.777e+457')
  1370. x.valueOf() // '1.777e+457'</pre>
  1371. <h4 id="instance-properties">Properties</h4>
  1372. <p>
  1373. A BigNumber is an object with three properties:
  1374. </p>
  1375. <table>
  1376. <tr>
  1377. <th>Property</th>
  1378. <th>Description</th>
  1379. <th>Type</th>
  1380. <th>Value</th>
  1381. </tr>
  1382. <tr>
  1383. <td class='centre' id='coefficient'><b>c</b></td>
  1384. <td>coefficient<sup>*</sup></td>
  1385. <td><i>number</i><code>[]</code></td>
  1386. <td> Array of base 1e14 numbers</td>
  1387. </tr>
  1388. <tr>
  1389. <td class='centre' id='exponent'><b>e</b></td>
  1390. <td>exponent</td>
  1391. <td><i>number</i></td>
  1392. <td>Integer, -1e9 to 1e9 inclusive</td>
  1393. </tr>
  1394. <tr>
  1395. <td class='centre' id='sign'><b>s</b></td>
  1396. <td>sign</td>
  1397. <td><i>number</i></td>
  1398. <td>-1 or 1</td>
  1399. </tr>
  1400. </table>
  1401. <p><sup>*</sup>significand</p>
  1402. <p>
  1403. The value of any of the three properties may also be <code>null</code>.
  1404. </p>
  1405. <p>
  1406. From version 2 of this library, the value of the coefficient of a
  1407. BigNumber is stored in a normalised base <code>100000000000000</code>
  1408. floating point format, as opposed to the base 10 format used in version 1.
  1409. </p>
  1410. <p>
  1411. This change means the properties of a BigNumber are now best considered to
  1412. be read-only. Previously it was acceptable to change the exponent of a
  1413. BigNumber by writing to its exponent property directly, but this is no
  1414. longer recommended as the number of digits in the first element of the
  1415. coefficient array is dependent on the exponent, so the coefficient would
  1416. also need to be altered.
  1417. </p>
  1418. <p>
  1419. Note that, as with JavaScript numbers, the original exponent and
  1420. fractional trailing zeros are not preserved.
  1421. </p>
  1422. <pre>x = new BigNumber(0.123) // '0.123'
  1423. x.toExponential() // '1.23e-1'
  1424. x.c // '1,2,3'
  1425. x.e // -1
  1426. x.s // 1
  1427. y = new Number(-123.4567000e+2) // '-12345.67'
  1428. y.toExponential() // '-1.234567e+4'
  1429. z = new BigNumber('-123.4567000e+2') // '-12345.67'
  1430. z.toExponential() // '-1.234567e+4'
  1431. z.c // '1,2,3,4,5,6,7'
  1432. z.e // 4
  1433. z.s // -1</pre>
  1434. <h4 id="zero-nan-infinity">Zero, NaN and Infinity</h4>
  1435. <p>
  1436. The table below shows how &plusmn;0, NaN and &plusmn;Infinity are stored.
  1437. </p>
  1438. <table>
  1439. <tr>
  1440. <th> </th>
  1441. <th class='centre'>c</th>
  1442. <th class='centre'>e</th>
  1443. <th class='centre'>s</th>
  1444. </tr>
  1445. <tr>
  1446. <td>&plusmn;0</td>
  1447. <td><code>[0]</code></td>
  1448. <td><code>0</code></td>
  1449. <td><code>&plusmn;1</code></td>
  1450. </tr>
  1451. <tr>
  1452. <td>NaN</td>
  1453. <td><code>null</code></td>
  1454. <td><code>null</code></td>
  1455. <td><code>null</code></td>
  1456. </tr>
  1457. <tr>
  1458. <td>&plusmn;Infinity</td>
  1459. <td><code>null</code></td>
  1460. <td><code>null</code></td>
  1461. <td><code>&plusmn;1</code></td>
  1462. </tr>
  1463. </table>
  1464. <pre>
  1465. x = new Number(-0) // 0
  1466. 1 / x == -Infinity // true
  1467. y = new BigNumber(-0) // '0'
  1468. y.c // '0' ( [0].toString() )
  1469. y.e // 0
  1470. y.s // -1</pre>
  1471. <h4 id='Errors'>Errors</h4>
  1472. <p>
  1473. The errors that are thrown are generic <code>Error</code> objects with
  1474. <code>name</code> <i>BigNumber Error</i>. The table below shows the errors
  1475. that may be thrown if <code>ERRORS</code> is <code>true</code>, and the
  1476. action taken if <code>ERRORS</code> is <code>false</code>.
  1477. </p>
  1478. <table class='error-table'>
  1479. <tr>
  1480. <th>Method(s)</th>
  1481. <th>ERRORS: true<br />Throw BigNumber Error</th>
  1482. <th>ERRORS: false<br />Action on invalid argument</th>
  1483. </tr>
  1484. <tr>
  1485. <td rowspan=5><code>
  1486. BigNumber<br />
  1487. comparedTo<br />
  1488. dividedBy<br />
  1489. dividedToIntegerBy<br />
  1490. equals<br />
  1491. greaterThan<br />
  1492. greaterThanOrEqualTo<br />
  1493. lessThan<br />
  1494. lessThanOrEqualTo<br />
  1495. minus<br />
  1496. mod<br />
  1497. plus<br />
  1498. times</code></td>
  1499. <td>number type has more than<br />15 significant digits</td>
  1500. <td>Accept.</td>
  1501. </tr>
  1502. <tr>
  1503. <td>not a base... number</td>
  1504. <td>Substitute <code>NaN</code>.</td>
  1505. </tr>
  1506. <tr>
  1507. <td>base not an integer</td>
  1508. <td>Truncate to integer.<br />Ignore if not a number.</td>
  1509. </tr>
  1510. <tr>
  1511. <td>base out of range</td>
  1512. <td>Ignore.</td>
  1513. </tr>
  1514. <tr>
  1515. <td>not a number<sup>*</sup></td>
  1516. <td>Substitute <code>NaN</code>.</td>
  1517. </tr>
  1518. <tr>
  1519. <td rowspan=10><code>config</code></td>
  1520. <td><code>DECIMAL_PLACES</code> not an integer</td>
  1521. <td>Truncate to integer.<br />Ignore if not a number.</td>
  1522. </tr>
  1523. <tr>
  1524. <td><code>DECIMAL_PLACES</code> out of range</td>
  1525. <td>Ignore.</td>
  1526. </tr>
  1527. <tr>
  1528. <td><code>ROUNDING_MODE</code> not an integer</td>
  1529. <td>Truncate to integer.<br />Ignore if not a number.</td>
  1530. </tr>
  1531. <tr>
  1532. <td><code>ROUNDING_MODE</code> out of range</td>
  1533. <td>Ignore.</td>
  1534. </tr>
  1535. <tr>
  1536. <td>
  1537. <code>EXPONENTIAL_AT</code> not an integer<br />
  1538. or not [integer, integer]
  1539. </td>
  1540. <td>Truncate to integer(s).<br />Ignore if not number(s).</td>
  1541. </tr>
  1542. <tr>
  1543. <td>
  1544. <code>EXPONENTIAL_AT</code> out of range<br />
  1545. or not [negative, positive]
  1546. </td>
  1547. <td>Ignore.</td>
  1548. </tr>
  1549. <tr>
  1550. <td>
  1551. <code>RANGE</code> not a non-zero integer<br />
  1552. or not [integer, integer]
  1553. </td>
  1554. <td> Truncate to integer(s).<br />Ignore if zero or not number(s).</td>
  1555. </tr>
  1556. <tr>
  1557. <td>
  1558. <code>RANGE</code> out of range<br />
  1559. or not [negative, positive]
  1560. </td>
  1561. <td>Ignore.</td>
  1562. </tr>
  1563. <tr>
  1564. <td>
  1565. <code>ERRORS</code> not a boolean<br />
  1566. or binary digit
  1567. </td>
  1568. <td>Ignore.</td>
  1569. </tr>
  1570. <tr>
  1571. <td>
  1572. <code>FORMAT</code> not an object
  1573. </td>
  1574. <td>Ignore.</td>
  1575. </tr>
  1576. <tr>
  1577. <td rowspan=2><code>toPower</code></td>
  1578. <td>exponent not an integer</td>
  1579. <td>
  1580. Truncate to integer.<br />Substitute <code>NaN</code> if not a number.
  1581. </td>
  1582. </tr>
  1583. <tr>
  1584. <td>exponent out of range</td>
  1585. <td>Substitute <code>&plusmn;Infinity</code>.
  1586. </td>
  1587. </tr>
  1588. <tr>
  1589. <td rowspan=4><code>round</code></td>
  1590. <td>decimal places not an integer</td>
  1591. <td>Truncate to integer.<br />Ignore if not a number.</td>
  1592. </tr>
  1593. <tr>
  1594. <td>decimal places out of range</td>
  1595. <td>Ignore.</td>
  1596. </tr>
  1597. <tr>
  1598. <td>mode not an integer</td>
  1599. <td>Truncate to integer.<br />Ignore if not a number.</td>
  1600. </tr>
  1601. <tr>
  1602. <td>mode out of range</td>
  1603. <td>Ignore.</td>
  1604. </tr>
  1605. <tr>
  1606. <td rowspan=2>
  1607. <code>toExponential</code><br />
  1608. <code>toFixed</code><br />
  1609. <code>toFormat</code>
  1610. </td>
  1611. <td>decimal places not an integer</td>
  1612. <td>Truncate to integer.<br />Ignore if not a number.</td>
  1613. </tr>
  1614. <tr>
  1615. <td>decimal places out of range</td>
  1616. <td>Ignore.</td>
  1617. </tr>
  1618. <tr>
  1619. <td rowspan=2><code>toFraction</code></td>
  1620. <td>max denominator not an integer</td>
  1621. <td>Truncate to integer.<br />Ignore if not a number.</td>
  1622. </tr>
  1623. <tr>
  1624. <td>max denominator out of range</td>
  1625. <td>Ignore.</td>
  1626. </tr>
  1627. <tr>
  1628. <td rowspan=2><code>toPrecision</code></td>
  1629. <td>precision not an integer</td>
  1630. <td>Truncate to integer.<br />Ignore if not a number.</td>
  1631. </tr>
  1632. <tr>
  1633. <td>precision out of range</td>
  1634. <td>Ignore.</td>
  1635. </tr>
  1636. <tr>
  1637. <td rowspan=2><code>toString</code></td>
  1638. <td>base not an integer</td>
  1639. <td>Truncate to integer.<br />Ignore if not a number.</td>
  1640. </tr>
  1641. <tr>
  1642. <td>base out of range</td>
  1643. <td>Ignore.</td>
  1644. </tr>
  1645. </table>
  1646. <p>
  1647. <sup>*</sup>No error is thrown if the value is <code>NaN</code> or 'NaN'
  1648. </p>
  1649. <p>
  1650. The message of a <i>BigNumber Error</i> will also contain the name of the
  1651. method from which the error originated.
  1652. </p>
  1653. <p>
  1654. To determine if an exception is a <i>BigNumber Error</i>:
  1655. </p>
  1656. <pre>
  1657. try {
  1658. // ...
  1659. } catch (e) {
  1660. if ( e instanceof Error && e.name == 'BigNumber Error' ) {
  1661. // ...
  1662. }
  1663. }</pre>
  1664. <h4 id='faq'>FAQ</h4>
  1665. <h6>Why are trailing fractional zeros removed from BigNumbers?</h6>
  1666. <p>
  1667. Many arbitrary-precision libraries retain trailing fractional zeros as
  1668. they can indicate the precision of a value. This can be useful but the
  1669. results of arithmetic operations can be misleading.
  1670. </p>
  1671. <pre>
  1672. x = new BigDecimal("1.0")
  1673. y = new BigDecimal("1.1000")
  1674. z = x.add(y) // 2.1000
  1675. x = new BigDecimal("1.20")
  1676. y = new BigDecimal("3.45000")
  1677. z = x.multiply(y) // 4.1400000</pre>
  1678. <p>
  1679. To specify the precision of a value is to specify that the value lies
  1680. within a certain range.
  1681. </p>
  1682. <p>
  1683. In the first example, <code>x</code> has a value of 1.0. The trailing zero
  1684. shows the precision of the value, implying that it is in the range 0.95 to
  1685. 1.05. Similarly, the precision indicated by the trailing zeros of
  1686. <code>y</code> indicates that the value is in the range 1.09995 to
  1687. 1.10005. If we add the two lowest values in the ranges we get 0.95 +
  1688. 1.09995 = 2.04995 and if we add the two highest values we get 1.05 +
  1689. 1.10005 = 2.15005, so the range of the result of the addition implied by
  1690. the precision of its operands is 2.04995 to 2.15005. The result given by
  1691. BigDecimal of 2.1000 however, indicates that the value is in the range
  1692. 2.09995 to 2.10005 and therefore the precision implied by its trailing
  1693. zeros is misleading.
  1694. </p>
  1695. <p>
  1696. In the second example, the true range is 4.122744 to 4.157256 yet the
  1697. BigDecimal answer of 4.1400000 indicates a range of 4.13999995 to
  1698. 4.14000005. Again, the precision implied by the trailing zeros is
  1699. misleading.
  1700. </p>
  1701. <p>
  1702. This library, like binary floating point and most calculators, does not
  1703. retain trailing fractional zeros. Instead, the <code>toExponential</code>,
  1704. <code>toFixed</code> and <code>toPrecision</code> methods enable trailing
  1705. zeros to be added if and when required.
  1706. </p>
  1707. <br />
  1708. </div>
  1709. </body>
  1710. </html>