You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="Author" content="M Mclaughlin">
  7. <title>bignumber.js API</title>
  8. <style>
  9. html{font-size:100%}
  10. body{background:#fff;font-family:"Helvetica Neue",Helvetica,Arial,sans-serif;font-size:13px;
  11. line-height:1.65em;min-height:100%;margin:0}
  12. body,i{color:#000}
  13. .nav{background:#fff;position:fixed;top:0;bottom:0;left:0;width:200px;overflow-y:auto;
  14. padding:15px 0 30px 15px}
  15. div.container{width:600px;margin:50px 0 50px 240px}
  16. p{margin:0 0 1em;width:600px}
  17. pre,ul{margin:1em 0}
  18. h1,h2,h3,h4,h5{margin:0;padding:1.5em 0 0}
  19. h1,h2{padding:.75em 0}
  20. h1{font:400 3em Verdana,sans-serif;color:#000;margin-bottom:1em}
  21. h2{font-size:2.25em;color:#ff2a00}
  22. h3{font-size:1.75em;color:#4dc71f}
  23. h4{font-size:1.75em;color:#ff2a00;padding-bottom:.75em}
  24. h5{font-size:1.2em;margin-bottom:.4em}
  25. h6{font-size:1.1em;margin-bottom:0.8em;padding:0.5em 0}
  26. dd{padding-top:.35em}
  27. dt{padding-top:.5em}
  28. b{font-weight:700}
  29. dt b{font-size:1.3em}
  30. a,a:visited{color:#ff2a00;text-decoration:none}
  31. a:active,a:hover{outline:0;text-decoration:underline}
  32. .nav a,.nav b,.nav a:visited{display:block;color:#ff2a00;font-weight:700; margin-top:15px}
  33. .nav b{color:#4dc71f;margin-top:20px;cursor:default;width:auto}
  34. ul{list-style-type:none;padding:0 0 0 20px}
  35. .nav ul{line-height:14px;padding-left:0;margin:5px 0 0}
  36. .nav ul a,.nav ul a:visited,span{display:inline;color:#000;font-family:Verdana,Geneva,sans-serif;
  37. font-size:11px;font-weight:400;margin:0}
  38. .inset,ul.inset{margin-left:20px}
  39. .inset{font-size:.9em}
  40. .nav li{width:auto;margin:0 0 3px}
  41. .alias{font-style:italic;margin-left:20px}
  42. table{border-collapse:collapse;border-spacing:0;border:2px solid #a7dbd8;margin:1.75em 0;padding:0}
  43. td,th{text-align:left;margin:0;padding:2px 5px;border:1px dotted #a7dbd8}
  44. th{border-top:2px solid #a7dbd8;border-bottom:2px solid #a7dbd8;color:#ff2a00}
  45. code,pre{font-family:Consolas, monaco, monospace;font-weight:400}
  46. pre{background:#f5f5f5;white-space:pre-wrap;word-wrap:break-word;border-left:5px solid #abef98;
  47. padding:1px 0 1px 15px;margin:1.2em 0}
  48. code,.nav-title{color:#ff2a00}
  49. .end{margin-bottom:25px}
  50. .centre{text-align:center}
  51. .error-table{font-size:13px;width:100%}
  52. #faq{margin:3em 0 0}
  53. li span{float:right;margin-right:10px;color:#c0c0c0}
  54. #js{font:inherit;color:#4dc71f}
  55. </style>
  56. </head>
  57. <body>
  58. <div class="nav">
  59. <b>v9.0.2</b>
  60. <a class='nav-title' href="#">API</a>
  61. <b> CONSTRUCTOR </b>
  62. <ul>
  63. <li><a href="#bignumber">BigNumber</a></li>
  64. </ul>
  65. <a href="#methods">Methods</a>
  66. <ul>
  67. <li><a href="#clone">clone</a></li>
  68. <li><a href="#config" >config</a><span>set</span></li>
  69. <li>
  70. <ul class="inset">
  71. <li><a href="#decimal-places">DECIMAL_PLACES</a></li>
  72. <li><a href="#rounding-mode" >ROUNDING_MODE</a></li>
  73. <li><a href="#exponential-at">EXPONENTIAL_AT</a></li>
  74. <li><a href="#range" >RANGE</a></li>
  75. <li><a href="#crypto" >CRYPTO</a></li>
  76. <li><a href="#modulo-mode" >MODULO_MODE</a></li>
  77. <li><a href="#pow-precision" >POW_PRECISION</a></li>
  78. <li><a href="#format" >FORMAT</a></li>
  79. <li><a href="#alphabet" >ALPHABET</a></li>
  80. </ul>
  81. </li>
  82. <li><a href="#isBigNumber">isBigNumber</a></li>
  83. <li><a href="#max" >maximum</a><span>max</span></li>
  84. <li><a href="#min" >minimum</a><span>min</span></li>
  85. <li><a href="#random" >random</a></li>
  86. <li><a href="#sum" >sum</a></li>
  87. </ul>
  88. <a href="#constructor-properties">Properties</a>
  89. <ul>
  90. <li><a href="#round-up" >ROUND_UP</a></li>
  91. <li><a href="#round-down" >ROUND_DOWN</a></li>
  92. <li><a href="#round-ceil" >ROUND_CEIL</a></li>
  93. <li><a href="#round-floor" >ROUND_FLOOR</a></li>
  94. <li><a href="#round-half-up" >ROUND_HALF_UP</a></li>
  95. <li><a href="#round-half-down" >ROUND_HALF_DOWN</a></li>
  96. <li><a href="#round-half-even" >ROUND_HALF_EVEN</a></li>
  97. <li><a href="#round-half-ceil" >ROUND_HALF_CEIL</a></li>
  98. <li><a href="#round-half-floor">ROUND_HALF_FLOOR</a></li>
  99. <li><a href="#debug" >DEBUG</a></li>
  100. </ul>
  101. <b> INSTANCE </b>
  102. <a href="#prototype-methods">Methods</a>
  103. <ul>
  104. <li><a href="#abs" >absoluteValue </a><span>abs</span> </li>
  105. <li><a href="#cmp" >comparedTo </a> </li>
  106. <li><a href="#dp" >decimalPlaces </a><span>dp</span> </li>
  107. <li><a href="#div" >dividedBy </a><span>div</span> </li>
  108. <li><a href="#divInt" >dividedToIntegerBy </a><span>idiv</span> </li>
  109. <li><a href="#pow" >exponentiatedBy </a><span>pow</span> </li>
  110. <li><a href="#int" >integerValue </a> </li>
  111. <li><a href="#eq" >isEqualTo </a><span>eq</span> </li>
  112. <li><a href="#isF" >isFinite </a> </li>
  113. <li><a href="#gt" >isGreaterThan </a><span>gt</span> </li>
  114. <li><a href="#gte" >isGreaterThanOrEqualTo</a><span>gte</span> </li>
  115. <li><a href="#isInt" >isInteger </a> </li>
  116. <li><a href="#lt" >isLessThan </a><span>lt</span> </li>
  117. <li><a href="#lte" >isLessThanOrEqualTo </a><span>lte</span> </li>
  118. <li><a href="#isNaN" >isNaN </a> </li>
  119. <li><a href="#isNeg" >isNegative </a> </li>
  120. <li><a href="#isPos" >isPositive </a> </li>
  121. <li><a href="#isZ" >isZero </a> </li>
  122. <li><a href="#minus" >minus </a> </li>
  123. <li><a href="#mod" >modulo </a><span>mod</span> </li>
  124. <li><a href="#times" >multipliedBy </a><span>times</span></li>
  125. <li><a href="#neg" >negated </a> </li>
  126. <li><a href="#plus" >plus </a> </li>
  127. <li><a href="#sd" >precision </a><span>sd</span> </li>
  128. <li><a href="#shift" >shiftedBy </a> </li>
  129. <li><a href="#sqrt" >squareRoot </a><span>sqrt</span> </li>
  130. <li><a href="#toE" >toExponential </a> </li>
  131. <li><a href="#toFix" >toFixed </a> </li>
  132. <li><a href="#toFor" >toFormat </a> </li>
  133. <li><a href="#toFr" >toFraction </a> </li>
  134. <li><a href="#toJSON" >toJSON </a> </li>
  135. <li><a href="#toN" >toNumber </a> </li>
  136. <li><a href="#toP" >toPrecision </a> </li>
  137. <li><a href="#toS" >toString </a> </li>
  138. <li><a href="#valueOf">valueOf </a> </li>
  139. </ul>
  140. <a href="#instance-properties">Properties</a>
  141. <ul>
  142. <li><a href="#coefficient">c: coefficient</a></li>
  143. <li><a href="#exponent" >e: exponent</a></li>
  144. <li><a href="#sign" >s: sign</a></li>
  145. </ul>
  146. <a href="#zero-nan-infinity">Zero, NaN &amp; Infinity</a>
  147. <a href="#Errors">Errors</a>
  148. <a href="#type-coercion">Type coercion</a>
  149. <a class='end' href="#faq">FAQ</a>
  150. </div>
  151. <div class="container">
  152. <h1>bignumber<span id='js'>.js</span></h1>
  153. <p>A JavaScript library for arbitrary-precision arithmetic.</p>
  154. <p><a href="https://github.com/MikeMcl/bignumber.js">Hosted on GitHub</a>. </p>
  155. <h2>API</h2>
  156. <p>
  157. See the <a href='https://github.com/MikeMcl/bignumber.js'>README</a> on GitHub for a
  158. quick-start introduction.
  159. </p>
  160. <p>
  161. In all examples below, <code>var</code> and semicolons are not shown, and if a commented-out
  162. value is in quotes it means <code>toString</code> has been called on the preceding expression.
  163. </p>
  164. <h3>CONSTRUCTOR</h3>
  165. <h5 id="bignumber">
  166. BigNumber<code class='inset'>BigNumber(n [, base]) <i>&rArr; BigNumber</i></code>
  167. </h5>
  168. <p>
  169. <code>n</code>: <i>number|string|BigNumber</i><br />
  170. <code>base</code>: <i>number</i>: integer, <code>2</code> to <code>36</code> inclusive. (See
  171. <a href='#alphabet'><code>ALPHABET</code></a> to extend this range).
  172. </p>
  173. <p>
  174. Returns a new instance of a BigNumber object with value <code>n</code>, where <code>n</code>
  175. is a numeric value in the specified <code>base</code>, or base <code>10</code> if
  176. <code>base</code> is omitted or is <code>null</code> or <code>undefined</code>.
  177. </p>
  178. <p>
  179. Note that the BigNnumber constructor accepts an <code>n</code> of type <em>number</em> purely
  180. as a convenience so that string quotes don't have to be typed when entering literal values,
  181. and that it is the <code>toString</code> value of <code>n</code> that is used rather than its
  182. underlying binary floating point value converted to decimal.
  183. </p>
  184. <pre>
  185. x = new BigNumber(123.4567) // '123.4567'
  186. // 'new' is optional
  187. y = BigNumber(x) // '123.4567'</pre>
  188. <p>
  189. If <code>n</code> is a base <code>10</code> value it can be in normal or exponential notation.
  190. Values in other bases must be in normal notation. Values in any base can have fraction digits,
  191. i.e. digits after the decimal point.
  192. </p>
  193. <pre>
  194. new BigNumber(43210) // '43210'
  195. new BigNumber('4.321e+4') // '43210'
  196. new BigNumber('-735.0918e-430') // '-7.350918e-428'
  197. new BigNumber('123412421.234324', 5) // '607236.557696'</pre>
  198. <p>
  199. Signed <code>0</code>, signed <code>Infinity</code> and <code>NaN</code> are supported.
  200. </p>
  201. <pre>
  202. new BigNumber('-Infinity') // '-Infinity'
  203. new BigNumber(NaN) // 'NaN'
  204. new BigNumber(-0) // '0'
  205. new BigNumber('.5') // '0.5'
  206. new BigNumber('+2') // '2'</pre>
  207. <p>
  208. String values in hexadecimal literal form, e.g. <code>'0xff'</code> or <code>'0xFF'</code>
  209. (but not <code>'0xfF'</code>), are valid, as are string values with the octal and binary
  210. prefixs <code>'0o'</code> and <code>'0b'</code>. String values in octal literal form without
  211. the prefix will be interpreted as decimals, e.g. <code>'011'</code> is interpreted as 11, not 9.
  212. </p>
  213. <pre>
  214. new BigNumber(-10110100.1, 2) // '-180.5'
  215. new BigNumber('-0b10110100.1') // '-180.5'
  216. new BigNumber('ff.8', 16) // '255.5'
  217. new BigNumber('0xff.8') // '255.5'</pre>
  218. <p>
  219. If a base is specified, <code>n</code> is rounded according to the current
  220. <a href='#decimal-places'><code>DECIMAL_PLACES</code></a> and
  221. <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> settings. <em>This includes base
  222. <code>10</code> so don't include a <code>base</code> parameter for decimal values unless
  223. this behaviour is wanted.</em>
  224. </p>
  225. <pre>BigNumber.config({ DECIMAL_PLACES: 5 })
  226. new BigNumber(1.23456789) // '1.23456789'
  227. new BigNumber(1.23456789, 10) // '1.23457'</pre>
  228. <p>An error is thrown if <code>base</code> is invalid. See <a href='#Errors'>Errors</a>.</p>
  229. <p>
  230. There is no limit to the number of digits of a value of type <em>string</em> (other than
  231. that of JavaScript's maximum array size). See <a href='#range'><code>RANGE</code></a> to set
  232. the maximum and minimum possible exponent value of a BigNumber.
  233. </p>
  234. <pre>
  235. new BigNumber('5032485723458348569331745.33434346346912144534543')
  236. new BigNumber('4.321e10000000')</pre>
  237. <p>BigNumber <code>NaN</code> is returned if <code>n</code> is invalid
  238. (unless <code>BigNumber.DEBUG</code> is <code>true</code>, see below).</p>
  239. <pre>
  240. new BigNumber('.1*') // 'NaN'
  241. new BigNumber('blurgh') // 'NaN'
  242. new BigNumber(9, 2) // 'NaN'</pre>
  243. <p>
  244. To aid in debugging, if <code>BigNumber.DEBUG</code> is <code>true</code> then an error will
  245. be thrown on an invalid <code>n</code>. An error will also be thrown if <code>n</code> is of
  246. type <em>number</em> and has more than <code>15</code> significant digits, as calling
  247. <code><a href='#toS'>toString</a></code> or <code><a href='#valueOf'>valueOf</a></code> on
  248. these numbers may not result in the intended value.
  249. </p>
  250. <pre>
  251. console.log(823456789123456.3) // 823456789123456.2
  252. new BigNumber(823456789123456.3) // '823456789123456.2'
  253. BigNumber.DEBUG = true
  254. // '[BigNumber Error] Number primitive has more than 15 significant digits'
  255. new BigNumber(823456789123456.3)
  256. // '[BigNumber Error] Not a base 2 number'
  257. new BigNumber(9, 2)</pre>
  258. <p>
  259. A BigNumber can also be created from an object literal.
  260. Use <code><a href='#isBigNumber'>isBigNumber</a></code> to check that it is well-formed.
  261. </p>
  262. <pre>new BigNumber({ s: 1, e: 2, c: [ 777, 12300000000000 ], _isBigNumber: true }) // '777.123'</pre>
  263. <h4 id="methods">Methods</h4>
  264. <p>The static methods of a BigNumber constructor.</p>
  265. <h5 id="clone">clone
  266. <code class='inset'>.clone([object]) <i>&rArr; BigNumber constructor</i></code>
  267. </h5>
  268. <p><code>object</code>: <i>object</i></p>
  269. <p>
  270. Returns a new independent BigNumber constructor with configuration as described by
  271. <code>object</code> (see <a href='#config'><code>config</code></a>), or with the default
  272. configuration if <code>object</code> is <code>null</code> or <code>undefined</code>.
  273. </p>
  274. <p>
  275. Throws if <code>object</code> is not an object. See <a href='#Errors'>Errors</a>.
  276. </p>
  277. <pre>BigNumber.config({ DECIMAL_PLACES: 5 })
  278. BN = BigNumber.clone({ DECIMAL_PLACES: 9 })
  279. x = new BigNumber(1)
  280. y = new BN(1)
  281. x.div(3) // 0.33333
  282. y.div(3) // 0.333333333
  283. // BN = BigNumber.clone({ DECIMAL_PLACES: 9 }) is equivalent to:
  284. BN = BigNumber.clone()
  285. BN.config({ DECIMAL_PLACES: 9 })</pre>
  286. <h5 id="config">config<code class='inset'>set([object]) <i>&rArr; object</i></code></h5>
  287. <p>
  288. <code>object</code>: <i>object</i>: an object that contains some or all of the following
  289. properties.
  290. </p>
  291. <p>Configures the settings for this particular BigNumber constructor.</p>
  292. <dl class='inset'>
  293. <dt id="decimal-places"><code><b>DECIMAL_PLACES</b></code></dt>
  294. <dd>
  295. <i>number</i>: integer, <code>0</code> to <code>1e+9</code> inclusive<br />
  296. Default value: <code>20</code>
  297. </dd>
  298. <dd>
  299. The <em>maximum</em> number of decimal places of the results of operations involving
  300. division, i.e. division, square root and base conversion operations, and power operations
  301. with negative exponents.<br />
  302. </dd>
  303. <dd>
  304. <pre>BigNumber.config({ DECIMAL_PLACES: 5 })
  305. BigNumber.set({ DECIMAL_PLACES: 5 }) // equivalent</pre>
  306. </dd>
  307. <dt id="rounding-mode"><code><b>ROUNDING_MODE</b></code></dt>
  308. <dd>
  309. <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive<br />
  310. Default value: <code>4</code> <a href="#round-half-up">(<code>ROUND_HALF_UP</code>)</a>
  311. </dd>
  312. <dd>
  313. The rounding mode used in the above operations and the default rounding mode of
  314. <a href='#dp'><code>decimalPlaces</code></a>,
  315. <a href='#sd'><code>precision</code></a>,
  316. <a href='#toE'><code>toExponential</code></a>,
  317. <a href='#toFix'><code>toFixed</code></a>,
  318. <a href='#toFor'><code>toFormat</code></a> and
  319. <a href='#toP'><code>toPrecision</code></a>.
  320. </dd>
  321. <dd>The modes are available as enumerated properties of the BigNumber constructor.</dd>
  322. <dd>
  323. <pre>BigNumber.config({ ROUNDING_MODE: 0 })
  324. BigNumber.set({ ROUNDING_MODE: BigNumber.ROUND_UP }) // equivalent</pre>
  325. </dd>
  326. <dt id="exponential-at"><code><b>EXPONENTIAL_AT</b></code></dt>
  327. <dd>
  328. <i>number</i>: integer, magnitude <code>0</code> to <code>1e+9</code> inclusive, or
  329. <br />
  330. <i>number</i>[]: [ integer <code>-1e+9</code> to <code>0</code> inclusive, integer
  331. <code>0</code> to <code>1e+9</code> inclusive ]<br />
  332. Default value: <code>[-7, 20]</code>
  333. </dd>
  334. <dd>
  335. The exponent value(s) at which <code>toString</code> returns exponential notation.
  336. </dd>
  337. <dd>
  338. If a single number is assigned, the value is the exponent magnitude.<br />
  339. If an array of two numbers is assigned then the first number is the negative exponent
  340. value at and beneath which exponential notation is used, and the second number is the
  341. positive exponent value at and above which the same.
  342. </dd>
  343. <dd>
  344. For example, to emulate JavaScript numbers in terms of the exponent values at which they
  345. begin to use exponential notation, use <code>[-7, 20]</code>.
  346. </dd>
  347. <dd>
  348. <pre>BigNumber.config({ EXPONENTIAL_AT: 2 })
  349. new BigNumber(12.3) // '12.3' e is only 1
  350. new BigNumber(123) // '1.23e+2'
  351. new BigNumber(0.123) // '0.123' e is only -1
  352. new BigNumber(0.0123) // '1.23e-2'
  353. BigNumber.config({ EXPONENTIAL_AT: [-7, 20] })
  354. new BigNumber(123456789) // '123456789' e is only 8
  355. new BigNumber(0.000000123) // '1.23e-7'
  356. // Almost never return exponential notation:
  357. BigNumber.config({ EXPONENTIAL_AT: 1e+9 })
  358. // Always return exponential notation:
  359. BigNumber.config({ EXPONENTIAL_AT: 0 })</pre>
  360. </dd>
  361. <dd>
  362. Regardless of the value of <code>EXPONENTIAL_AT</code>, the <code>toFixed</code> method
  363. will always return a value in normal notation and the <code>toExponential</code> method
  364. will always return a value in exponential form.
  365. </dd>
  366. <dd>
  367. Calling <code>toString</code> with a base argument, e.g. <code>toString(10)</code>, will
  368. also always return normal notation.
  369. </dd>
  370. <dt id="range"><code><b>RANGE</b></code></dt>
  371. <dd>
  372. <i>number</i>: integer, magnitude <code>1</code> to <code>1e+9</code> inclusive, or
  373. <br />
  374. <i>number</i>[]: [ integer <code>-1e+9</code> to <code>-1</code> inclusive, integer
  375. <code>1</code> to <code>1e+9</code> inclusive ]<br />
  376. Default value: <code>[-1e+9, 1e+9]</code>
  377. </dd>
  378. <dd>
  379. The exponent value(s) beyond which overflow to <code>Infinity</code> and underflow to
  380. zero occurs.
  381. </dd>
  382. <dd>
  383. If a single number is assigned, it is the maximum exponent magnitude: values wth a
  384. positive exponent of greater magnitude become <code>Infinity</code> and those with a
  385. negative exponent of greater magnitude become zero.
  386. <dd>
  387. If an array of two numbers is assigned then the first number is the negative exponent
  388. limit and the second number is the positive exponent limit.
  389. </dd>
  390. <dd>
  391. For example, to emulate JavaScript numbers in terms of the exponent values at which they
  392. become zero and <code>Infinity</code>, use <code>[-324, 308]</code>.
  393. </dd>
  394. <dd>
  395. <pre>BigNumber.config({ RANGE: 500 })
  396. BigNumber.config().RANGE // [ -500, 500 ]
  397. new BigNumber('9.999e499') // '9.999e+499'
  398. new BigNumber('1e500') // 'Infinity'
  399. new BigNumber('1e-499') // '1e-499'
  400. new BigNumber('1e-500') // '0'
  401. BigNumber.config({ RANGE: [-3, 4] })
  402. new BigNumber(99999) // '99999' e is only 4
  403. new BigNumber(100000) // 'Infinity' e is 5
  404. new BigNumber(0.001) // '0.01' e is only -3
  405. new BigNumber(0.0001) // '0' e is -4</pre>
  406. </dd>
  407. <dd>
  408. The largest possible magnitude of a finite BigNumber is
  409. <code>9.999...e+1000000000</code>.<br />
  410. The smallest possible magnitude of a non-zero BigNumber is <code>1e-1000000000</code>.
  411. </dd>
  412. <dt id="crypto"><code><b>CRYPTO</b></code></dt>
  413. <dd>
  414. <i>boolean</i>: <code>true</code> or <code>false</code>.<br />
  415. Default value: <code>false</code>
  416. </dd>
  417. <dd>
  418. The value that determines whether cryptographically-secure pseudo-random number
  419. generation is used.
  420. </dd>
  421. <dd>
  422. If <code>CRYPTO</code> is set to <code>true</code> then the
  423. <a href='#random'><code>random</code></a> method will generate random digits using
  424. <code>crypto.getRandomValues</code> in browsers that support it, or
  425. <code>crypto.randomBytes</code> if using Node.js.
  426. </dd>
  427. <dd>
  428. If neither function is supported by the host environment then attempting to set
  429. <code>CRYPTO</code> to <code>true</code> will fail and an exception will be thrown.
  430. </dd>
  431. <dd>
  432. If <code>CRYPTO</code> is <code>false</code> then the source of randomness used will be
  433. <code>Math.random</code> (which is assumed to generate at least <code>30</code> bits of
  434. randomness).
  435. </dd>
  436. <dd>See <a href='#random'><code>random</code></a>.</dd>
  437. <dd>
  438. <pre>
  439. // Node.js
  440. const crypto = require('crypto'); // CommonJS
  441. import * as crypto from 'crypto'; // ES module
  442. global.crypto = crypto;
  443. BigNumber.config({ CRYPTO: true })
  444. BigNumber.config().CRYPTO // true
  445. BigNumber.random() // 0.54340758610486147524</pre>
  446. </dd>
  447. <dt id="modulo-mode"><code><b>MODULO_MODE</b></code></dt>
  448. <dd>
  449. <i>number</i>: integer, <code>0</code> to <code>9</code> inclusive<br />
  450. Default value: <code>1</code> (<a href="#round-down"><code>ROUND_DOWN</code></a>)
  451. </dd>
  452. <dd>The modulo mode used when calculating the modulus: <code>a mod n</code>.</dd>
  453. <dd>
  454. The quotient, <code>q = a / n</code>, is calculated according to the
  455. <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> that corresponds to the chosen
  456. <code>MODULO_MODE</code>.
  457. </dd>
  458. <dd>The remainder, <code>r</code>, is calculated as: <code>r = a - n * q</code>.</dd>
  459. <dd>
  460. The modes that are most commonly used for the modulus/remainder operation are shown in
  461. the following table. Although the other rounding modes can be used, they may not give
  462. useful results.
  463. </dd>
  464. <dd>
  465. <table>
  466. <tr><th>Property</th><th>Value</th><th>Description</th></tr>
  467. <tr>
  468. <td><b>ROUND_UP</b></td><td class='centre'>0</td>
  469. <td>
  470. The remainder is positive if the dividend is negative, otherwise it is negative.
  471. </td>
  472. </tr>
  473. <tr>
  474. <td><b>ROUND_DOWN</b></td><td class='centre'>1</td>
  475. <td>
  476. The remainder has the same sign as the dividend.<br />
  477. This uses 'truncating division' and matches the behaviour of JavaScript's
  478. remainder operator <code>%</code>.
  479. </td>
  480. </tr>
  481. <tr>
  482. <td><b>ROUND_FLOOR</b></td><td class='centre'>3</td>
  483. <td>
  484. The remainder has the same sign as the divisor.<br />
  485. This matches Python's <code>%</code> operator.
  486. </td>
  487. </tr>
  488. <tr>
  489. <td><b>ROUND_HALF_EVEN</b></td><td class='centre'>6</td>
  490. <td>The <i>IEEE 754</i> remainder function.</td>
  491. </tr>
  492. <tr>
  493. <td><b>EUCLID</b></td><td class='centre'>9</td>
  494. <td>
  495. The remainder is always positive. Euclidian division: <br />
  496. <code>q = sign(n) * floor(a / abs(n))</code>
  497. </td>
  498. </tr>
  499. </table>
  500. </dd>
  501. <dd>
  502. The rounding/modulo modes are available as enumerated properties of the BigNumber
  503. constructor.
  504. </dd>
  505. <dd>See <a href='#mod'><code>modulo</code></a>.</dd>
  506. <dd>
  507. <pre>BigNumber.config({ MODULO_MODE: BigNumber.EUCLID })
  508. BigNumber.config({ MODULO_MODE: 9 }) // equivalent</pre>
  509. </dd>
  510. <dt id="pow-precision"><code><b>POW_PRECISION</b></code></dt>
  511. <dd>
  512. <i>number</i>: integer, <code>0</code> to <code>1e+9</code> inclusive.<br />
  513. Default value: <code>0</code>
  514. </dd>
  515. <dd>
  516. The <i>maximum</i> precision, i.e. number of significant digits, of the result of the power
  517. operation (unless a modulus is specified).
  518. </dd>
  519. <dd>If set to <code>0</code>, the number of significant digits will not be limited.</dd>
  520. <dd>See <a href='#pow'><code>exponentiatedBy</code></a>.</dd>
  521. <dd><pre>BigNumber.config({ POW_PRECISION: 100 })</pre></dd>
  522. <dt id="format"><code><b>FORMAT</b></code></dt>
  523. <dd><i>object</i></dd>
  524. <dd>
  525. The <code>FORMAT</code> object configures the format of the string returned by the
  526. <a href='#toFor'><code>toFormat</code></a> method.
  527. </dd>
  528. <dd>
  529. The example below shows the properties of the <code>FORMAT</code> object that are
  530. recognised, and their default values.
  531. </dd>
  532. <dd>
  533. Unlike the other configuration properties, the values of the properties of the
  534. <code>FORMAT</code> object will not be checked for validity. The existing
  535. <code>FORMAT</code> object will simply be replaced by the object that is passed in.
  536. The object can include any number of the properties shown below.
  537. </dd>
  538. <dd>See <a href='#toFor'><code>toFormat</code></a> for examples of usage.</dd>
  539. <dd>
  540. <pre>
  541. BigNumber.config({
  542. FORMAT: {
  543. // string to prepend
  544. prefix: '',
  545. // decimal separator
  546. decimalSeparator: '.',
  547. // grouping separator of the integer part
  548. groupSeparator: ',',
  549. // primary grouping size of the integer part
  550. groupSize: 3,
  551. // secondary grouping size of the integer part
  552. secondaryGroupSize: 0,
  553. // grouping separator of the fraction part
  554. fractionGroupSeparator: ' ',
  555. // grouping size of the fraction part
  556. fractionGroupSize: 0,
  557. // string to append
  558. suffix: ''
  559. }
  560. });</pre>
  561. </dd>
  562. <dt id="alphabet"><code><b>ALPHABET</b></code></dt>
  563. <dd>
  564. <i>string</i><br />
  565. Default value: <code>'0123456789abcdefghijklmnopqrstuvwxyz'</code>
  566. </dd>
  567. <dd>
  568. The alphabet used for base conversion. The length of the alphabet corresponds to the
  569. maximum value of the base argument that can be passed to the
  570. <a href='#bignumber'><code>BigNumber</code></a> constructor or
  571. <a href='#toS'><code>toString</code></a>.
  572. </dd>
  573. <dd>
  574. There is no maximum length for the alphabet, but it must be at least 2 characters long, and
  575. it must not contain whitespace or a repeated character, or the sign indicators
  576. <code>'+'</code> and <code>'-'</code>, or the decimal separator <code>'.'</code>.
  577. </dd>
  578. <dd>
  579. <pre>// duodecimal (base 12)
  580. BigNumber.config({ ALPHABET: '0123456789TE' })
  581. x = new BigNumber('T', 12)
  582. x.toString() // '10'
  583. x.toString(12) // 'T'</pre>
  584. </dd>
  585. </dl>
  586. <br /><br />
  587. <p>Returns an object with the above properties and their current values.</p>
  588. <p>
  589. Throws if <code>object</code> is not an object, or if an invalid value is assigned to
  590. one or more of the above properties. See <a href='#Errors'>Errors</a>.
  591. </p>
  592. <pre>
  593. BigNumber.config({
  594. DECIMAL_PLACES: 40,
  595. ROUNDING_MODE: BigNumber.ROUND_HALF_CEIL,
  596. EXPONENTIAL_AT: [-10, 20],
  597. RANGE: [-500, 500],
  598. CRYPTO: true,
  599. MODULO_MODE: BigNumber.ROUND_FLOOR,
  600. POW_PRECISION: 80,
  601. FORMAT: {
  602. groupSize: 3,
  603. groupSeparator: ' ',
  604. decimalSeparator: ','
  605. },
  606. ALPHABET: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_'
  607. });
  608. obj = BigNumber.config();
  609. obj.DECIMAL_PLACES // 40
  610. obj.RANGE // [-500, 500]</pre>
  611. <h5 id="isBigNumber">
  612. isBigNumber<code class='inset'>.isBigNumber(value) <i>&rArr; boolean</i></code>
  613. </h5>
  614. <p><code>value</code>: <i>any</i><br /></p>
  615. <p>
  616. Returns <code>true</code> if <code>value</code> is a BigNumber instance, otherwise returns
  617. <code>false</code>.
  618. </p>
  619. <pre>x = 42
  620. y = new BigNumber(x)
  621. BigNumber.isBigNumber(x) // false
  622. y instanceof BigNumber // true
  623. BigNumber.isBigNumber(y) // true
  624. BN = BigNumber.clone();
  625. z = new BN(x)
  626. z instanceof BigNumber // false
  627. BigNumber.isBigNumber(z) // true</pre>
  628. <p>
  629. If <code>value</code> is a BigNumber instance and <code>BigNumber.DEBUG</code> is <code>true</code>,
  630. then this method will also check if <code>value</code> is well-formed, and throw if it is not.
  631. See <a href='#Errors'>Errors</a>.
  632. </p>
  633. <p>
  634. The check can be useful if creating a BigNumber from an object literal.
  635. See <a href='#bignumber'>BigNumber</a>.
  636. </p>
  637. <pre>
  638. x = new BigNumber(10)
  639. // Change x.c to an illegitimate value.
  640. x.c = NaN
  641. BigNumber.DEBUG = false
  642. // No error.
  643. BigNumber.isBigNumber(x) // true
  644. BigNumber.DEBUG = true
  645. // Error.
  646. BigNumber.isBigNumber(x) // '[BigNumber Error] Invalid BigNumber'</pre>
  647. <h5 id="max">maximum<code class='inset'>.max(n...) <i>&rArr; BigNumber</i></code></h5>
  648. <p>
  649. <code>n</code>: <i>number|string|BigNumber</i><br />
  650. <i>See <code><a href="#bignumber">BigNumber</a></code> for further parameter details.</i>
  651. </p>
  652. <p>
  653. Returns a BigNumber whose value is the maximum of the arguments.
  654. </p>
  655. <p>The return value is always exact and unrounded.</p>
  656. <pre>x = new BigNumber('3257869345.0378653')
  657. BigNumber.maximum(4e9, x, '123456789.9') // '4000000000'
  658. arr = [12, '13', new BigNumber(14)]
  659. BigNumber.max.apply(null, arr) // '14'</pre>
  660. <h5 id="min">minimum<code class='inset'>.min(n...) <i>&rArr; BigNumber</i></code></h5>
  661. <p>
  662. <code>n</code>: <i>number|string|BigNumber</i><br />
  663. <i>See <code><a href="#bignumber">BigNumber</a></code> for further parameter details.</i>
  664. </p>
  665. <p>
  666. Returns a BigNumber whose value is the minimum of the arguments.
  667. </p>
  668. <p>The return value is always exact and unrounded.</p>
  669. <pre>x = new BigNumber('3257869345.0378653')
  670. BigNumber.minimum(4e9, x, '123456789.9') // '123456789.9'
  671. arr = [2, new BigNumber(-14), '-15.9999', -12]
  672. BigNumber.min.apply(null, arr) // '-15.9999'</pre>
  673. <h5 id="random">
  674. random<code class='inset'>.random([dp]) <i>&rArr; BigNumber</i></code>
  675. </h5>
  676. <p><code>dp</code>: <i>number</i>: integer, <code>0</code> to <code>1e+9</code> inclusive</p>
  677. <p>
  678. Returns a new BigNumber with a pseudo-random value equal to or greater than <code>0</code> and
  679. less than <code>1</code>.
  680. </p>
  681. <p>
  682. The return value will have <code>dp</code> decimal places (or less if trailing zeros are
  683. produced).<br />
  684. If <code>dp</code> is omitted then the number of decimal places will default to the current
  685. <a href='#decimal-places'><code>DECIMAL_PLACES</code></a> setting.
  686. </p>
  687. <p>
  688. Depending on the value of this BigNumber constructor's
  689. <a href='#crypto'><code>CRYPTO</code></a> setting and the support for the
  690. <code>crypto</code> object in the host environment, the random digits of the return value are
  691. generated by either <code>Math.random</code> (fastest), <code>crypto.getRandomValues</code>
  692. (Web Cryptography API in recent browsers) or <code>crypto.randomBytes</code> (Node.js).
  693. </p>
  694. <p>
  695. To be able to set <a href='#crypto'><code>CRYPTO</code></a> to <code>true</code> when using
  696. Node.js, the <code>crypto</code> object must be available globally:
  697. </p>
  698. <pre>// Node.js
  699. const crypto = require('crypto'); // CommonJS
  700. import * as crypto from 'crypto'; // ES module
  701. global.crypto = crypto;</pre>
  702. <p>
  703. If <a href='#crypto'><code>CRYPTO</code></a> is <code>true</code>, i.e. one of the
  704. <code>crypto</code> methods is to be used, the value of a returned BigNumber should be
  705. cryptographically-secure and statistically indistinguishable from a random value.
  706. </p>
  707. <p>
  708. Throws if <code>dp</code> is invalid. See <a href='#Errors'>Errors</a>.
  709. </p>
  710. <pre>BigNumber.config({ DECIMAL_PLACES: 10 })
  711. BigNumber.random() // '0.4117936847'
  712. BigNumber.random(20) // '0.78193327636914089009'</pre>
  713. <h5 id="sum">sum<code class='inset'>.sum(n...) <i>&rArr; BigNumber</i></code></h5>
  714. <p>
  715. <code>n</code>: <i>number|string|BigNumber</i><br />
  716. <i>See <code><a href="#bignumber">BigNumber</a></code> for further parameter details.</i>
  717. </p>
  718. <p>Returns a BigNumber whose value is the sum of the arguments.</p>
  719. <p>The return value is always exact and unrounded.</p>
  720. <pre>x = new BigNumber('3257869345.0378653')
  721. BigNumber.sum(4e9, x, '123456789.9') // '7381326134.9378653'
  722. arr = [2, new BigNumber(14), '15.9999', 12]
  723. BigNumber.sum.apply(null, arr) // '43.9999'</pre>
  724. <h4 id="constructor-properties">Properties</h4>
  725. <p>
  726. The library's enumerated rounding modes are stored as properties of the constructor.<br />
  727. (They are not referenced internally by the library itself.)
  728. </p>
  729. <p>
  730. Rounding modes <code>0</code> to <code>6</code> (inclusive) are the same as those of Java's
  731. BigDecimal class.
  732. </p>
  733. <table>
  734. <tr>
  735. <th>Property</th>
  736. <th>Value</th>
  737. <th>Description</th>
  738. </tr>
  739. <tr>
  740. <td id="round-up"><b>ROUND_UP</b></td>
  741. <td class='centre'>0</td>
  742. <td>Rounds away from zero</td>
  743. </tr>
  744. <tr>
  745. <td id="round-down"><b>ROUND_DOWN</b></td>
  746. <td class='centre'>1</td>
  747. <td>Rounds towards zero</td>
  748. </tr>
  749. <tr>
  750. <td id="round-ceil"><b>ROUND_CEIL</b></td>
  751. <td class='centre'>2</td>
  752. <td>Rounds towards <code>Infinity</code></td>
  753. </tr>
  754. <tr>
  755. <td id="round-floor"><b>ROUND_FLOOR</b></td>
  756. <td class='centre'>3</td>
  757. <td>Rounds towards <code>-Infinity</code></td>
  758. </tr>
  759. <tr>
  760. <td id="round-half-up"><b>ROUND_HALF_UP</b></td>
  761. <td class='centre'>4</td>
  762. <td>
  763. Rounds towards nearest neighbour.<br />
  764. If equidistant, rounds away from zero
  765. </td>
  766. </tr>
  767. <tr>
  768. <td id="round-half-down"><b>ROUND_HALF_DOWN</b></td>
  769. <td class='centre'>5</td>
  770. <td>
  771. Rounds towards nearest neighbour.<br />
  772. If equidistant, rounds towards zero
  773. </td>
  774. </tr>
  775. <tr>
  776. <td id="round-half-even"><b>ROUND_HALF_EVEN</b></td>
  777. <td class='centre'>6</td>
  778. <td>
  779. Rounds towards nearest neighbour.<br />
  780. If equidistant, rounds towards even neighbour
  781. </td>
  782. </tr>
  783. <tr>
  784. <td id="round-half-ceil"><b>ROUND_HALF_CEIL</b></td>
  785. <td class='centre'>7</td>
  786. <td>
  787. Rounds towards nearest neighbour.<br />
  788. If equidistant, rounds towards <code>Infinity</code>
  789. </td>
  790. </tr>
  791. <tr>
  792. <td id="round-half-floor"><b>ROUND_HALF_FLOOR</b></td>
  793. <td class='centre'>8</td>
  794. <td>
  795. Rounds towards nearest neighbour.<br />
  796. If equidistant, rounds towards <code>-Infinity</code>
  797. </td>
  798. </tr>
  799. </table>
  800. <pre>
  801. BigNumber.config({ ROUNDING_MODE: BigNumber.ROUND_CEIL })
  802. BigNumber.config({ ROUNDING_MODE: 2 }) // equivalent</pre>
  803. <h5 id="debug">DEBUG</h5>
  804. <p><i>undefined|false|true</i></p>
  805. <p>
  806. If <code>BigNumber.DEBUG</code> is set <code>true</code> then an error will be thrown
  807. if this <a href='#bignumber'>BigNumber</a> constructor receives an invalid value, such as
  808. a value of type <em>number</em> with more than <code>15</code> significant digits.
  809. See <a href='#bignumber'>BigNumber</a>.
  810. </p>
  811. <p>
  812. An error will also be thrown if the <code><a href='#isBigNumber'>isBigNumber</a></code>
  813. method receives a BigNumber that is not well-formed.
  814. See <code><a href='#isBigNumber'>isBigNumber</a></code>.
  815. </p>
  816. <pre>BigNumber.DEBUG = true</pre>
  817. <h3>INSTANCE</h3>
  818. <h4 id="prototype-methods">Methods</h4>
  819. <p>The methods inherited by a BigNumber instance from its constructor's prototype object.</p>
  820. <p>A BigNumber is immutable in the sense that it is not changed by its methods. </p>
  821. <p>
  822. The treatment of &plusmn;<code>0</code>, &plusmn;<code>Infinity</code> and <code>NaN</code> is
  823. consistent with how JavaScript treats these values.
  824. </p>
  825. <p>Many method names have a shorter alias.</p>
  826. <h5 id="abs">absoluteValue<code class='inset'>.abs() <i>&rArr; BigNumber</i></code></h5>
  827. <p>
  828. Returns a BigNumber whose value is the absolute value, i.e. the magnitude, of the value of
  829. this BigNumber.
  830. </p>
  831. <p>The return value is always exact and unrounded.</p>
  832. <pre>
  833. x = new BigNumber(-0.8)
  834. y = x.absoluteValue() // '0.8'
  835. z = y.abs() // '0.8'</pre>
  836. <h5 id="cmp">
  837. comparedTo<code class='inset'>.comparedTo(n [, base]) <i>&rArr; number</i></code>
  838. </h5>
  839. <p>
  840. <code>n</code>: <i>number|string|BigNumber</i><br />
  841. <code>base</code>: <i>number</i><br />
  842. <i>See <a href="#bignumber">BigNumber</a> for further parameter details.</i>
  843. </p>
  844. <table>
  845. <tr><th>Returns</th><th>&nbsp;</th></tr>
  846. <tr>
  847. <td class='centre'><code>1</code></td>
  848. <td>If the value of this BigNumber is greater than the value of <code>n</code></td>
  849. </tr>
  850. <tr>
  851. <td class='centre'><code>-1</code></td>
  852. <td>If the value of this BigNumber is less than the value of <code>n</code></td>
  853. </tr>
  854. <tr>
  855. <td class='centre'><code>0</code></td>
  856. <td>If this BigNumber and <code>n</code> have the same value</td>
  857. </tr>
  858. <tr>
  859. <td class='centre'><code>null</code></td>
  860. <td>If the value of either this BigNumber or <code>n</code> is <code>NaN</code></td>
  861. </tr>
  862. </table>
  863. <pre>
  864. x = new BigNumber(Infinity)
  865. y = new BigNumber(5)
  866. x.comparedTo(y) // 1
  867. x.comparedTo(x.minus(1)) // 0
  868. y.comparedTo(NaN) // null
  869. y.comparedTo('110', 2) // -1</pre>
  870. <h5 id="dp">
  871. decimalPlaces<code class='inset'>.dp([dp [, rm]]) <i>&rArr; BigNumber|number</i></code>
  872. </h5>
  873. <p>
  874. <code>dp</code>: <i>number</i>: integer, <code>0</code> to <code>1e+9</code> inclusive<br />
  875. <code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive
  876. </p>
  877. <p>
  878. If <code>dp</code> is a number, returns a BigNumber whose value is the value of this BigNumber
  879. rounded by rounding mode <code>rm</code> to a maximum of <code>dp</code> decimal places.
  880. </p>
  881. <p>
  882. If <code>dp</code> is omitted, or is <code>null</code> or <code>undefined</code>, the return
  883. value is the number of decimal places of the value of this BigNumber, or <code>null</code> if
  884. the value of this BigNumber is &plusmn;<code>Infinity</code> or <code>NaN</code>.
  885. </p>
  886. <p>
  887. If <code>rm</code> is omitted, or is <code>null</code> or <code>undefined</code>,
  888. <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> is used.
  889. </p>
  890. <p>
  891. Throws if <code>dp</code> or <code>rm</code> is invalid. See <a href='#Errors'>Errors</a>.
  892. </p>
  893. <pre>
  894. x = new BigNumber(1234.56)
  895. x.decimalPlaces(1) // '1234.6'
  896. x.dp() // 2
  897. x.decimalPlaces(2) // '1234.56'
  898. x.dp(10) // '1234.56'
  899. x.decimalPlaces(0, 1) // '1234'
  900. x.dp(0, 6) // '1235'
  901. x.decimalPlaces(1, 1) // '1234.5'
  902. x.dp(1, BigNumber.ROUND_HALF_EVEN) // '1234.6'
  903. x // '1234.56'
  904. y = new BigNumber('9.9e-101')
  905. y.dp() // 102</pre>
  906. <h5 id="div">dividedBy<code class='inset'>.div(n [, base]) <i>&rArr; BigNumber</i></code>
  907. </h5>
  908. <p>
  909. <code>n</code>: <i>number|string|BigNumber</i><br />
  910. <code>base</code>: <i>number</i><br />
  911. <i>See <a href="#bignumber">BigNumber</a> for further parameter details.</i>
  912. </p>
  913. <p>
  914. Returns a BigNumber whose value is the value of this BigNumber divided by
  915. <code>n</code>, rounded according to the current
  916. <a href='#decimal-places'><code>DECIMAL_PLACES</code></a> and
  917. <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> settings.
  918. </p>
  919. <pre>
  920. x = new BigNumber(355)
  921. y = new BigNumber(113)
  922. x.dividedBy(y) // '3.14159292035398230088'
  923. x.div(5) // '71'
  924. x.div(47, 16) // '5'</pre>
  925. <h5 id="divInt">
  926. dividedToIntegerBy<code class='inset'>.idiv(n [, base]) &rArr;
  927. <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>See <a href="#bignumber">BigNumber</a> for further parameter details.</i>
  933. </p>
  934. <p>
  935. Returns a BigNumber whose value is the integer part of dividing the value of this BigNumber by
  936. <code>n</code>.
  937. </p>
  938. <pre>
  939. x = new BigNumber(5)
  940. y = new BigNumber(3)
  941. x.dividedToIntegerBy(y) // '1'
  942. x.idiv(0.7) // '7'
  943. x.idiv('0.f', 16) // '5'</pre>
  944. <h5 id="pow">
  945. exponentiatedBy<code class='inset'>.pow(n [, m]) <i>&rArr; BigNumber</i></code>
  946. </h5>
  947. <p>
  948. <code>n</code>: <i>number|string|BigNumber</i>: integer<br />
  949. <code>m</code>: <i>number|string|BigNumber</i>
  950. </p>
  951. <p>
  952. Returns a BigNumber whose value is the value of this BigNumber exponentiated by
  953. <code>n</code>, i.e. raised to the power <code>n</code>, and optionally modulo a modulus
  954. <code>m</code>.
  955. </p>
  956. <p>
  957. Throws if <code>n</code> is not an integer. See <a href='#Errors'>Errors</a>.
  958. </p>
  959. <p>
  960. If <code>n</code> is negative the result is rounded according to the current
  961. <a href='#decimal-places'><code>DECIMAL_PLACES</code></a> and
  962. <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> settings.
  963. </p>
  964. <p>
  965. As the number of digits of the result of the power operation can grow so large so quickly,
  966. e.g. 123.456<sup>10000</sup> has over <code>50000</code> digits, the number of significant
  967. digits calculated is limited to the value of the
  968. <a href='#pow-precision'><code>POW_PRECISION</code></a> setting (unless a modulus
  969. <code>m</code> is specified).
  970. </p>
  971. <p>
  972. By default <a href='#pow-precision'><code>POW_PRECISION</code></a> is set to <code>0</code>.
  973. This means that an unlimited number of significant digits will be calculated, and that the
  974. method's performance will decrease dramatically for larger exponents.
  975. </p>
  976. <p>
  977. If <code>m</code> is specified and the value of <code>m</code>, <code>n</code> and this
  978. BigNumber are integers, and <code>n</code> is positive, then a fast modular exponentiation
  979. algorithm is used, otherwise the operation will be performed as
  980. <code>x.exponentiatedBy(n).modulo(m)</code> with a
  981. <a href='#pow-precision'><code>POW_PRECISION</code></a> of <code>0</code>.
  982. </p>
  983. <pre>
  984. Math.pow(0.7, 2) // 0.48999999999999994
  985. x = new BigNumber(0.7)
  986. x.exponentiatedBy(2) // '0.49'
  987. BigNumber(3).pow(-2) // '0.11111111111111111111'</pre>
  988. <h5 id="int">
  989. integerValue<code class='inset'>.integerValue([rm]) <i>&rArr; BigNumber</i></code>
  990. </h5>
  991. <p>
  992. <code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive
  993. </p>
  994. <p>
  995. Returns a BigNumber whose value is the value of this BigNumber rounded to an integer using
  996. rounding mode <code>rm</code>.
  997. </p>
  998. <p>
  999. If <code>rm</code> is omitted, or is <code>null</code> or <code>undefined</code>,
  1000. <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> is used.
  1001. </p>
  1002. <p>
  1003. Throws if <code>rm</code> is invalid. See <a href='#Errors'>Errors</a>.
  1004. </p>
  1005. <pre>
  1006. x = new BigNumber(123.456)
  1007. x.integerValue() // '123'
  1008. x.integerValue(BigNumber.ROUND_CEIL) // '124'
  1009. y = new BigNumber(-12.7)
  1010. y.integerValue() // '-13'
  1011. y.integerValue(BigNumber.ROUND_DOWN) // '-12'</pre>
  1012. <p>
  1013. The following is an example of how to add a prototype method that emulates JavaScript's
  1014. <code>Math.round</code> function. <code>Math.ceil</code>, <code>Math.floor</code> and
  1015. <code>Math.trunc</code> can be emulated in the same way with
  1016. <code>BigNumber.ROUND_CEIL</code>, <code>BigNumber.ROUND_FLOOR</code> and
  1017. <code> BigNumber.ROUND_DOWN</code> respectively.
  1018. </p>
  1019. <pre>
  1020. BigNumber.prototype.round = function () {
  1021. return this.integerValue(BigNumber.ROUND_HALF_CEIL);
  1022. };
  1023. x.round() // '123'</pre>
  1024. <h5 id="eq">isEqualTo<code class='inset'>.eq(n [, base]) <i>&rArr; boolean</i></code></h5>
  1025. <p>
  1026. <code>n</code>: <i>number|string|BigNumber</i><br />
  1027. <code>base</code>: <i>number</i><br />
  1028. <i>See <a href="#bignumber">BigNumber</a> for further parameter details.</i>
  1029. </p>
  1030. <p>
  1031. Returns <code>true</code> if the value of this BigNumber is equal to the value of
  1032. <code>n</code>, otherwise returns <code>false</code>.<br />
  1033. As with JavaScript, <code>NaN</code> does not equal <code>NaN</code>.
  1034. </p>
  1035. <p>Note: This method uses the <a href='#cmp'><code>comparedTo</code></a> method internally.</p>
  1036. <pre>
  1037. 0 === 1e-324 // true
  1038. x = new BigNumber(0)
  1039. x.isEqualTo('1e-324') // false
  1040. BigNumber(-0).eq(x) // true ( -0 === 0 )
  1041. BigNumber(255).eq('ff', 16) // true
  1042. y = new BigNumber(NaN)
  1043. y.isEqualTo(NaN) // false</pre>
  1044. <h5 id="isF">isFinite<code class='inset'>.isFinite() <i>&rArr; boolean</i></code></h5>
  1045. <p>
  1046. Returns <code>true</code> if the value of this BigNumber is a finite number, otherwise
  1047. returns <code>false</code>.
  1048. </p>
  1049. <p>
  1050. The only possible non-finite values of a BigNumber are <code>NaN</code>, <code>Infinity</code>
  1051. and <code>-Infinity</code>.
  1052. </p>
  1053. <pre>
  1054. x = new BigNumber(1)
  1055. x.isFinite() // true
  1056. y = new BigNumber(Infinity)
  1057. y.isFinite() // false</pre>
  1058. <p>
  1059. Note: The native method <code>isFinite()</code> can be used if
  1060. <code>n &lt;= Number.MAX_VALUE</code>.
  1061. </p>
  1062. <h5 id="gt">isGreaterThan<code class='inset'>.gt(n [, base]) <i>&rArr; boolean</i></code></h5>
  1063. <p>
  1064. <code>n</code>: <i>number|string|BigNumber</i><br />
  1065. <code>base</code>: <i>number</i><br />
  1066. <i>See <a href="#bignumber">BigNumber</a> for further parameter details.</i>
  1067. </p>
  1068. <p>
  1069. Returns <code>true</code> if the value of this BigNumber is greater than the value of
  1070. <code>n</code>, otherwise returns <code>false</code>.
  1071. </p>
  1072. <p>Note: This method uses the <a href='#cmp'><code>comparedTo</code></a> method internally.</p>
  1073. <pre>
  1074. 0.1 &gt; (0.3 - 0.2) // true
  1075. x = new BigNumber(0.1)
  1076. x.isGreaterThan(BigNumber(0.3).minus(0.2)) // false
  1077. BigNumber(0).gt(x) // false
  1078. BigNumber(11, 3).gt(11.1, 2) // true</pre>
  1079. <h5 id="gte">
  1080. isGreaterThanOrEqualTo<code class='inset'>.gte(n [, base]) <i>&rArr; boolean</i></code>
  1081. </h5>
  1082. <p>
  1083. <code>n</code>: <i>number|string|BigNumber</i><br />
  1084. <code>base</code>: <i>number</i><br />
  1085. <i>See <a href="#bignumber">BigNumber</a> for further parameter details.</i>
  1086. </p>
  1087. <p>
  1088. Returns <code>true</code> if the value of this BigNumber is greater than or equal to the value
  1089. of <code>n</code>, otherwise returns <code>false</code>.
  1090. </p>
  1091. <p>Note: This method uses the <a href='#cmp'><code>comparedTo</code></a> method internally.</p>
  1092. <pre>
  1093. (0.3 - 0.2) &gt;= 0.1 // false
  1094. x = new BigNumber(0.3).minus(0.2)
  1095. x.isGreaterThanOrEqualTo(0.1) // true
  1096. BigNumber(1).gte(x) // true
  1097. BigNumber(10, 18).gte('i', 36) // true</pre>
  1098. <h5 id="isInt">isInteger<code class='inset'>.isInteger() <i>&rArr; boolean</i></code></h5>
  1099. <p>
  1100. Returns <code>true</code> if the value of this BigNumber is an integer, otherwise returns
  1101. <code>false</code>.
  1102. </p>
  1103. <pre>
  1104. x = new BigNumber(1)
  1105. x.isInteger() // true
  1106. y = new BigNumber(123.456)
  1107. y.isInteger() // false</pre>
  1108. <h5 id="lt">isLessThan<code class='inset'>.lt(n [, base]) <i>&rArr; boolean</i></code></h5>
  1109. <p>
  1110. <code>n</code>: <i>number|string|BigNumber</i><br />
  1111. <code>base</code>: <i>number</i><br />
  1112. <i>See <a href="#bignumber">BigNumber</a> for further parameter details.</i>
  1113. </p>
  1114. <p>
  1115. Returns <code>true</code> if the value of this BigNumber is less than the value of
  1116. <code>n</code>, otherwise returns <code>false</code>.
  1117. </p>
  1118. <p>Note: This method uses the <a href='#cmp'><code>comparedTo</code></a> method internally.</p>
  1119. <pre>
  1120. (0.3 - 0.2) &lt; 0.1 // true
  1121. x = new BigNumber(0.3).minus(0.2)
  1122. x.isLessThan(0.1) // false
  1123. BigNumber(0).lt(x) // true
  1124. BigNumber(11.1, 2).lt(11, 3) // true</pre>
  1125. <h5 id="lte">
  1126. isLessThanOrEqualTo<code class='inset'>.lte(n [, base]) <i>&rArr; boolean</i></code>
  1127. </h5>
  1128. <p>
  1129. <code>n</code>: <i>number|string|BigNumber</i><br />
  1130. <code>base</code>: <i>number</i><br />
  1131. <i>See <a href="#bignumber">BigNumber</a> for further parameter details.</i>
  1132. </p>
  1133. <p>
  1134. Returns <code>true</code> if the value of this BigNumber is less than or equal to the value of
  1135. <code>n</code>, otherwise returns <code>false</code>.
  1136. </p>
  1137. <p>Note: This method uses the <a href='#cmp'><code>comparedTo</code></a> method internally.</p>
  1138. <pre>
  1139. 0.1 &lt;= (0.3 - 0.2) // false
  1140. x = new BigNumber(0.1)
  1141. x.isLessThanOrEqualTo(BigNumber(0.3).minus(0.2)) // true
  1142. BigNumber(-1).lte(x) // true
  1143. BigNumber(10, 18).lte('i', 36) // true</pre>
  1144. <h5 id="isNaN">isNaN<code class='inset'>.isNaN() <i>&rArr; boolean</i></code></h5>
  1145. <p>
  1146. Returns <code>true</code> if the value of this BigNumber is <code>NaN</code>, otherwise
  1147. returns <code>false</code>.
  1148. </p>
  1149. <pre>
  1150. x = new BigNumber(NaN)
  1151. x.isNaN() // true
  1152. y = new BigNumber('Infinity')
  1153. y.isNaN() // false</pre>
  1154. <p>Note: The native method <code>isNaN()</code> can also be used.</p>
  1155. <h5 id="isNeg">isNegative<code class='inset'>.isNegative() <i>&rArr; boolean</i></code></h5>
  1156. <p>
  1157. Returns <code>true</code> if the sign of this BigNumber is negative, otherwise returns
  1158. <code>false</code>.
  1159. </p>
  1160. <pre>
  1161. x = new BigNumber(-0)
  1162. x.isNegative() // true
  1163. y = new BigNumber(2)
  1164. y.isNegative() // false</pre>
  1165. <p>Note: <code>n &lt; 0</code> can be used if <code>n &lt;= -Number.MIN_VALUE</code>.</p>
  1166. <h5 id="isPos">isPositive<code class='inset'>.isPositive() <i>&rArr; boolean</i></code></h5>
  1167. <p>
  1168. Returns <code>true</code> if the sign of this BigNumber is positive, otherwise returns
  1169. <code>false</code>.
  1170. </p>
  1171. <pre>
  1172. x = new BigNumber(-0)
  1173. x.isPositive() // false
  1174. y = new BigNumber(2)
  1175. y.isPositive() // true</pre>
  1176. <h5 id="isZ">isZero<code class='inset'>.isZero() <i>&rArr; boolean</i></code></h5>
  1177. <p>
  1178. Returns <code>true</code> if the value of this BigNumber is zero or minus zero, otherwise
  1179. returns <code>false</code>.
  1180. </p>
  1181. <pre>
  1182. x = new BigNumber(-0)
  1183. x.isZero() &amp;&amp; x.isNegative() // true
  1184. y = new BigNumber(Infinity)
  1185. y.isZero() // false</pre>
  1186. <p>Note: <code>n == 0</code> can be used if <code>n &gt;= Number.MIN_VALUE</code>.</p>
  1187. <h5 id="minus">
  1188. minus<code class='inset'>.minus(n [, base]) <i>&rArr; BigNumber</i></code>
  1189. </h5>
  1190. <p>
  1191. <code>n</code>: <i>number|string|BigNumber</i><br />
  1192. <code>base</code>: <i>number</i><br />
  1193. <i>See <a href="#bignumber">BigNumber</a> for further parameter details.</i>
  1194. </p>
  1195. <p>Returns a BigNumber whose value is the value of this BigNumber minus <code>n</code>.</p>
  1196. <p>The return value is always exact and unrounded.</p>
  1197. <pre>
  1198. 0.3 - 0.1 // 0.19999999999999998
  1199. x = new BigNumber(0.3)
  1200. x.minus(0.1) // '0.2'
  1201. x.minus(0.6, 20) // '0'</pre>
  1202. <h5 id="mod">modulo<code class='inset'>.mod(n [, base]) <i>&rArr; BigNumber</i></code></h5>
  1203. <p>
  1204. <code>n</code>: <i>number|string|BigNumber</i><br />
  1205. <code>base</code>: <i>number</i><br />
  1206. <i>See <a href="#bignumber">BigNumber</a> for further parameter details.</i>
  1207. </p>
  1208. <p>
  1209. Returns a BigNumber whose value is the value of this BigNumber modulo <code>n</code>, i.e.
  1210. the integer remainder of dividing this BigNumber by <code>n</code>.
  1211. </p>
  1212. <p>
  1213. The value returned, and in particular its sign, is dependent on the value of the
  1214. <a href='#modulo-mode'><code>MODULO_MODE</code></a> setting of this BigNumber constructor.
  1215. If it is <code>1</code> (default value), the result will have the same sign as this BigNumber,
  1216. and it will match that of Javascript's <code>%</code> operator (within the limits of double
  1217. precision) and BigDecimal's <code>remainder</code> method.
  1218. </p>
  1219. <p>The return value is always exact and unrounded.</p>
  1220. <p>
  1221. See <a href='#modulo-mode'><code>MODULO_MODE</code></a> for a description of the other
  1222. modulo modes.
  1223. </p>
  1224. <pre>
  1225. 1 % 0.9 // 0.09999999999999998
  1226. x = new BigNumber(1)
  1227. x.modulo(0.9) // '0.1'
  1228. y = new BigNumber(33)
  1229. y.mod('a', 33) // '3'</pre>
  1230. <h5 id="times">
  1231. multipliedBy<code class='inset'>.times(n [, base]) <i>&rArr; BigNumber</i></code>
  1232. </h5>
  1233. <p>
  1234. <code>n</code>: <i>number|string|BigNumber</i><br />
  1235. <code>base</code>: <i>number</i><br />
  1236. <i>See <a href="#bignumber">BigNumber</a> for further parameter details.</i>
  1237. </p>
  1238. <p>
  1239. Returns a BigNumber whose value is the value of this BigNumber multiplied by <code>n</code>.
  1240. </p>
  1241. <p>The return value is always exact and unrounded.</p>
  1242. <pre>
  1243. 0.6 * 3 // 1.7999999999999998
  1244. x = new BigNumber(0.6)
  1245. y = x.multipliedBy(3) // '1.8'
  1246. BigNumber('7e+500').times(y) // '1.26e+501'
  1247. x.multipliedBy('-a', 16) // '-6'</pre>
  1248. <h5 id="neg">negated<code class='inset'>.negated() <i>&rArr; BigNumber</i></code></h5>
  1249. <p>
  1250. Returns a BigNumber whose value is the value of this BigNumber negated, i.e. multiplied by
  1251. <code>-1</code>.
  1252. </p>
  1253. <pre>
  1254. x = new BigNumber(1.8)
  1255. x.negated() // '-1.8'
  1256. y = new BigNumber(-1.3)
  1257. y.negated() // '1.3'</pre>
  1258. <h5 id="plus">plus<code class='inset'>.plus(n [, base]) <i>&rArr; BigNumber</i></code></h5>
  1259. <p>
  1260. <code>n</code>: <i>number|string|BigNumber</i><br />
  1261. <code>base</code>: <i>number</i><br />
  1262. <i>See <a href="#bignumber">BigNumber</a> for further parameter details.</i>
  1263. </p>
  1264. <p>Returns a BigNumber whose value is the value of this BigNumber plus <code>n</code>.</p>
  1265. <p>The return value is always exact and unrounded.</p>
  1266. <pre>
  1267. 0.1 + 0.2 // 0.30000000000000004
  1268. x = new BigNumber(0.1)
  1269. y = x.plus(0.2) // '0.3'
  1270. BigNumber(0.7).plus(x).plus(y) // '1.1'
  1271. x.plus('0.1', 8) // '0.225'</pre>
  1272. <h5 id="sd">
  1273. precision<code class='inset'>.sd([d [, rm]]) <i>&rArr; BigNumber|number</i></code>
  1274. </h5>
  1275. <p>
  1276. <code>d</code>: <i>number|boolean</i>: integer, <code>1</code> to <code>1e+9</code>
  1277. inclusive, or <code>true</code> or <code>false</code><br />
  1278. <code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive.
  1279. </p>
  1280. <p>
  1281. If <code>d</code> is a number, returns a BigNumber whose value is the value of this BigNumber
  1282. rounded to a precision of <code>d</code> significant digits using rounding mode
  1283. <code>rm</code>.
  1284. </p>
  1285. <p>
  1286. If <code>d</code> is omitted or is <code>null</code> or <code>undefined</code>, the return
  1287. value is the number of significant digits of the value of this BigNumber, or <code>null</code>
  1288. if the value of this BigNumber is &plusmn;<code>Infinity</code> or <code>NaN</code>.
  1289. </p>
  1290. <p>
  1291. If <code>d</code> is <code>true</code> then any trailing zeros of the integer
  1292. part of a number are counted as significant digits, otherwise they are not.
  1293. </p>
  1294. <p>
  1295. If <code>rm</code> is omitted or is <code>null</code> or <code>undefined</code>,
  1296. <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> will be used.
  1297. </p>
  1298. <p>
  1299. Throws if <code>d</code> or <code>rm</code> is invalid. See <a href='#Errors'>Errors</a>.
  1300. </p>
  1301. <pre>
  1302. x = new BigNumber(9876.54321)
  1303. x.precision(6) // '9876.54'
  1304. x.sd() // 9
  1305. x.precision(6, BigNumber.ROUND_UP) // '9876.55'
  1306. x.sd(2) // '9900'
  1307. x.precision(2, 1) // '9800'
  1308. x // '9876.54321'
  1309. y = new BigNumber(987000)
  1310. y.precision() // 3
  1311. y.sd(true) // 6</pre>
  1312. <h5 id="shift">shiftedBy<code class='inset'>.shiftedBy(n) <i>&rArr; BigNumber</i></code></h5>
  1313. <p>
  1314. <code>n</code>: <i>number</i>: integer,
  1315. <code>-9007199254740991</code> to <code>9007199254740991</code> inclusive
  1316. </p>
  1317. <p>
  1318. Returns a BigNumber whose value is the value of this BigNumber shifted by <code>n</code>
  1319. places.
  1320. <p>
  1321. The shift is of the decimal point, i.e. of powers of ten, and is to the left if <code>n</code>
  1322. is negative or to the right if <code>n</code> is positive.
  1323. </p>
  1324. <p>The return value is always exact and unrounded.</p>
  1325. <p>
  1326. Throws if <code>n</code> is invalid. See <a href='#Errors'>Errors</a>.
  1327. </p>
  1328. <pre>
  1329. x = new BigNumber(1.23)
  1330. x.shiftedBy(3) // '1230'
  1331. x.shiftedBy(-3) // '0.00123'</pre>
  1332. <h5 id="sqrt">squareRoot<code class='inset'>.sqrt() <i>&rArr; BigNumber</i></code></h5>
  1333. <p>
  1334. Returns a BigNumber whose value is the square root of the value of this BigNumber,
  1335. rounded according to the current
  1336. <a href='#decimal-places'><code>DECIMAL_PLACES</code></a> and
  1337. <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> settings.
  1338. </p>
  1339. <p>
  1340. The return value will be correctly rounded, i.e. rounded as if the result was first calculated
  1341. to an infinite number of correct digits before rounding.
  1342. </p>
  1343. <pre>
  1344. x = new BigNumber(16)
  1345. x.squareRoot() // '4'
  1346. y = new BigNumber(3)
  1347. y.sqrt() // '1.73205080756887729353'</pre>
  1348. <h5 id="toE">
  1349. toExponential<code class='inset'>.toExponential([dp [, rm]]) <i>&rArr; string</i></code>
  1350. </h5>
  1351. <p>
  1352. <code>dp</code>: <i>number</i>: integer, <code>0</code> to <code>1e+9</code> inclusive<br />
  1353. <code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive
  1354. </p>
  1355. <p>
  1356. Returns a string representing the value of this BigNumber in exponential notation rounded
  1357. using rounding mode <code>rm</code> to <code>dp</code> decimal places, i.e with one digit
  1358. before the decimal point and <code>dp</code> digits after it.
  1359. </p>
  1360. <p>
  1361. If the value of this BigNumber in exponential notation has fewer than <code>dp</code> fraction
  1362. digits, the return value will be appended with zeros accordingly.
  1363. </p>
  1364. <p>
  1365. If <code>dp</code> is omitted, or is <code>null</code> or <code>undefined</code>, the number
  1366. of digits after the decimal point defaults to the minimum number of digits necessary to
  1367. represent the value exactly.<br />
  1368. If <code>rm</code> is omitted or is <code>null</code> or <code>undefined</code>,
  1369. <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> is used.
  1370. </p>
  1371. <p>
  1372. Throws if <code>dp</code> or <code>rm</code> is invalid. See <a href='#Errors'>Errors</a>.
  1373. </p>
  1374. <pre>
  1375. x = 45.6
  1376. y = new BigNumber(x)
  1377. x.toExponential() // '4.56e+1'
  1378. y.toExponential() // '4.56e+1'
  1379. x.toExponential(0) // '5e+1'
  1380. y.toExponential(0) // '5e+1'
  1381. x.toExponential(1) // '4.6e+1'
  1382. y.toExponential(1) // '4.6e+1'
  1383. y.toExponential(1, 1) // '4.5e+1' (ROUND_DOWN)
  1384. x.toExponential(3) // '4.560e+1'
  1385. y.toExponential(3) // '4.560e+1'</pre>
  1386. <h5 id="toFix">
  1387. toFixed<code class='inset'>.toFixed([dp [, rm]]) <i>&rArr; string</i></code>
  1388. </h5>
  1389. <p>
  1390. <code>dp</code>: <i>number</i>: integer, <code>0</code> to <code>1e+9</code> inclusive<br />
  1391. <code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive
  1392. </p>
  1393. <p>
  1394. Returns a string representing the value of this BigNumber in normal (fixed-point) notation
  1395. rounded to <code>dp</code> decimal places using rounding mode <code>rm</code>.
  1396. </p>
  1397. <p>
  1398. If the value of this BigNumber in normal notation has fewer than <code>dp</code> fraction
  1399. digits, the return value will be appended with zeros accordingly.
  1400. </p>
  1401. <p>
  1402. Unlike <code>Number.prototype.toFixed</code>, which returns exponential notation if a number
  1403. is greater or equal to <code>10<sup>21</sup></code>, this method will always return normal
  1404. notation.
  1405. </p>
  1406. <p>
  1407. If <code>dp</code> is omitted or is <code>null</code> or <code>undefined</code>, the return
  1408. value will be unrounded and in normal notation. This is also unlike
  1409. <code>Number.prototype.toFixed</code>, which returns the value to zero decimal places.<br />
  1410. It is useful when fixed-point notation is required and the current
  1411. <a href="#exponential-at"><code>EXPONENTIAL_AT</code></a> setting causes
  1412. <code><a href='#toS'>toString</a></code> to return exponential notation.<br />
  1413. If <code>rm</code> is omitted or is <code>null</code> or <code>undefined</code>,
  1414. <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> is used.
  1415. </p>
  1416. <p>
  1417. Throws if <code>dp</code> or <code>rm</code> is invalid. See <a href='#Errors'>Errors</a>.
  1418. </p>
  1419. <pre>
  1420. x = 3.456
  1421. y = new BigNumber(x)
  1422. x.toFixed() // '3'
  1423. y.toFixed() // '3.456'
  1424. y.toFixed(0) // '3'
  1425. x.toFixed(2) // '3.46'
  1426. y.toFixed(2) // '3.46'
  1427. y.toFixed(2, 1) // '3.45' (ROUND_DOWN)
  1428. x.toFixed(5) // '3.45600'
  1429. y.toFixed(5) // '3.45600'</pre>
  1430. <h5 id="toFor">
  1431. toFormat<code class='inset'>.toFormat([dp [, rm[, format]]]) <i>&rArr; string</i></code>
  1432. </h5>
  1433. <p>
  1434. <code>dp</code>: <i>number</i>: integer, <code>0</code> to <code>1e+9</code> inclusive<br />
  1435. <code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive<br />
  1436. <code>format</code>: <i>object</i>: see <a href='#format'><code>FORMAT</code></a>
  1437. </p>
  1438. <p>
  1439. <p>
  1440. Returns a string representing the value of this BigNumber in normal (fixed-point) notation
  1441. rounded to <code>dp</code> decimal places using rounding mode <code>rm</code>, and formatted
  1442. according to the properties of the <code>format</code> object.
  1443. </p>
  1444. <p>
  1445. See <a href='#format'><code>FORMAT</code></a> and the examples below for the properties of the
  1446. <code>format</code> object, their types, and their usage. A formatting object may contain
  1447. some or all of the recognised properties.
  1448. </p>
  1449. <p>
  1450. If <code>dp</code> is omitted or is <code>null</code> or <code>undefined</code>, then the
  1451. return value is not rounded to a fixed number of decimal places.<br />
  1452. If <code>rm</code> is omitted or is <code>null</code> or <code>undefined</code>,
  1453. <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> is used.<br />
  1454. If <code>format</code> is omitted or is <code>null</code> or <code>undefined</code>, the
  1455. <a href='#format'><code>FORMAT</code></a> object is used.
  1456. </p>
  1457. <p>
  1458. Throws if <code>dp</code>, <code>rm</code> or <code>format</code> is invalid. See
  1459. <a href='#Errors'>Errors</a>.
  1460. </p>
  1461. <pre>
  1462. fmt = {
  1463. prefix: '',
  1464. decimalSeparator: '.',
  1465. groupSeparator: ',',
  1466. groupSize: 3,
  1467. secondaryGroupSize: 0,
  1468. fractionGroupSeparator: ' ',
  1469. fractionGroupSize: 0,
  1470. suffix: ''
  1471. }
  1472. x = new BigNumber('123456789.123456789')
  1473. // Set the global formatting options
  1474. BigNumber.config({ FORMAT: fmt })
  1475. x.toFormat() // '123,456,789.123456789'
  1476. x.toFormat(3) // '123,456,789.123'
  1477. // If a reference to the object assigned to FORMAT has been retained,
  1478. // the format properties can be changed directly
  1479. fmt.groupSeparator = ' '
  1480. fmt.fractionGroupSize = 5
  1481. x.toFormat() // '123 456 789.12345 6789'
  1482. // Alternatively, pass the formatting options as an argument
  1483. fmt = {
  1484. prefix: '=> ',
  1485. decimalSeparator: ',',
  1486. groupSeparator: '.',
  1487. groupSize: 3,
  1488. secondaryGroupSize: 2
  1489. }
  1490. x.toFormat() // '123 456 789.12345 6789'
  1491. x.toFormat(fmt) // '=> 12.34.56.789,123456789'
  1492. x.toFormat(2, fmt) // '=> 12.34.56.789,12'
  1493. x.toFormat(3, BigNumber.ROUND_UP, fmt) // '=> 12.34.56.789,124'</pre>
  1494. <h5 id="toFr">
  1495. toFraction<code class='inset'>.toFraction([maximum_denominator])
  1496. <i>&rArr; [BigNumber, BigNumber]</i></code>
  1497. </h5>
  1498. <p>
  1499. <code>maximum_denominator</code>:
  1500. <i>number|string|BigNumber</i>: integer &gt;= <code>1</code> and &lt;=
  1501. <code>Infinity</code>
  1502. </p>
  1503. <p>
  1504. Returns an array of two BigNumbers representing the value of this BigNumber as a simple
  1505. fraction with an integer numerator and an integer denominator. The denominator will be a
  1506. positive non-zero value less than or equal to <code>maximum_denominator</code>.
  1507. </p>
  1508. <p>
  1509. If a <code>maximum_denominator</code> is not specified, or is <code>null</code> or
  1510. <code>undefined</code>, the denominator will be the lowest value necessary to represent the
  1511. number exactly.
  1512. </p>
  1513. <p>
  1514. Throws if <code>maximum_denominator</code> is invalid. See <a href='#Errors'>Errors</a>.
  1515. </p>
  1516. <pre>
  1517. x = new BigNumber(1.75)
  1518. x.toFraction() // '7, 4'
  1519. pi = new BigNumber('3.14159265358')
  1520. pi.toFraction() // '157079632679,50000000000'
  1521. pi.toFraction(100000) // '312689, 99532'
  1522. pi.toFraction(10000) // '355, 113'
  1523. pi.toFraction(100) // '311, 99'
  1524. pi.toFraction(10) // '22, 7'
  1525. pi.toFraction(1) // '3, 1'</pre>
  1526. <h5 id="toJSON">toJSON<code class='inset'>.toJSON() <i>&rArr; string</i></code></h5>
  1527. <p>As <a href='#valueOf'><code>valueOf</code></a>.</p>
  1528. <pre>
  1529. x = new BigNumber('177.7e+457')
  1530. y = new BigNumber(235.4325)
  1531. z = new BigNumber('0.0098074')
  1532. // Serialize an array of three BigNumbers
  1533. str = JSON.stringify( [x, y, z] )
  1534. // "["1.777e+459","235.4325","0.0098074"]"
  1535. // Return an array of three BigNumbers
  1536. JSON.parse(str, function (key, val) {
  1537. return key === '' ? val : new BigNumber(val)
  1538. })</pre>
  1539. <h5 id="toN">toNumber<code class='inset'>.toNumber() <i>&rArr; number</i></code></h5>
  1540. <p>Returns the value of this BigNumber as a JavaScript number primitive.</p>
  1541. <p>
  1542. This method is identical to using type coercion with the unary plus operator.
  1543. </p>
  1544. <pre>
  1545. x = new BigNumber(456.789)
  1546. x.toNumber() // 456.789
  1547. +x // 456.789
  1548. y = new BigNumber('45987349857634085409857349856430985')
  1549. y.toNumber() // 4.598734985763409e+34
  1550. z = new BigNumber(-0)
  1551. 1 / z.toNumber() // -Infinity
  1552. 1 / +z // -Infinity</pre>
  1553. <h5 id="toP">
  1554. toPrecision<code class='inset'>.toPrecision([sd [, rm]]) <i>&rArr; string</i></code>
  1555. </h5>
  1556. <p>
  1557. <code>sd</code>: <i>number</i>: integer, <code>1</code> to <code>1e+9</code> inclusive<br />
  1558. <code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive
  1559. </p>
  1560. <p>
  1561. Returns a string representing the value of this BigNumber rounded to <code>sd</code>
  1562. significant digits using rounding mode <code>rm</code>.
  1563. </p>
  1564. <p>
  1565. If <code>sd</code> is less than the number of digits necessary to represent the integer part
  1566. of the value in normal (fixed-point) notation, then exponential notation is used.
  1567. </p>
  1568. <p>
  1569. If <code>sd</code> is omitted, or is <code>null</code> or <code>undefined</code>, then the
  1570. return value is the same as <code>n.toString()</code>.<br />
  1571. If <code>rm</code> is omitted or is <code>null</code> or <code>undefined</code>,
  1572. <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> is used.
  1573. </p>
  1574. <p>
  1575. Throws if <code>sd</code> or <code>rm</code> is invalid. See <a href='#Errors'>Errors</a>.
  1576. </p>
  1577. <pre>
  1578. x = 45.6
  1579. y = new BigNumber(x)
  1580. x.toPrecision() // '45.6'
  1581. y.toPrecision() // '45.6'
  1582. x.toPrecision(1) // '5e+1'
  1583. y.toPrecision(1) // '5e+1'
  1584. y.toPrecision(2, 0) // '4.6e+1' (ROUND_UP)
  1585. y.toPrecision(2, 1) // '4.5e+1' (ROUND_DOWN)
  1586. x.toPrecision(5) // '45.600'
  1587. y.toPrecision(5) // '45.600'</pre>
  1588. <h5 id="toS">toString<code class='inset'>.toString([base]) <i>&rArr; string</i></code></h5>
  1589. <p>
  1590. <code>base</code>: <i>number</i>: integer, <code>2</code> to <code>ALPHABET.length</code>
  1591. inclusive (see <a href='#alphabet'><code>ALPHABET</code></a>).
  1592. </p>
  1593. <p>
  1594. Returns a string representing the value of this BigNumber in the specified base, or base
  1595. <code>10</code> if <code>base</code> is omitted or is <code>null</code> or
  1596. <code>undefined</code>.
  1597. </p>
  1598. <p>
  1599. For bases above <code>10</code>, and using the default base conversion alphabet
  1600. (see <a href='#alphabet'><code>ALPHABET</code></a>), values from <code>10</code> to
  1601. <code>35</code> are represented by <code>a-z</code>
  1602. (as with <code>Number.prototype.toString</code>).
  1603. </p>
  1604. <p>
  1605. If a base is specified the value is rounded according to the current
  1606. <a href='#decimal-places'><code>DECIMAL_PLACES</code></a>
  1607. and <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> settings.
  1608. </p>
  1609. <p>
  1610. If a base is not specified, and this BigNumber has a positive
  1611. exponent that is equal to or greater than the positive component of the
  1612. current <a href="#exponential-at"><code>EXPONENTIAL_AT</code></a> setting,
  1613. or a negative exponent equal to or less than the negative component of the
  1614. setting, then exponential notation is returned.
  1615. </p>
  1616. <p>If <code>base</code> is <code>null</code> or <code>undefined</code> it is ignored.</p>
  1617. <p>
  1618. Throws if <code>base</code> is invalid. See <a href='#Errors'>Errors</a>.
  1619. </p>
  1620. <pre>
  1621. x = new BigNumber(750000)
  1622. x.toString() // '750000'
  1623. BigNumber.config({ EXPONENTIAL_AT: 5 })
  1624. x.toString() // '7.5e+5'
  1625. y = new BigNumber(362.875)
  1626. y.toString(2) // '101101010.111'
  1627. y.toString(9) // '442.77777777777777777778'
  1628. y.toString(32) // 'ba.s'
  1629. BigNumber.config({ DECIMAL_PLACES: 4 });
  1630. z = new BigNumber('1.23456789')
  1631. z.toString() // '1.23456789'
  1632. z.toString(10) // '1.2346'</pre>
  1633. <h5 id="valueOf">valueOf<code class='inset'>.valueOf() <i>&rArr; string</i></code></h5>
  1634. <p>
  1635. As <a href='#toS'><code>toString</code></a>, but does not accept a base argument and includes
  1636. the minus sign for negative zero.
  1637. </p>
  1638. <pre>
  1639. x = new BigNumber('-0')
  1640. x.toString() // '0'
  1641. x.valueOf() // '-0'
  1642. y = new BigNumber('1.777e+457')
  1643. y.valueOf() // '1.777e+457'</pre>
  1644. <h4 id="instance-properties">Properties</h4>
  1645. <p>The properties of a BigNumber instance:</p>
  1646. <table>
  1647. <tr>
  1648. <th>Property</th>
  1649. <th>Description</th>
  1650. <th>Type</th>
  1651. <th>Value</th>
  1652. </tr>
  1653. <tr>
  1654. <td class='centre' id='coefficient'><b>c</b></td>
  1655. <td>coefficient<sup>*</sup></td>
  1656. <td><i>number</i><code>[]</code></td>
  1657. <td> Array of base <code>1e14</code> numbers</td>
  1658. </tr>
  1659. <tr>
  1660. <td class='centre' id='exponent'><b>e</b></td>
  1661. <td>exponent</td>
  1662. <td><i>number</i></td>
  1663. <td>Integer, <code>-1000000000</code> to <code>1000000000</code> inclusive</td>
  1664. </tr>
  1665. <tr>
  1666. <td class='centre' id='sign'><b>s</b></td>
  1667. <td>sign</td>
  1668. <td><i>number</i></td>
  1669. <td><code>-1</code> or <code>1</code></td>
  1670. </tr>
  1671. </table>
  1672. <p><sup>*</sup>significand</p>
  1673. <p>
  1674. The value of any of the <code>c</code>, <code>e</code> and <code>s</code> properties may also
  1675. be <code>null</code>.
  1676. </p>
  1677. <p>
  1678. The above properties are best considered to be read-only. In early versions of this library it
  1679. was okay to change the exponent of a BigNumber by writing to its exponent property directly,
  1680. but this is no longer reliable as the value of the first element of the coefficient array is
  1681. now dependent on the exponent.
  1682. </p>
  1683. <p>
  1684. Note that, as with JavaScript numbers, the original exponent and fractional trailing zeros are
  1685. not necessarily preserved.
  1686. </p>
  1687. <pre>x = new BigNumber(0.123) // '0.123'
  1688. x.toExponential() // '1.23e-1'
  1689. x.c // '1,2,3'
  1690. x.e // -1
  1691. x.s // 1
  1692. y = new Number(-123.4567000e+2) // '-12345.67'
  1693. y.toExponential() // '-1.234567e+4'
  1694. z = new BigNumber('-123.4567000e+2') // '-12345.67'
  1695. z.toExponential() // '-1.234567e+4'
  1696. z.c // '1,2,3,4,5,6,7'
  1697. z.e // 4
  1698. z.s // -1</pre>
  1699. <h4 id="zero-nan-infinity">Zero, NaN and Infinity</h4>
  1700. <p>
  1701. The table below shows how &plusmn;<code>0</code>, <code>NaN</code> and
  1702. &plusmn;<code>Infinity</code> are stored.
  1703. </p>
  1704. <table>
  1705. <tr>
  1706. <th> </th>
  1707. <th class='centre'>c</th>
  1708. <th class='centre'>e</th>
  1709. <th class='centre'>s</th>
  1710. </tr>
  1711. <tr>
  1712. <td>&plusmn;0</td>
  1713. <td><code>[0]</code></td>
  1714. <td><code>0</code></td>
  1715. <td><code>&plusmn;1</code></td>
  1716. </tr>
  1717. <tr>
  1718. <td>NaN</td>
  1719. <td><code>null</code></td>
  1720. <td><code>null</code></td>
  1721. <td><code>null</code></td>
  1722. </tr>
  1723. <tr>
  1724. <td>&plusmn;Infinity</td>
  1725. <td><code>null</code></td>
  1726. <td><code>null</code></td>
  1727. <td><code>&plusmn;1</code></td>
  1728. </tr>
  1729. </table>
  1730. <pre>
  1731. x = new Number(-0) // 0
  1732. 1 / x == -Infinity // true
  1733. y = new BigNumber(-0) // '0'
  1734. y.c // '0' ( [0].toString() )
  1735. y.e // 0
  1736. y.s // -1</pre>
  1737. <h4 id='Errors'>Errors</h4>
  1738. <p>The table below shows the errors that are thrown.</p>
  1739. <p>
  1740. The errors are generic <code>Error</code> objects whose message begins
  1741. <code>'[BigNumber Error]'</code>.
  1742. </p>
  1743. <table class='error-table'>
  1744. <tr>
  1745. <th>Method</th>
  1746. <th>Throws</th>
  1747. </tr>
  1748. <tr>
  1749. <td rowspan=6>
  1750. <code>BigNumber</code><br />
  1751. <code>comparedTo</code><br />
  1752. <code>dividedBy</code><br />
  1753. <code>dividedToIntegerBy</code><br />
  1754. <code>isEqualTo</code><br />
  1755. <code>isGreaterThan</code><br />
  1756. <code>isGreaterThanOrEqualTo</code><br />
  1757. <code>isLessThan</code><br />
  1758. <code>isLessThanOrEqualTo</code><br />
  1759. <code>minus</code><br />
  1760. <code>modulo</code><br />
  1761. <code>plus</code><br />
  1762. <code>multipliedBy</code>
  1763. </td>
  1764. <td>Base not a primitive number</td>
  1765. </tr>
  1766. <tr>
  1767. <td>Base not an integer</td>
  1768. </tr>
  1769. <tr>
  1770. <td>Base out of range</td>
  1771. </tr>
  1772. <tr>
  1773. <td>Number primitive has more than 15 significant digits<sup>*</sup></td>
  1774. </tr>
  1775. <tr>
  1776. <td>Not a base... number<sup>*</sup></td>
  1777. </tr>
  1778. <tr>
  1779. <td>Not a number<sup>*</sup></td>
  1780. </tr>
  1781. <tr>
  1782. <td><code>clone</code></td>
  1783. <td>Object expected</td>
  1784. </tr>
  1785. <tr>
  1786. <td rowspan=24><code>config</code></td>
  1787. <td>Object expected</td>
  1788. </tr>
  1789. <tr>
  1790. <td><code>DECIMAL_PLACES</code> not a primitive number</td>
  1791. </tr>
  1792. <tr>
  1793. <td><code>DECIMAL_PLACES</code> not an integer</td>
  1794. </tr>
  1795. <tr>
  1796. <td><code>DECIMAL_PLACES</code> out of range</td>
  1797. </tr>
  1798. <tr>
  1799. <td><code>ROUNDING_MODE</code> not a primitive number</td>
  1800. </tr>
  1801. <tr>
  1802. <td><code>ROUNDING_MODE</code> not an integer</td>
  1803. </tr>
  1804. <tr>
  1805. <td><code>ROUNDING_MODE</code> out of range</td>
  1806. </tr>
  1807. <tr>
  1808. <td><code>EXPONENTIAL_AT</code> not a primitive number</td>
  1809. </tr>
  1810. <tr>
  1811. <td><code>EXPONENTIAL_AT</code> not an integer</td>
  1812. </tr>
  1813. <tr>
  1814. <td><code>EXPONENTIAL_AT</code> out of range</td>
  1815. </tr>
  1816. <tr>
  1817. <td><code>RANGE</code> not a primitive number</td>
  1818. </tr>
  1819. <tr>
  1820. <td><code>RANGE</code> not an integer</td>
  1821. </tr>
  1822. <tr>
  1823. <td><code>RANGE</code> cannot be zero</td>
  1824. </tr>
  1825. <tr>
  1826. <td><code>RANGE</code> cannot be zero</td>
  1827. </tr>
  1828. <tr>
  1829. <td><code>CRYPTO</code> not true or false</td>
  1830. </tr>
  1831. <tr>
  1832. <td><code>crypto</code> unavailable</td>
  1833. </tr>
  1834. <tr>
  1835. <td><code>MODULO_MODE</code> not a primitive number</td>
  1836. </tr>
  1837. <tr>
  1838. <td><code>MODULO_MODE</code> not an integer</td>
  1839. </tr>
  1840. <tr>
  1841. <td><code>MODULO_MODE</code> out of range</td>
  1842. </tr>
  1843. <tr>
  1844. <td><code>POW_PRECISION</code> not a primitive number</td>
  1845. </tr>
  1846. <tr>
  1847. <td><code>POW_PRECISION</code> not an integer</td>
  1848. </tr>
  1849. <tr>
  1850. <td><code>POW_PRECISION</code> out of range</td>
  1851. </tr>
  1852. <tr>
  1853. <td><code>FORMAT</code> not an object</td>
  1854. </tr>
  1855. <tr>
  1856. <td><code>ALPHABET</code> invalid</td>
  1857. </tr>
  1858. <tr>
  1859. <td rowspan=3>
  1860. <code>decimalPlaces</code><br />
  1861. <code>precision</code><br />
  1862. <code>random</code><br />
  1863. <code>shiftedBy</code><br />
  1864. <code>toExponential</code><br />
  1865. <code>toFixed</code><br />
  1866. <code>toFormat</code><br />
  1867. <code>toPrecision</code>
  1868. </td>
  1869. <td>Argument not a primitive number</td>
  1870. </tr>
  1871. <tr>
  1872. <td>Argument not an integer</td>
  1873. </tr>
  1874. <tr>
  1875. <td>Argument out of range</td>
  1876. </tr>
  1877. <tr>
  1878. <td>
  1879. <code>decimalPlaces</code><br />
  1880. <code>precision</code>
  1881. </td>
  1882. <td>Argument not true or false</td>
  1883. </tr>
  1884. <tr>
  1885. <td><code>exponentiatedBy</code></td>
  1886. <td>Argument not an integer</td>
  1887. </tr>
  1888. <tr>
  1889. <td><code>isBigNumber</code></td>
  1890. <td>Invalid BigNumber<sup>*</sup></td>
  1891. </tr>
  1892. <tr>
  1893. <td>
  1894. <code>minimum</code><br />
  1895. <code>maximum</code>
  1896. </td>
  1897. <td>Not a number<sup>*</sup></td>
  1898. </tr>
  1899. <tr>
  1900. <td>
  1901. <code>random</code>
  1902. </td>
  1903. <td>crypto unavailable</td>
  1904. </tr>
  1905. <tr>
  1906. <td>
  1907. <code>toFormat</code>
  1908. </td>
  1909. <td>Argument not an object</td>
  1910. </tr>
  1911. <tr>
  1912. <td rowspan=2><code>toFraction</code></td>
  1913. <td>Argument not an integer</td>
  1914. </tr>
  1915. <tr>
  1916. <td>Argument out of range</td>
  1917. </tr>
  1918. <tr>
  1919. <td rowspan=3><code>toString</code></td>
  1920. <td>Base not a primitive number</td>
  1921. </tr>
  1922. <tr>
  1923. <td>Base not an integer</td>
  1924. </tr>
  1925. <tr>
  1926. <td>Base out of range</td>
  1927. </tr>
  1928. </table>
  1929. <p><sup>*</sup>Only thrown if <code>BigNumber.DEBUG</code> is <code>true</code>.</p>
  1930. <p>To determine if an exception is a BigNumber Error:</p>
  1931. <pre>
  1932. try {
  1933. // ...
  1934. } catch (e) {
  1935. if (e instanceof Error &amp;&amp; e.message.indexOf('[BigNumber Error]') === 0) {
  1936. // ...
  1937. }
  1938. }</pre>
  1939. <h4 id="type-coercion">Type coercion</h4>
  1940. <p>
  1941. To prevent the accidental use of a BigNumber in primitive number operations, or the
  1942. accidental addition of a BigNumber to a string, the <code>valueOf</code> method can be safely
  1943. overwritten as shown below.
  1944. </p>
  1945. <p>
  1946. The <a href='#valueOf'><code>valueOf</code></a> method is the same as the
  1947. <a href='#toJSON'><code>toJSON</code></a> method, and both are the same as the
  1948. <a href='#toS'><code>toString</code></a> method except they do not take a <code>base</code>
  1949. argument and they include the minus sign for negative zero.
  1950. </p>
  1951. <pre>
  1952. BigNumber.prototype.valueOf = function () {
  1953. throw Error('valueOf called!')
  1954. }
  1955. x = new BigNumber(1)
  1956. x / 2 // '[BigNumber Error] valueOf called!'
  1957. x + 'abc' // '[BigNumber Error] valueOf called!'
  1958. </pre>
  1959. <h4 id='faq'>FAQ</h4>
  1960. <h6>Why are trailing fractional zeros removed from BigNumbers?</h6>
  1961. <p>
  1962. Some arbitrary-precision libraries retain trailing fractional zeros as they can indicate the
  1963. precision of a value. This can be useful but the results of arithmetic operations can be
  1964. misleading.
  1965. </p>
  1966. <pre>
  1967. x = new BigDecimal("1.0")
  1968. y = new BigDecimal("1.1000")
  1969. z = x.add(y) // 2.1000
  1970. x = new BigDecimal("1.20")
  1971. y = new BigDecimal("3.45000")
  1972. z = x.multiply(y) // 4.1400000</pre>
  1973. <p>
  1974. To specify the precision of a value is to specify that the value lies
  1975. within a certain range.
  1976. </p>
  1977. <p>
  1978. In the first example, <code>x</code> has a value of <code>1.0</code>. The trailing zero shows
  1979. the precision of the value, implying that it is in the range <code>0.95</code> to
  1980. <code>1.05</code>. Similarly, the precision indicated by the trailing zeros of <code>y</code>
  1981. indicates that the value is in the range <code>1.09995</code> to <code>1.10005</code>.
  1982. </p>
  1983. <p>
  1984. If we add the two lowest values in the ranges we have, <code>0.95 + 1.09995 = 2.04995</code>,
  1985. and if we add the two highest values we have, <code>1.05 + 1.10005 = 2.15005</code>, so the
  1986. range of the result of the addition implied by the precision of its operands is
  1987. <code>2.04995</code> to <code>2.15005</code>.
  1988. </p>
  1989. <p>
  1990. The result given by BigDecimal of <code>2.1000</code> however, indicates that the value is in
  1991. the range <code>2.09995</code> to <code>2.10005</code> and therefore the precision implied by
  1992. its trailing zeros may be misleading.
  1993. </p>
  1994. <p>
  1995. In the second example, the true range is <code>4.122744</code> to <code>4.157256</code> yet
  1996. the BigDecimal answer of <code>4.1400000</code> indicates a range of <code>4.13999995</code>
  1997. to <code>4.14000005</code>. Again, the precision implied by the trailing zeros may be
  1998. misleading.
  1999. </p>
  2000. <p>
  2001. This library, like binary floating point and most calculators, does not retain trailing
  2002. fractional zeros. Instead, the <code>toExponential</code>, <code>toFixed</code> and
  2003. <code>toPrecision</code> methods enable trailing zeros to be added if and when required.<br />
  2004. </p>
  2005. </div>
  2006. </body>
  2007. </html>