1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900 |
- <!DOCTYPE HTML>
- <html>
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
- <meta name="Author" content="M Mclaughlin">
- <title>bignumber.js API</title>
- <style>
- html{font-family:sans-serif;font-size:100%}
- body{font-family:"Helvetica Neue",Helvetica,Arial,sans-serif;font-size:14px;
- line-height:1.65em;background:#fefff5;color:#000;min-height:100%;margin:0}
- .nav{background:#fff;position:fixed;top:0;bottom:0;left:0;width:180px;
- overflow-y:auto;padding:15px 0 30px 20px}
- div.container{width:600px;margin:50px 0 50px 240px}
- p{margin:0 0 1em;width:600px}
- pre,ul{margin:1em 0}
- h1,h2,h3,h4,h5{margin:0;padding:1.5em 0 0}
- h1,h2{padding:.75em 0}
- h1{font-size:2.5em;color:#fa6900}
- h2{font-size:2.25em;color:#fa6900}
- h3{font-size:1.75em;color:#69d2e7}
- h4{font-size:1.75em;color:#fa6900;padding-bottom:.75em}
- h5{font-size:1.2em;padding-bottom:.3em}
- h6{font-size:1.1em;margin:0;padding:0.5em 0}
- dd dt{font-size:1.2em}
- dt{padding-top:.5em}
- dd{padding-top:.35em}
- b{font-weight:700}
- a,a:visited{color:#444;text-decoration:none}
- a:active,a:hover{outline:0;color:#000}
- .nav a:hover{text-decoration:underline}
- .nav a,.nav b,.nav a:visited{display:block;color:#fa6900;font-weight:700;
- margin-top:15px}
- .nav b{color:#69d2e7;margin-top:20px;cursor:default;width:auto}
- ul{list-style-type:none;padding:0 0 0 20px}
- .nav ul{line-height:14px;padding-left:0;margin:5px 0 0}
- .nav ul a,.nav ul a:visited{display:inline;color:#000;font-family:Verdana,
- Geneva,sans-serif;font-size:11px;font-weight:400;margin:0}
- .inset,ul.inset{margin-left:20px}
- code.inset{font-size:.9em}
- .nav li{cursor:pointer;width:auto;margin:0 0 3px}
- span.alias{font-style:italic;margin-left:20px}
- table{border-collapse:collapse;border-spacing:0;border:2px solid #a7dbd8;
- margin:1.75em 0;padding:0}
- td,th{text-align:left;margin:0;padding:2px 5px;border:1px dotted #a7dbd8}
- th{border-top:2px solid #a7dbd8;border-bottom:2px solid #a7dbd8;color:#f38630}
- pre{background:#fff;white-space:pre-wrap;word-wrap:break-word;
- border-left:5px solid #a7dbd8;padding:1px 0 1px 15px;margin:1.2em 0}
- code,pre{font-family:Monaco,Consolas,"Lucida Console",monospace;
- font-weight:400}
- .end{margin-bottom:25px}
- .nav-title{color:#fa6900}
- .centre{text-align:center}
- .error-table{font-size:13px;width:100%}
- </style>
- </head>
- <body>
- <div class="nav">
- <a class='nav-title' href="#">bignumber.js</a>
- <b> CONSTRUCTOR </b>
- <ul>
- <li><a href="#bignumber">BigNumber</a></li>
- </ul>
- <a href="#methods">Methods</a>
- <ul>
- <li><a href="#config">config</a></li>
- <li>
- <ul class="inset">
- <li><a href="#decimal-places">DECIMAL_PLACES</a></li>
- <li><a href="#rounding-mode" >ROUNDING_MODE</a></li>
- <li><a href="#exponential-at">EXPONENTIAL_AT</a></li>
- <li><a href="#range" >RANGE</a></li>
- <li><a href="#errors" >ERRORS</a></li>
- <li><a href="#format" >FORMAT</a></li>
- </ul>
- </li>
- </ul>
- <a href="#constructor-properties">Properties</a>
- <ul>
- <li><a href="#round-up" >ROUND_UP</a></li>
- <li><a href="#round-down" >ROUND_DOWN</a></li>
- <li><a href="#round-ceil" >ROUND_CEIL</a></li>
- <li><a href="#round-floor" >ROUND_FLOOR</a></li>
- <li><a href="#round-half-up" >ROUND_HALF_UP</a></li>
- <li><a href="#round-half-down" >ROUND_HALF_DOWN</a></li>
- <li><a href="#round-half-even" >ROUND_HALF_EVEN</a></li>
- <li><a href="#round-half-ceil" >ROUND_HALF_CEIL</a></li>
- <li><a href="#round-half-floor">ROUND_HALF_FLOOR</a></li>
- </ul>
- <b> INSTANCE </b>
- <a href="#prototype-methods">Methods</a>
- <ul>
- <li><a href="#abs" >absoluteValue</a></li>
- <li><a href="#ceil" >ceil</a></li>
- <li><a href="#cmp" >comparedTo</a></li>
- <li><a href="#dp" >decimalPlaces</a></li>
- <li><a href="#div" >dividedBy</a></li>
- <li><a href="#divInt" >dividedToIntegerBy</a></li>
- <li><a href="#eq" >equals</a></li>
- <li><a href="#floor" >floor</a></li>
- <li><a href="#gt" >greaterThan</a></li>
- <li><a href="#gte" >greaterThanOrEqualTo</a></li>
- <li><a href="#isF" >isFinite</a></li>
- <li><a href="#isInt" >isInteger</a></li>
- <li><a href="#isNaN" >isNaN</a></li>
- <li><a href="#isNeg" >isNegative</a></li>
- <li><a href="#isZ" >isZero</a></li>
- <li><a href="#lt" >lessThan</a></li>
- <li><a href="#lte" >lessThanOrEqualTo</a></li>
- <li><a href="#minus" >minus</a></li>
- <li><a href="#mod" >modulo</a></li>
- <li><a href="#neg" >negated</a></li>
- <li><a href="#plus" >plus</a></li>
- <li><a href="#round" >round</a></li>
- <li><a href="#sqrt" >squareRoot</a></li>
- <li><a href="#times" >times</a></li>
- <li><a href="#toE" >toExponential</a></li>
- <li><a href="#toF" >toFixed</a></li>
- <li><a href="#toFo" >toFormat</a></li>
- <li><a href="#toFr" >toFraction</a></li>
- <li><a href="#toJSON" >toJSON</a></li>
- <li><a href="#toN" >toNumber</a></li>
- <li><a href="#pow" >toPower</a></li>
- <li><a href="#toP" >toPrecision</a></li>
- <li><a href="#toS" >toString</a></li>
- <li><a href="#valueOf">valueOf</a></li>
- </ul>
- <a href="#instance-properties">Properties</a>
- <ul>
- <li><a href="#coefficient">c : coefficient</a></li>
- <li><a href="#exponent" >e : exponent</a></li>
- <li><a href="#sign" >s : sign</a></li>
- </ul>
- <a href="#zero-nan-infinity">Zero, NaN & Infinity</a>
- <a href="#Errors">Errors</a>
- <a class='end' href="#faq">FAQ</a>
- </div>
- <div class="container">
- <h1>bignumber.js</h1>
- <p>A JavaScript library for arbitrary-precision arithmetic.</p>
- <p>
- <a href="https://github.com/MikeMcl/bignumber.js">Hosted on GitHub</a>.
- </p>
- <h2>API</h2>
- <p>
- In all examples below, <code>var</code> and semicolons are not shown, and
- if a commented-out value is in quotes it means <code>toString</code> has
- been called on the preceding expression.
- </p>
- <h3>CONSTRUCTOR</h3>
- <h5 id="bignumber">
- BigNumber<code class='inset'>BigNumber(value [, base]) ⇒
- <i>BigNumber</i>
- </code>
- </h5>
- <dl>
- <dt><code>value</code></dt>
- <dd>
- <i>number|string|BigNumber</i> : See <a href='#range'>RANGE</a> for
- range.
- </dd>
- <dd>
- A numeric value.
- </dd>
- <dd>
- Legitimate values include <code>±0</code>,
- <code>±Infinity</code> and <code>NaN</code>.
- </dd>
- <dd>
- Values of type <em>number</em> with more than 15 significant digits are
- considered invalid as calling <code>toString</code> or
- <code>valueOf</code> on such numbers may not result in the intended
- value.
- <pre>console.log( 823456789123456.3 ); // 823456789123456.2</pre>
- </dd>
- <dd>
- There is no limit to the number of digits of a value of type
- <em>string</em> (other than that of JavaScript's maximum array size).
- </dd>
- <dd>
- Decimal string values may be in exponential, as well as normal
- (fixed-point) notation. Non-decimal values must be in normal
- notation.
- </dd>
- <dd>
- String values in hexadecimal literal form, e.g. <code>'0xff'</code>, are
- invalid, and string values in octal literal form will be interpreted as
- decimals, e.g. <code>'011'</code> is interpreted as 11, not 9.
- </dd>
- <dd>Values in any base may have fraction digits.</dd>
- <dd>
- For bases from 10 to 36, lower and/or upper case letters can be used to
- represent values from 10 to 35. For bases above 36, <code>a-z</code>
- represents values from 10 to 35, <code>A-Z</code> from 36 to 61, and
- <code>$</code> and <code>_</code> represent 62 and 63 respectively <i>
- (this can be changed by editing the <code>DIGITS</code> variable near
- the top of the source file).</i>
- </dd>
- </dl>
- <dl>
- <dt><code>base</code></dt>
- <dd>
- <i>number</i> : integer, <code>2</code> to <code>64</code> inclusive
- </dd>
- <dd>The base of <code>value</code>.</dd>
- <dd>If <code>base</code> is omitted, or is <code>null</code> or undefined,
- base 10 is assumed.</dd>
- </dl>
- <p>Returns a new instance of a BigNumber object.</p>
- <p>
- If a base is specified, the value is rounded according to
- the current <a href='#decimal-places'><code>DECIMAL_PLACES</code></a> and
- <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> settings.
- Usefully, this means the decimal places of a decimal value
- passed to the constructor can be limited by explicitly specifying base 10.
- </p>
- <p>
- See <a href='#Errors'>Errors</a> for the treatment of an invalid
- <code>value</code> or <code>base</code>.
- </p>
- <pre>
- x = new BigNumber(9) // '9'
- y = new BigNumber(x) // '9'
- BigNumber(435.345) // 'new' is optional
- new BigNumber('5032485723458348569331745.33434346346912144534543')
- new BigNumber('4.321e+4') // '43210'
- new BigNumber('-735.0918e-430') // '-7.350918e-428'
- new BigNumber(Infinity) // 'Infinity'
- new BigNumber(NaN) // 'NaN'
- new BigNumber('.5') // '0.5'
- new BigNumber('+2') // '2'
- new BigNumber(-10110100.1, 2) // '-180.5'
- new BigNumber('123412421.234324', 5) // '607236.557696'
- new BigNumber('ff.8', 16) // '255.5'
- new BigNumber(9, 2)
- // Throws 'not a base 2 number' if ERRORS is true, otherwise 'NaN'
- new BigNumber(96517860459076817.4395)
- // Throws 'number type has more than 15 significant digits'
- // if ERRORS is true, otherwise '96517860459076820'
- new BigNumber('blurgh')
- // Throws 'not a number' if ERRORS is true, otherwise 'NaN'
- BigNumber.config({DECIMAL_PLACES: 5})
- new BigNumber(1.23456789) // '1.23456789'
- new BigNumber(1.23456789, 10) // '1.23457'</pre>
- <h4 id="methods">Methods</h4>
- <p>
- The <code>BigNumber</code> constructor has one added method,
- <code>config</code>, which configures the library-wide settings for
- arithmetic, formatting and errors.
- </p>
- <h5 id="config">
- config<code class='inset'>config([settings]) ⇒ <i>object</i></code>
- </h5>
- <i>
- Note: the settings can also be supplied as an argument list, see below.
- </i>
- <dl>
- <dt><code>settings</code></dt>
- <dd><i>object</i></dd>
- <dd>
- An object that contains some or all of the following properties:
- <dl>
- <dt id="decimal-places"><code><b>DECIMAL_PLACES</b></code></dt>
- <dd>
- <i>number</i> : integer, <code>0</code> to <code>1e+9</code>
- inclusive<br />
- Default value: <code>20</code>
- </dd>
- <dd>
- The <u>maximum</u> number of decimal places of the results of
- division, square root and base conversion operations, and power
- operations with negative exponents.<br />
- I.e. aside from the base conversion which may be involved with any
- method that accepts a base argument, the value is only relevant to
- the <code>dividedBy</code>, <code>squareRoot</code> and
- <code>toPower</code> methods.
- </dd>
- <dd>
- <pre>BigNumber.config({ DECIMAL_PLACES: 5 })
- BigNumber.config(5) // equivalent</pre>
- </dd>
- <dt id="rounding-mode"><code><b>ROUNDING_MODE</b></code></dt>
- <dd>
- <i>number</i> : integer, <code>0</code> to <code>8</code>
- inclusive<br />
- Default value: <code>4</code>
- <a href="#h-up">(<code>ROUND_HALF_UP</code>)</a>
- </dd>
- <dd>
- The rounding mode used in the above operations and by
- <a href='#round'><code>round</code></a>,
- <a href='#toE'><code>toExponential</code></a>,
- <a href='#toF'><code>toFixed</code></a>,
- <a href='#toFo'><code>toFormat</code></a> and
- <a href='#toP'><code>toPrecision</code></a>.
- </dd>
- <dd>
- The modes are available as enumerated properties of the BigNumber
- constructor.
- </dd>
- <dd>
- <pre>BigNumber.config({ ROUNDING_MODE: 0 })
- BigNumber.config(null, BigNumber.ROUND_UP) // equivalent</pre>
- </dd>
- <dt id="exponential-at"><code><b>EXPONENTIAL_AT</b></code></dt>
- <dd>
- <i>number</i> : integer, magnitude <code>0</code> to
- <code>1e+9</code> inclusive, or<br />
- <i>number</i>[] : [ integer -1e+9 to 0 inclusive, integer 0 to 1e+9
- inclusive ]<br />
- Default value: <code>[-7, 20]</code>
- </dd>
- <dd>
- The exponent value(s) at which <code>toString</code> returns
- exponential notation.
- </dd>
- <dd>
- If a single number is assigned, the value is the exponent magnitude.
- <br />
- If an array of two numbers is assigned then the first number is the
- negative exponent value at and beneath which exponential notation is
- used, and the second number is the positive exponent value at and
- above which the same.
- </dd>
- <dd>
- For example, to emulate JavaScript numbers in terms of the exponent
- values at which they begin to use exponential notation, use
- <code>[-7, 20]</code>.
- </dd>
- <dd>
- <pre>BigNumber.config({ EXPONENTIAL_AT: 2 })
- new BigNumber(12.3) // '12.3' e is only 1
- new BigNumber(123) // '1.23e+2'
- new BigNumber(0.123) // '0.123' e is only -1
- new BigNumber(0.0123) // '1.23e-2'
- BigNumber.config({ EXPONENTIAL_AT: [-7, 20] })
- new BigNumber(123456789) // '123456789' e is only 8
- new BigNumber(0.000000123) // '1.23e-7'
- // Almost never return exponential notation:
- BigNumber.config({ EXPONENTIAL_AT: 1e+9 })
- // Always return exponential notation:
- BigNumber.config({ EXPONENTIAL_AT: 0 })</pre>
- </dd>
- <dd>
- Regardless of the value of <code>EXPONENTIAL_AT</code>, the
- <code>toFixed</code> method will always return a value in
- normal notation and the <code>toExponential</code> method will
- always return a value in exponential form.
- </dd>
- <dd>
- Calling <code>toString</code> with a base argument, e.g.
- <code>toString(10)</code>, will also always return normal notation.
- </dd>
- <dt id="range"><code><b>RANGE</b></code></dt>
- <dd>
- <i>number</i> : integer, magnitude <code>1</code> to
- <code>1e+9</code> inclusive, or<br />
- <i>number</i>[] : [ integer -1e+9 to -1 inclusive, integer 1 to 1e+9
- inclusive ]<br />
- Default value: <code>[-1e+9, 1e+9]</code>
- </dd>
- <dd>
- The exponent value(s) beyond which overflow to Infinity and
- underflow to zero occurs.
- </dd>
- <dd>
- If a single number is assigned, it is the maximum exponent
- magnitude: values wth a positive exponent of greater magnitude
- become Infinity and those with a negative exponent of
- greater magnitude become zero.
- <dd>
- If an array of two numbers is assigned then the first number is the
- negative exponent limit and the second number is the positive
- exponent limit.
- </dd>
- <dd>
- For example, to emulate JavaScript numbers in terms of the exponent
- values at which they become zero and Infinity, use
- <code>[-324, 308]</code>.
- </dd>
- <dd>
- <pre>BigNumber.config({ RANGE: 500 })
- BigNumber.config().RANGE // [ -500, 500 ]
- new BigNumber('9.999e499') // '9.999e+499'
- new BigNumber('1e500') // 'Infinity'
- new BigNumber('1e-499') // '1e-499'
- new BigNumber('1e-500') // '0'
- BigNumber.config({ RANGE: [-3, 4] })
- new BigNumber(99999) // '99999' e is only 4
- new BigNumber(100000) // 'Infinity' e is 5
- new BigNumber(0.001) // '0.01' e is only -3
- new BigNumber(0.0001) // '0' e is -4</pre>
- </dd>
- <dd>
- The largest possible magnitude of a finite BigNumber is<br />
- 9.999...e+1000000000<br />
- The smallest possible magnitude of a non-zero BigNumber is<br />
- 1e-1000000000
- </dd>
- <dt id="errors"><code><b>ERRORS</b></code></dt>
- <dd>
- <i>boolean/number</i> : <code>true, false, 1 or 0</code><br />
- Default value: <code>true</code>
- </dd>
- <dd>
- The value that determines whether BigNumber Errors are thrown.<br />
- If <code>ERRORS</code> is false, this library will not throw errors.
- </dd>
- <dd>
- See <a href='#Errors'>Errors</a>.
- </dd>
- <dd>
- <pre>BigNumber.config({ ERRORS: false })</pre>
- </dd>
- <dt id="format"><code><b>FORMAT</b></code></dt>
- <dd><i>object</i></dd>
- <dd>
- The <code>FORMAT</code> object configures the format of the string
- returned by the <a href='#toFo'><code>toFormat</code></a> method.
- </dd>
- <dd>
- The example below shows the properties of the <code>FORMAT</code>
- object that are recognised, and their default values.
- </dd>
- <dd>
- Unlike when changing other settings, the values of the properties
- of the <code>FORMAT</code> object will not be checked for validity.
- The existing <code>FORMAT</code> object will simply be replaced by
- the object that is passed in. Note that all the properties shown
- below do not have to be included.
- </dd>
- <dd>
- See <a href='#toFo'><code>toFormat</code></a> for examples of usage.
- </dd>
- <dd><pre>
- BigNumber.config({
- FORMAT: {
- // the decimal separator
- decimalSeparator : '.',
- // the grouping separator of the integer part
- groupSeparator : ',',
- // the primary grouping size of the integer part
- groupSize : 3,
- // the secondary grouping size of the integer part
- secondaryGroupSize : 0,
- // the grouping separator of the fraction part
- fractionGroupSeparator : ' ',
- // the grouping size of the fraction part
- fractionGroupSize : 0
- }
- });</pre></dd>
- </dl>
- </dd>
- </dl>
- <p>
- <br />Returns an object with the above properties and their current
- values.
- </p>
- <p>
- If the value to be assigned to any of the above properties is
- <code>null</code> or undefined it is ignored. See
- <a href='#Errors'>Errors</a> for the treatment of invalid values.
- </p>
- <pre>
- BigNumber.config({
- DECIMAL_PLACES: 40,
- ROUNDING_MODE: BigNumber.ROUND_HALF_CEIL,
- EXPONENTIAL_AT: [-10, 20],
- RANGE: [-500, 500],
- ERRORS: true
- });
- // Alternatively but equivalently:
- BigNumber.config( 40, 7, [-10, 20], 500, 1 )
- obj = BigNumber.config();
- obj.ERRORS // true
- obj.RANGE // [-500, 500]</pre>
- <h4 id="constructor-properties">Properties</h4>
- <p>
- The library's enumerated rounding modes are stored as properties of the
- constructor.<br />
- They are not referenced internally by the library itself.
- </p>
- <p>
- Rounding modes 0 to 6 (inclusive) are the same as those of Java's
- BigDecimal class.
- </p>
- <table>
- <tr>
- <th>Property</th>
- <th>Value</th>
- <th>Description</th>
- </tr>
- <tr>
- <td id="round-up"><b>ROUND_UP</b></td>
- <td class='centre'>0</td>
- <td>Rounds away from zero</td>
- </tr>
- <tr>
- <td id="round-down"><b>ROUND_DOWN</b></td>
- <td class='centre'>1</td>
- <td>Rounds towards zero</td>
- </tr>
- <tr>
- <td id="round-ceil"><b>ROUND_CEIL</b></td>
- <td class='centre'>2</td>
- <td>Rounds towards Infinity</td>
- </tr>
- <tr>
- <td id="round-floor"><b>ROUND_FLOOR</b></td>
- <td class='centre'>3</td>
- <td>Rounds towards -Infinity</td>
- </tr>
- <tr>
- <td id="round-half-up"><b>ROUND_HALF_UP</b></td>
- <td class='centre'>4</td>
- <td>
- Rounds towards nearest neighbour.<br />
- If equidistant, rounds away from zero
- </td>
- </tr>
- <tr>
- <td id="round-half-down"><b>ROUND_HALF_DOWN</b></td>
- <td class='centre'>5</td>
- <td>
- Rounds towards nearest neighbour.<br />
- If equidistant, rounds towards zero
- </td>
- </tr>
- <tr>
- <td id="round-half-even"><b>ROUND_HALF_EVEN</b></td>
- <td class='centre'>6</td>
- <td>
- Rounds towards nearest neighbour.<br />
- If equidistant, rounds towards even neighbour
- </td>
- </tr>
- <tr>
- <td id="round-half-ceil"><b>ROUND_HALF_CEIL</b></td>
- <td class='centre'>7</td>
- <td>
- Rounds towards nearest neighbour.<br />
- If equidistant, rounds towards Infinity
- </td>
- </tr>
- <tr>
- <td id="round-half-floor"><b>ROUND_HALF_FLOOR</b></td>
- <td class='centre'>8</td>
- <td>
- Rounds towards nearest neighbour.<br />
- If equidistant, rounds towards -Infinity
- </td>
- </tr>
- </table>
- <pre>
- BigNumber.config({ ROUNDING_MODE: BigNumber.ROUND_CEIL })
- BigNumber.config({ ROUNDING_MODE: 2 }) // equivalent</pre>
- <h3>INSTANCE</h3>
- <h4 id="prototype-methods">Methods</h4>
- <p>
- The methods inherited by a BigNumber instance from its constructor's
- prototype object.
- </p>
- <p>
- A BigNumber is immutable in the sense that it is not changed by its
- methods.
- </p>
- <p>
- The treatment of ±<code>0</code>, ±<code>Infinity</code> and
- <code>NaN</code> is consistent with how JavaScript treats these values.
- </p>
- <p>
- Many method names have a shorter alias.<br />
- Internally, the library always uses the shorter method names.
- </p>
- <h5 id="abs">
- absoluteValue<code class='inset'>.abs() ⇒ <i>BigNumber</i></code>
- </h5>
- <p>
- Returns a BigNumber whose value is the absolute value, i.e. the magnitude,
- of this BigNumber.
- </p>
- <pre>
- x = new BigNumber(-0.8)
- y = x.absoluteValue() // '0.8'
- z = y.abs() // '0.8'</pre>
- <h5 id="ceil">
- ceil<code class='inset'>.ceil() ⇒ <i>BigNumber</i></code>
- </h5>
- <p>
- Returns a BigNumber whose value is the value of this BigNumber rounded to
- a whole number in the direction of Infinity.
- </p>
- <pre>
- x = new BigNumber(1.3)
- x.ceil() // '2'
- y = new BigNumber(-1.8)
- y.ceil() // '-1'</pre>
- <h5 id="cmp">
- comparedTo<code class='inset'>.cmp(n [, base]) ⇒ <i>number</i></code>
- </h5>
- <p>
- <code>n</code> : <i>number|string|BigNumber</i><br />
- <code>base</code> : <i>number</i><br />
- <i>
- See <a href="#bignumber">constructor</a> for further parameter details.
- </i>
- </p>
- <table>
- <tr>
- <th>Returns</th>
- <th colspan=2> </th>
- </tr>
- <tr>
- <td class='centre'>1</td>
- <td>
- If the value of this BigNumber is greater than the value of
- <code>n</code>
- </td>
- </tr>
- <tr>
- <td class='centre'>-1</td>
- <td>
- If the value of this BigNumber is less than the value of
- <code>n</code>
- </td>
- </tr>
- <tr>
- <td class='centre'>0</td>
- <td>If this BigNumber and <code>n</code> have the same value</td>
- </tr>
- <tr>
- <td class='centre'>null</td>
- <td>
- if the value of either this BigNumber or <code>n</code> is
- <code>NaN</code>
- </td>
- </tr>
- </table>
- <pre>
- x = new BigNumber(Infinity)
- y = new BigNumber(5)
- x.comparedTo(y) // 1
- x.comparedTo(x.minus(1)) // 0
- y.cmp(NaN) // null
- y.cmp('110', 2) // -1</pre>
- <h5 id="dp">
- decimalPlaces<code class='inset'>.dp() ⇒ <i>number</i></code>
- </h5>
- <p>
- Return the number of decimal places of the value of this BigNumber, or
- <code>null</code> if the value of this BigNumber is
- <code>±Infinity</code> or <code>NaN</code>.
- </p>
- <pre>
- x = new BigNumber(123.45)
- x.decimalPlaces() // 2
- y = new BigNumber('9.9e-101')
- y.dp() // 102</pre>
- <h5 id="div">
- dividedBy<code class='inset'>.div(n [, base]) ⇒
- <i>BigNumber</i></code>
- </h5>
- <p>
- <code>n</code> : <i>number|string|BigNumber</i><br />
- <code>base</code> : <i>number</i><br />
- <i>
- See <a href="#bignumber">constructor</a> for further parameter details.
- </i>
- </p>
- <p>
- Returns a BigNumber whose value is the value of this BigNumber divided by
- <code>n</code>, rounded according to the current
- <a href='#decimal-places'><code>DECIMAL_PLACES</code></a> and
- <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> settings.
- </p>
- <pre>
- x = new BigNumber(355)
- y = new BigNumber(113)
- x.dividedBy(y) // '3.14159292035398230088'
- x.div(5) // '71'
- x.div(47, 16) // '5'</pre>
- <h5 id="divInt">
- dividedToIntegerBy<code class='inset'>.divToInt(n [, base]) ⇒
- <i>BigNumber</i></code>
- </h5>
- <p>
- <code>n</code> : <i>number|string|BigNumber</i><br />
- <code>base</code> : <i>number</i><br />
- <i>
- See <a href="#bignumber">constructor</a> for further parameter details.
- </i>
- </p>
- <p>
- Return a BigNumber whose value is the integer part (truncating towards
- zero) of dividing the value of this BigNumber by <code>n</code>.
- </p>
- <pre>
- x = new BigNumber(5)
- y = new BigNumber(3)
- x.dividedToIntegerBy(y) // '1'
- x.divToInt(0.7) // '7'
- x.divToInt('0.f', 16) // '5'</pre>
- <h5 id="eq">
- equals<code class='inset'>.eq(n [, base]) ⇒ <i>boolean</i></code>
- </h5>
- <p>
- <code>n</code> : <i>number|string|BigNumber</i><br />
- <code>base</code> : <i>number</i><br />
- <i>
- See <a href="#bignumber">constructor</a> for further parameter details.
- </i>
- </p>
- <p>
- Returns <code>true</code> if the value of this BigNumber equals the value
- of <code>n</code>, otherwise returns <code>false</code>.<br />
- As with JavaScript, NaN does not equal NaN.
- <br />Note : This method uses the <code>comparedTo</code> method
- internally.
- </p>
- <pre>
- 0 === 1e-324 // true
- x = new BigNumber(0)
- x.equals('1e-324') // false
- BigNumber(-0).eq(x) // true ( -0 === 0 )
- BigNumber(255).eq('ff', 16) // true
- y = new BigNumber(NaN)
- y.equals(NaN) // false</pre>
- <h5 id="floor">
- floor<code class='inset'>.floor() ⇒
- <i>BigNumber</i></code>
- </h5>
- <p>
- Returns a BigNumber whose value is the value of this BigNumber rounded to
- a whole number in the direction of -Infinity.
- </p>
- <pre>
- x = new BigNumber(1.8)
- x.floor() // '1'
- y = new BigNumber(-1.3)
- y.floor() // '-2'</pre>
- <h5 id="gt">
- greaterThan<code class='inset'>.gt(n [, base]) ⇒
- <i>boolean</i></code>
- </h5>
- <p>
- <code>n</code> : <i>number|string|BigNumber</i><br />
- <code>base</code> : <i>number</i><br />
- <i>
- See <a href="#bignumber">constructor</a> for further parameter details.
- </i>
- </p>
- <p>
- Returns <code>true</code> if the value of this BigNumber is greater than
- the value of <code>n</code>, otherwise returns <code>false</code>.<br />
- Note : This method uses the <code>comparedTo</code> method internally.
- </p>
- <pre>
- 0.1 > (0.3 - 0.2) // true
- x = new BigNumber(0.1)
- x.greaterThan(BigNumber(0.3).minus(0.2)) // false
- BigNumber(0).gt(x) // false
- BigNumber(11, 3).gt(11.1, 2) // true</pre>
- <h5 id="gte">
- greaterThanOrEqualTo<code class='inset'>.gte(n [, base]) ⇒
- <i>boolean</i></code>
- </h5>
- <p>
- <code>n</code> : <i>number|string|BigNumber</i><br />
- <code>base</code> : <i>number</i><br />
- <i>
- See <a href="#bignumber">constructor</a> for further parameter details.
- </i>
- </p>
- <p>
- Returns <code>true</code> if the value of this BigNumber is greater than
- or equal to the value of <code>n</code>, otherwise returns
- <code>false</code>.<br />
- Note : This method uses the <code>comparedTo</code> method internally.
- </p>
- <pre>
- (0.3 - 0.2) >= 0.1 // false
- x = new BigNumber(0.3).minus(0.2)
- x.greaterThanOrEqualTo(0.1) // true
- BigNumber(1).gte(x) // true
- BigNumber(10, 18).gte('i', 36) // true</pre>
- <h5 id="isF">
- isFinite<code class='inset'>.isFinite() ⇒ <i>boolean</i></code>
- </h5>
- <p>
- Returns <code>true</code> if the value of this BigNumber is a finite
- number, otherwise returns <code>false</code>.<br />
- The only possible non-finite values of a BigNumber are NaN, Infinity and
- -Infinity.
- </p>
- <pre>
- x = new BigNumber(1)
- x.isFinite() // true
- y = new BigNumber(Infinity)
- y.isFinite() // false</pre>
- <p>
- Note: The native method <code>isFinite()</code> can be used if
- <code>n <= Number.MAX_VALUE</code>.
- </p>
- <h5 id="isInt">
- isInteger<code class='inset'>.isInt() ⇒ <i>boolean</i></code>
- </h5>
- <p>
- Returns <code>true</code> if the value of this BigNumber is a whole
- number, otherwise returns <code>false</code>.
- </p>
- <pre>
- x = new BigNumber(1)
- x.isInteger() // true
- y = new BigNumber(123.456)
- y.isInt() // false</pre>
- <h5 id="isNaN">
- isNaN<code class='inset'>.isNaN() ⇒ <i>boolean</i></code>
- </h5>
- <p>
- Returns <code>true</code> if the value of this BigNumber is NaN, otherwise
- returns <code>false</code>.<br />
- </p>
- <pre>
- x = new BigNumber(NaN)
- x.isNaN() // true
- y = new BigNumber('Infinity')
- y.isNaN() // false</pre>
- <p>
- Note: The native method <code>isNaN()</code> can also be used.
- </p>
- <h5 id="isNeg">
- isNegative<code class='inset'>.isNeg() ⇒ <i>boolean</i></code>
- </h5>
- <p>
- Returns <code>true</code> if the value of this BigNumber is negative,
- otherwise returns <code>false</code>.<br />
- </p>
- <pre>
- x = new BigNumber(-0)
- x.isNegative() // true
- y = new BigNumber(2)
- y.isNeg // false</pre>
- <p>
- Note: <code>n < 0</code> can be used if
- <code>n <= -Number.MIN_VALUE</code>.
- </p>
- <h5 id="isZ">
- isZero<code class='inset'>.isZero() ⇒ <i>boolean</i></code>
- </h5>
- <p>
- Returns <code>true</code> if the value of this BigNumber is zero or minus
- zero, otherwise returns <code>false</code>.<br />
- </p>
- <pre>
- x = new BigNumber(-0)
- x.isZero() && x.isNeg() // true
- y = new BigNumber(Infinity)
- y.isZero() // false</pre>
- <p>
- Note: <code>n == 0</code> can be used if
- <code>n >= Number.MIN_VALUE</code>.
- </p>
- <h5 id="lt">
- lessThan<code class='inset'>.lt(n [, base]) ⇒ <i>boolean</i></code>
- </h5>
- <p>
- <code>n</code> : <i>number|string|BigNumber</i><br />
- <code>base</code> : <i>number</i><br />
- <i>
- See <a href="#bignumber">constructor</a> for further parameter details.
- </i>
- </p>
- <p>
- Returns <code>true</code> if the value of this BigNumber is less than the
- value of <code>n</code>, otherwise returns <code>false</code>.<br />
- Note : This method uses the <code>comparedTo</code> method internally.
- </p>
- <pre>
- (0.3 - 0.2) < 0.1 // true
- x = new BigNumber(0.3).minus(0.2)
- x.lessThan(0.1) // false
- BigNumber(0).lt(x) // true
- BigNumber(11.1, 2).lt(11, 3) // true</pre>
- <h5 id="lte">
- lessThanOrEqualTo<code class='inset'>.lte(n [, base]) ⇒
- <i>boolean</i></code>
- </h5>
- <p>
- <code>n</code> : <i>number|string|BigNumber</i><br />
- <code>base</code> : <i>number</i><br />
- <i>
- See <a href="#bignumber">constructor</a> for further parameter details.
- </i>
- </p>
- <p>
- Returns <code>true</code> if the value of this BigNumber is less than or
- equal to the value of <code>n</code>, otherwise returns
- <code>false</code>.<br />
- Note : This method uses the <code>comparedTo</code> method internally.
- </p>
- <pre>
- 0.1 <= (0.3 - 0.2) // false
- x = new BigNumber(0.1)
- x.lessThanOrEqualTo(BigNumber(0.3).minus(0.2)) // true
- BigNumber(-1).lte(x) // true
- BigNumber(10, 18).lte('i', 36) // true</pre>
- <h5 id="minus">
- minus<code class='inset'>.minus(n [, base]) ⇒ <i>BigNumber</i></code>
- </h5>
- <p>
- <code>n</code> : <i>number|string|BigNumber</i><br />
- <code>base</code> : <i>number</i><br />
- <i>
- See <a href="#bignumber">constructor</a> for further parameter details.
- </i>
- </p>
- <p>
- Returns a BigNumber whose value is the value of this BigNumber minus
- <code>n</code>.
- </p>
- <pre>
- 0.3 - 0.1 // 0.19999999999999998
- x = new BigNumber(0.3)
- x.minus(0.1) // '0.2'
- x.minus(0.6, 20) // '0'</pre>
- <h5 id="mod">
- modulo<code class='inset'>.mod(n [, base]) ⇒ <i>BigNumber</i></code>
- </h5>
- <p>
- <code>n</code> : <i>number|string|BigNumber</i><br />
- <code>base</code> : <i>number</i><br />
- <i>
- See <a href="#bignumber">constructor</a> for further parameter details.
- </i>
- </p>
- <p>
- Returns a BigNumber whose value is the value of this BigNumber modulo
- <code>n</code>, i.e. the integer remainder of dividing this BigNumber by
- <code>n</code>.
- </p>
- <p>
- The result will have the same sign as this BigNumber, and it will match
- that of JavaScript's % operator (within the limits of its precision) and
- BigDecimal's remainder method.
- </p>
- <pre>
- 1 % 0.9 // 0.09999999999999998
- x = new BigNumber(1)
- x.modulo(0.9) // '0.1'
- y = new BigNumber(33)
- y.mod('a', 33) // '3'</pre>
- <h5 id="neg">
- negated<code class='inset'>.neg() ⇒ <i>BigNumber</i></code>
- </h5>
- <p>
- Returns a BigNumber whose value is the value of this BigNumber negated,
- i.e. multiplied by -1.
- </p>
- <pre>
- x = new BigNumber(1.8)
- x.negated() // '-1.8'
- y = new BigNumber(-1.3)
- y.neg() // '1.3'</pre>
- <h5 id="plus">
- plus<code class='inset'>.plus(n [, base]) ⇒ <i>BigNumber</i></code>
- </h5>
- <p>
- <code>n</code> : <i>number|string|BigNumber</i><br />
- <code>base</code> : <i>number</i><br />
- <i>
- See <a href="#bignumber">constructor</a> for further parameter details.
- </i>
- </p>
- <p>
- Returns a BigNumber whose value is the value of this BigNumber plus
- <code>n</code>.
- </p>
- <pre>
- 0.1 + 0.2 // 0.30000000000000004
- x = new BigNumber(0.1)
- y = x.plus(0.2) // '0.3'
- BigNumber(0.7).plus(x).plus(y) // '1'
- x.plus('0.1', 8) // '0.225'</pre>
- <h5 id="round">
- round
- <code class='inset'>.round([dp [, rm]]) ⇒ <i>BigNumber</i></code>
- </h5>
- <p>
- <code>dp</code> : <i>number</i> : integer, 0 to 1e+9 inclusive<br />
- <code>rm</code> : <i>number</i> : integer, 0 to 8 inclusive
- </p>
- <p>
- Returns a BigNumber whose value is the value of this BigNumber rounded by
- rounding mode <code>rm</code> to a maximum of <code>dp</code> decimal
- places.
- </p>
- <p>
- if <code>dp</code> is omitted, or is <code>null</code> or undefined, the
- return value is <code>n</code> rounded to a whole number.
- </p>
- <p>
- if <code>rm</code> is omitted, or is <code>null</code> or undefined, the
- current <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> setting is
- used.
- </p>
- <p>
- See <a href='#Errors'>Errors</a> for the treatment of other non-integer or
- out of range <code>dp</code> or <code>rm</code> values.
- </p>
- <pre>
- x = 1234.56
- Math.round(x) // 1235
- y = new BigNumber(x)
- y.round() // '1235'
- y.round(1) // '1234.6'
- y.round(2) // '1234.56'
- y.round(10) // '1234.56'
- y.round(0, 1) // '1234'
- y.round(0, 6) // '1235'
- y.round(1, 1) // '1234.5'
- y.round(1, BigNumber.ROUND_HALF_EVEN) // '1234.6'
- y // '1234.56'</pre>
- <h5 id="sqrt">
- squareRoot<code class='inset'>.sqrt() ⇒ <i>BigNumber</i></code>
- </h5>
- <p>
- Returns a BigNumber whose value is the square root of this BigNumber,
- correctly rounded according to the current
- <a href='#decimal-places'><code>DECIMAL_PLACES</code></a> and
- <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> settings.
- </p>
- <pre>
- x = new BigNumber(16)
- x.squareRoot() // '4'
- y = new BigNumber(3)
- y.sqrt() // '1.73205080756887729353'</pre>
- <h5 id="times">
- times<code class='inset'>.times(n [, base]) ⇒ <i>BigNumber</i></code>
- </h5>
- <p>
- <code>n</code> : <i>number|string|BigNumber</i><br />
- <code>base</code> : <i>number</i><br />
- <i>
- See <a href="#bignumber">constructor</a> for further parameter details.
- </i>
- </p>
- <p>
- Returns a BigNumber whose value is the value of this BigNumber times
- <code>n</code>.
- </p>
- <pre>
- 0.6 * 3 // 1.7999999999999998
- x = new BigNumber(0.6)
- y = x.times(3) // '1.8'
- BigNumber('7e+500').times(y) // '1.26e+501'
- x.times('-a', 16) // '-6'</pre>
- <h5 id="toE">
- toExponential<code class='inset'>.toExponential([dp]) ⇒ <i>string</i></code>
- </h5>
- <p>
- <code>dp</code> : <i>number</i> : integer, 0 to 1e+9 inclusive
- </p>
- <p>
- Returns a string representing the value of this BigNumber in exponential
- notation to the specified decimal places, i.e with one digit before the
- decimal point and <code>dp</code> digits after it. If rounding
- is necessary, the current
- <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> is used.
- </p>
- <p>
- If the value of this BigNumber in exponential notation has fewer fraction
- digits then is specified by <code>dp</code>, the return value will be
- appended with zeros accordingly.
- </p>
- <p>
- If <code>dp</code> is omitted, or is <code>null</code> or undefined, the
- number of digits after the decimal point defaults to the minimum number of
- digits necessary to represent the value exactly.
- </p>
- <p>
- See <a href='#Errors'>Errors</a> for the treatment of other
- non-integer or out of range <code>dp</code> values.
- </p>
- <pre>
- x = 45.6
- y = new BigNumber(x)
- x.toExponential() // '4.56e+1'
- y.toExponential() // '4.56e+1'
- x.toExponential(0) // '5e+1'
- y.toExponential(0) // '5e+1'
- x.toExponential(1) // '4.6e+1'
- y.toExponential(1) // '4.6e+1'
- x.toExponential(3) // '4.560e+1'
- y.toExponential(3) // '4.560e+1'</pre>
- <h5 id="toF">
- toFixed<code class='inset'>.toFixed([dp]) ⇒ <i>string</i></code>
- </h5>
- <p>
- <code>dp</code> : <i>number</i> : integer, 0 to 1e+9 inclusive
- </p>
- <p>
- Returns a string representing the value of this BigNumber in normal
- notation to the specified fixed number of decimal places, i.e. with
- <code>dp</code> digits after the decimal point. If rounding is necessary,
- the current <a href='#rounding-mode'><code>ROUNDING_MODE</code></a>
- setting is used.
- </p>
- <p>
- If the value of this BigNumber in normal notation has fewer fraction
- digits then is specified by <code>dp</code>, the return value will be
- appended with zeros accordingly.
- </p>
- <p>
- Unlike <code>Number.prototype.toFixed</code>, which returns
- exponential notation if a number is greater or equal to 10<sup>21</sup>,
- this method will always return normal notation.
- </p>
- <p>
- If <code>dp</code> is omitted, or is <code>null</code> or undefined, then
- the return value is the same as <code>n.toString()</code>. This is also
- unlike <code>Number.prototype.toFixed</code>, which returns the value to
- zero decimal places.
- </p>
- <p>
- See <a href='#Errors'>Errors</a> for the treatment of other
- non-integer or out of range <code>dp</code> values.
- </p>
- <pre>
- x = 45.6
- y = new BigNumber(x)
- x.toFixed() // '46'
- y.toFixed() // '45.6'
- y.toFixed(0) // '46'
- x.toFixed(3) // '45.600'
- y.toFixed(3) // '45.600'</pre>
- <h5 id="toFo">
- toFormat<code class='inset'>.toFormat([dp]) ⇒ <i>string</i></code>
- </h5>
- <p>
- <code>dp</code> : <i>number</i> : integer, 0 to 1e+9 inclusive
- </p>
- <p>
- Returns a string representing the value of this BigNumber in normal
- notation rounded to <code>dp</code> decimal places using the current
- <a href='#rounding-mode'><code>ROUNDING_MODE</code></a>, and formatted
- according to the properties of the
- <a href='#format'><code>FORMAT</code></a> object.
- </p>
- <p>
- See the examples below for the properties of the
- <a href='#format'><code>FORMAT</code></a> object, their types and their
- usage.
- </p>
- <p>
- If <code>dp</code> is omitted or is <code>null</code> or undefined, then
- the return value is not rounded to a fixed number of decimal places.
- </p>
- <pre>
- format = {
- decimalSeparator: '.',
- groupSeparator: ',',
- groupSize: 3,
- secondaryGroupSize: 0,
- fractionGroupSeparator: ' ',
- fractionGroupSize: 0
- }
- BigNumber.config({ FORMAT: format })
- x = new BigNumber('123456789.123456789')
- x.toFormat() // '123,456,789.123456789'
- x.toFormat(1) // '123,456,789.1'
- // If a reference to the object assigned to FORMAT has been retained,
- // the format properties can be changed directly
- format.groupSeparator = ' '
- format.fractionGroupSize = 5
- x.toFormat() // '123 456 789.12345 6789'
- BigNumber.config({
- FORMAT: {
- decimalSeparator = ',',
- groupSeparator = '.',
- groupSize = 3,
- secondaryGroupSize = 2
- }
- })
- x.toFormat(6) // '12.34.56.789,123'</pre>
- <h5 id="toFr">
- toFraction<code class='inset'>.toFraction([max_denominator]) ⇒
- <i>[string, string]</i></code>
- </h5>
- <p>
- <code>max_denominator</code> : <i>number|string|BigNumber</i> :
- integer >= 1 and < Infinity
- </p>
- <p>
- Returns a string array representing the value of this BigNumber as a
- simple fraction with an integer numerator and an integer denominator. The
- denominator will be a positive non-zero value less than or equal to
- <code>max_denominator</code>.
- </p>
- <p>
- If a maximum denominator is not specified, or is <code>null</code> or
- undefined, the denominator will be the lowest value necessary to represent
- the number exactly.
- </p>
- <p>
- See <a href='#Errors'>Errors</a> for the treatment of other non-integer or
- out of range <code>max_denominator</code> values.
- </p>
- <pre>
- x = new BigNumber(1.75)
- x.toFraction() // '7, 4'
- pi = new BigNumber('3.14159265358')
- pi.toFraction() // '157079632679,50000000000'
- pi.toFraction(100000) // '312689, 99532'
- pi.toFraction(10000) // '355, 113'
- pi.toFraction(100) // '311, 99'
- pi.toFraction(10) // '22, 7'
- pi.toFraction(1) // '3, 1'</pre>
- <h5 id="toJSON">
- toJSON<code class='inset'>.toJSON() ⇒ <i>string</i></code>
- </h5>
- <p>
- As <code>toString</code>.
- </p>
- <pre>
- x = new BigNumber('123.4e+567')
- str = JSON.stringify(x)</pre>
- <h5 id="toN">
- toNumber<code class='inset'>.toNumber() ⇒ <i>number</i></code>
- </h5>
- <p>Returns the value of this BigNumber as a number primitive.</p>
- <p>
- Type coercion with, for example, JavaScript's unary plus operator can
- alternatively be used, but then a BigNumber with the value minus zero will
- convert to positive zero.
- </p>
- <pre>
- x = new BigNumber(456.789)
- x.toNumber() // 456.789
- +x // 456.789
- y = new BigNumber('45987349857634085409857349856430985')
- y.toNumber() // 4.598734985763409e+34
- z = new BigNumber(-0)
- 1 / +z // Infinity
- 1 / z.toNumber() // -Infinity</pre>
- <h5 id="pow">
- toPower<code class='inset'>.pow(n) ⇒ <i>BigNumber</i></code>
- </h5>
- <p>
- <code>n</code> : <i>number</i> : integer, -1e+6 to 1e+6 inclusive
- </p>
- <p>
- Returns a BigNumber whose value is the value of this BigNumber raised to
- the power <code>n</code>.
- </p>
- <p>
- If <code>n</code> is negative the result is rounded according to the
- current <a href='#decimal-places'><code>DECIMAL_PLACES</code></a> and
- <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> settings.
- </p>
- <p>
- If <code>n</code> is not an integer or is out of range:
- </p>
- <p class='inset'>
- If <code>ERRORS</code> is <code>true</code> a BigNumber
- Error is thrown,<br />
- else if <code>n</code> is greater than 1e+6, it is interpreted as
- <code>Infinity</code>;<br />
- else if <code>n</code> is less than -1e+6, it is interpreted as
- <code>-Infinity</code>;<br />
- else if <code>n</code> is otherwise a number, it is truncated to an
- integer;<br />
- else it is interpreted as <code>NaN</code>.
- </p>
- <p>
- Note: High value exponents may cause this method to be slow to return.
- </p>
- <pre>
- Math.pow(0.7, 2) // 0.48999999999999994
- x = new BigNumber(0.7)
- x.toPower(2) // '0.49'
- BigNumber(3).pow(-2) // '0.11111111111111111111'
- new BigNumber(2).toPower(100000).toString().length // 30111</pre>
- <h5 id="toP">
- toPrecision<code class='inset'>.toPrecision([sd]) ⇒
- <i>string</i></code>
- </h5>
- <p>
- <code>sd</code> : <i>number</i> : integer, 1 to 1e+9 inclusive
- </p>
- <p>
- Returns a string representing the value of this BigNumber to
- <code>sd</code> significant digits. If rounding is necessary, the
- current <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> setting
- is used.
- </p>
- <p>
- If <code>sd</code> is less than the number of digits necessary to
- represent the integer part of the value in normal notation, then
- exponential notation is used.
- </p>
- <p>
- If <code>sd</code> is omitted, or is <code>null</code> or undefined,
- then the return value is the same as <code>n.toString()</code>.
- </p>
- <p>
- See <a href='#Errors'>Errors</a> for the treatment of other
- non-integer or out of range <code>sd</code> values.
- </p>
- <pre>
- x = 45.6
- y = new BigNumber(x)
- x.toPrecision() // '45.6'
- y.toPrecision() // '45.6'
- x.toPrecision(1) // '5e+1'
- y.toPrecision(1) // '5e+1'
- x.toPrecision(5) // '45.600'
- y.toPrecision(5) // '45.600'</pre>
- <h5 id="toS">
- toString<code class='inset'>.toString([base]) ⇒ <i>string</i></code>
- </h5>
- <p>
- <code>base</code> : <i>number</i> : integer, 2 to 64 inclusive
- </p>
- <p>
- Returns a string representing the value of this BigNumber in the specified
- base, or base 10 if <code>base</code> is omitted. For bases above 10,
- values from 10 to 35 are represented by <code>a-z</code> (as with
- <code>Number.toString</code>), 36 to 61 by <code>A-Z</code>, and 62 and 63
- by <code>$</code> and <code>_</code> respectively.
- </p>
- <p>
- If a base is specified the value is rounded according to the current
- <a href='#decimal-places'><code>DECIMAL_PLACES</code></a>
- and <a href='#rounding-mode'><code>ROUNDING_MODE</code></a> settings.
- </p>
- <p>
- If a base is not specified, and this BigNumber has a positive
- exponent that is equal to or greater than the positive component of the
- current <a href="#exponential-at"><code>EXPONENTIAL_AT</code></a> setting,
- or a negative exponent equal to or less than the negative component of the
- setting, then exponential notation is returned.
- </p>
- <p>
- If <code>base</code> is <code>null</code> or undefined it is ignored.
- <br />
- See <a href='#Errors'>Errors</a> for the treatment of other non-integer or
- out of range <code>base</code> values.
- </p>
- <pre>
- x = new BigNumber(750000)
- x.toString() // '750000'
- BigNumber.config({ EXPONENTIAL_AT: 5 })
- x.toString() // '7.5e+5'
- y = new BigNumber(362.875)
- y.toString(2) // '101101010.111'
- y.toString(9) // '442.77777777777777777778'
- y.toString(32) // 'ba.s'
- BigNumber.config({ DECIMAL_PLACES: 4 });
- z = new BigNumber('1.23456789')
- z.toString() // '1.23456789'
- z.toString(10) // '1.2346'</pre>
- <h5 id="valueOf">
- valueOf<code class='inset'>.valueOf() ⇒ <i>string</i></code>
- </h5>
- <p>
- As <code>toString</code>, but does not accept a base argument.
- </p>
- <pre>
- x = new BigNumber('1.777e+457')
- x.valueOf() // '1.777e+457'</pre>
- <h4 id="instance-properties">Properties</h4>
- <p>
- A BigNumber is an object with three properties:
- </p>
- <table>
- <tr>
- <th>Property</th>
- <th>Description</th>
- <th>Type</th>
- <th>Value</th>
- </tr>
- <tr>
- <td class='centre' id='coefficient'><b>c</b></td>
- <td>coefficient<sup>*</sup></td>
- <td><i>number</i><code>[]</code></td>
- <td> Array of base 1e14 numbers</td>
- </tr>
- <tr>
- <td class='centre' id='exponent'><b>e</b></td>
- <td>exponent</td>
- <td><i>number</i></td>
- <td>Integer, -1e9 to 1e9 inclusive</td>
- </tr>
- <tr>
- <td class='centre' id='sign'><b>s</b></td>
- <td>sign</td>
- <td><i>number</i></td>
- <td>-1 or 1</td>
- </tr>
- </table>
- <p><sup>*</sup>significand</p>
- <p>
- The value of any of the three properties may also be <code>null</code>.
- </p>
- <p>
- From version 2 of this library, the value of the coefficient of a
- BigNumber is stored in a normalised base <code>100000000000000</code>
- floating point format, as opposed to the base 10 format used in version 1.
- </p>
- <p>
- This change means the properties of a BigNumber are now best considered to
- be read-only. Previously it was acceptable to change the exponent of a
- BigNumber by writing to its exponent property directly, but this is no
- longer recommended as the number of digits in the first element of the
- coefficient array is dependent on the exponent, so the coefficient would
- also need to be altered.
- </p>
- <p>
- Note that, as with JavaScript numbers, the original exponent and
- fractional trailing zeros are not preserved.
- </p>
- <pre>x = new BigNumber(0.123) // '0.123'
- x.toExponential() // '1.23e-1'
- x.c // '1,2,3'
- x.e // -1
- x.s // 1
- y = new Number(-123.4567000e+2) // '-12345.67'
- y.toExponential() // '-1.234567e+4'
- z = new BigNumber('-123.4567000e+2') // '-12345.67'
- z.toExponential() // '-1.234567e+4'
- z.c // '1,2,3,4,5,6,7'
- z.e // 4
- z.s // -1</pre>
- <h4 id="zero-nan-infinity">Zero, NaN and Infinity</h4>
- <p>
- The table below shows how ±0, NaN and ±Infinity are stored.
- </p>
- <table>
- <tr>
- <th> </th>
- <th class='centre'>c</th>
- <th class='centre'>e</th>
- <th class='centre'>s</th>
- </tr>
- <tr>
- <td>±0</td>
- <td><code>[0]</code></td>
- <td><code>0</code></td>
- <td><code>±1</code></td>
- </tr>
- <tr>
- <td>NaN</td>
- <td><code>null</code></td>
- <td><code>null</code></td>
- <td><code>null</code></td>
- </tr>
- <tr>
- <td>±Infinity</td>
- <td><code>null</code></td>
- <td><code>null</code></td>
- <td><code>±1</code></td>
- </tr>
- </table>
- <pre>
- x = new Number(-0) // 0
- 1 / x == -Infinity // true
- y = new BigNumber(-0) // '0'
- y.c // '0' ( [0].toString() )
- y.e // 0
- y.s // -1</pre>
- <h4 id='Errors'>Errors</h4>
- <p>
- The errors that are thrown are generic <code>Error</code> objects with
- <code>name</code> <i>BigNumber Error</i>. The table below shows the errors
- that may be thrown if <code>ERRORS</code> is <code>true</code>, and the
- action taken if <code>ERRORS</code> is <code>false</code>.
- </p>
- <table class='error-table'>
- <tr>
- <th>Method(s)</th>
- <th>ERRORS: true<br />Throw BigNumber Error</th>
- <th>ERRORS: false<br />Action on invalid argument</th>
- </tr>
- <tr>
- <td rowspan=5><code>
- BigNumber<br />
- comparedTo<br />
- dividedBy<br />
- dividedToIntegerBy<br />
- equals<br />
- greaterThan<br />
- greaterThanOrEqualTo<br />
- lessThan<br />
- lessThanOrEqualTo<br />
- minus<br />
- mod<br />
- plus<br />
- times</code></td>
- <td>number type has more than<br />15 significant digits</td>
- <td>Accept.</td>
- </tr>
- <tr>
- <td>not a base... number</td>
- <td>Substitute <code>NaN</code>.</td>
- </tr>
- <tr>
- <td>base not an integer</td>
- <td>Truncate to integer.<br />Ignore if not a number.</td>
- </tr>
- <tr>
- <td>base out of range</td>
- <td>Ignore.</td>
- </tr>
- <tr>
- <td>not a number<sup>*</sup></td>
- <td>Substitute <code>NaN</code>.</td>
- </tr>
- <tr>
- <td rowspan=10><code>config</code></td>
- <td><code>DECIMAL_PLACES</code> not an integer</td>
- <td>Truncate to integer.<br />Ignore if not a number.</td>
- </tr>
- <tr>
- <td><code>DECIMAL_PLACES</code> out of range</td>
- <td>Ignore.</td>
- </tr>
- <tr>
- <td><code>ROUNDING_MODE</code> not an integer</td>
- <td>Truncate to integer.<br />Ignore if not a number.</td>
- </tr>
- <tr>
- <td><code>ROUNDING_MODE</code> out of range</td>
- <td>Ignore.</td>
- </tr>
- <tr>
- <td>
- <code>EXPONENTIAL_AT</code> not an integer<br />
- or not [integer, integer]
- </td>
- <td>Truncate to integer(s).<br />Ignore if not number(s).</td>
- </tr>
- <tr>
- <td>
- <code>EXPONENTIAL_AT</code> out of range<br />
- or not [negative, positive]
- </td>
- <td>Ignore.</td>
- </tr>
- <tr>
- <td>
- <code>RANGE</code> not a non-zero integer<br />
- or not [integer, integer]
- </td>
- <td> Truncate to integer(s).<br />Ignore if zero or not number(s).</td>
- </tr>
- <tr>
- <td>
- <code>RANGE</code> out of range<br />
- or not [negative, positive]
- </td>
- <td>Ignore.</td>
- </tr>
- <tr>
- <td>
- <code>ERRORS</code> not a boolean<br />
- or binary digit
- </td>
- <td>Ignore.</td>
- </tr>
- <tr>
- <td>
- <code>FORMAT</code> not an object
- </td>
- <td>Ignore.</td>
- </tr>
- <tr>
- <td rowspan=2><code>toPower</code></td>
- <td>exponent not an integer</td>
- <td>
- Truncate to integer.<br />Substitute <code>NaN</code> if not a number.
- </td>
- </tr>
- <tr>
- <td>exponent out of range</td>
- <td>Substitute <code>±Infinity</code>.
- </td>
- </tr>
- <tr>
- <td rowspan=4><code>round</code></td>
- <td>decimal places not an integer</td>
- <td>Truncate to integer.<br />Ignore if not a number.</td>
- </tr>
- <tr>
- <td>decimal places out of range</td>
- <td>Ignore.</td>
- </tr>
- <tr>
- <td>mode not an integer</td>
- <td>Truncate to integer.<br />Ignore if not a number.</td>
- </tr>
- <tr>
- <td>mode out of range</td>
- <td>Ignore.</td>
- </tr>
- <tr>
- <td rowspan=2>
- <code>toExponential</code><br />
- <code>toFixed</code><br />
- <code>toFormat</code>
- </td>
- <td>decimal places not an integer</td>
- <td>Truncate to integer.<br />Ignore if not a number.</td>
- </tr>
- <tr>
- <td>decimal places out of range</td>
- <td>Ignore.</td>
- </tr>
- <tr>
- <td rowspan=2><code>toFraction</code></td>
- <td>max denominator not an integer</td>
- <td>Truncate to integer.<br />Ignore if not a number.</td>
- </tr>
- <tr>
- <td>max denominator out of range</td>
- <td>Ignore.</td>
- </tr>
- <tr>
- <td rowspan=2><code>toPrecision</code></td>
- <td>precision not an integer</td>
- <td>Truncate to integer.<br />Ignore if not a number.</td>
- </tr>
- <tr>
- <td>precision out of range</td>
- <td>Ignore.</td>
- </tr>
- <tr>
- <td rowspan=2><code>toString</code></td>
- <td>base not an integer</td>
- <td>Truncate to integer.<br />Ignore if not a number.</td>
- </tr>
- <tr>
- <td>base out of range</td>
- <td>Ignore.</td>
- </tr>
- </table>
- <p>
- <sup>*</sup>No error is thrown if the value is <code>NaN</code> or 'NaN'
- </p>
- <p>
- The message of a <i>BigNumber Error</i> will also contain the name of the
- method from which the error originated.
- </p>
- <p>
- To determine if an exception is a <i>BigNumber Error</i>:
- </p>
- <pre>
- try {
- // ...
- } catch (e) {
- if ( e instanceof Error && e.name == 'BigNumber Error' ) {
- // ...
- }
- }</pre>
- <h4 id='faq'>FAQ</h4>
- <h6>Why are trailing fractional zeros removed from BigNumbers?</h6>
- <p>
- Many arbitrary-precision libraries retain trailing fractional zeros as
- they can indicate the precision of a value. This can be useful but the
- results of arithmetic operations can be misleading.
- </p>
- <pre>
- x = new BigDecimal("1.0")
- y = new BigDecimal("1.1000")
- z = x.add(y) // 2.1000
- x = new BigDecimal("1.20")
- y = new BigDecimal("3.45000")
- z = x.multiply(y) // 4.1400000</pre>
- <p>
- To specify the precision of a value is to specify that the value lies
- within a certain range.
- </p>
- <p>
- In the first example, <code>x</code> has a value of 1.0. The trailing zero
- shows the precision of the value, implying that it is in the range 0.95 to
- 1.05. Similarly, the precision indicated by the trailing zeros of
- <code>y</code> indicates that the value is in the range 1.09995 to
- 1.10005. If we add the two lowest values in the ranges we get 0.95 +
- 1.09995 = 2.04995 and if we add the two highest values we get 1.05 +
- 1.10005 = 2.15005, so the range of the result of the addition implied by
- the precision of its operands is 2.04995 to 2.15005. The result given by
- BigDecimal of 2.1000 however, indicates that the value is in the range
- 2.09995 to 2.10005 and therefore the precision implied by its trailing
- zeros is misleading.
- </p>
- <p>
- In the second example, the true range is 4.122744 to 4.157256 yet the
- BigDecimal answer of 4.1400000 indicates a range of 4.13999995 to
- 4.14000005. Again, the precision implied by the trailing zeros is
- misleading.
- </p>
- <p>
- This library, like binary floating point and most calculators, does not
- retain trailing fractional zeros. Instead, the <code>toExponential</code>,
- <code>toFixed</code> and <code>toPrecision</code> methods enable trailing
- zeros to be added if and when required.
- </p>
- <br />
- </div>
- </body>
- </html>
|