API.html 78 KB

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