v1.4.0 Added toNumber
This commit is contained in:
parent
1f9f40055c
commit
09a7f9dfb2
13
README.md
13
README.md
|
@ -18,7 +18,8 @@ A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic.
|
|||
|
||||
If an even smaller and simpler library is required see [big.js](https://github.com/MikeMcl/big.js/).
|
||||
It's half the size but only works with decimal numbers and only has half the methods.
|
||||
It also does not allow `NaN` or `Infinity`, or have the configuration options of this library.
|
||||
It also does not allow `NaN` or `Infinity`, or have the configuration options of this library.
|
||||
See also [decimal.js](https://github.com/MikeMcl/decimal.js/).
|
||||
|
||||
## Load
|
||||
|
||||
|
@ -40,11 +41,11 @@ The library is also available from the [npm](https://npmjs.org/) registry, so
|
|||
|
||||
$ npm install bignumber.js
|
||||
|
||||
will install this entire directory in a *node_modules* directory within the current directory.
|
||||
will install this directory in a *node_modules* directory within the current directory.
|
||||
|
||||
To load with AMD loader libraries such as [requireJS](http://requirejs.org/):
|
||||
|
||||
require(['bignumber'], function(BigNumber) {
|
||||
require(['path/to/bignumber'], function(BigNumber) {
|
||||
// Use BigNumber here in local scope. No global BigNumber.
|
||||
});
|
||||
|
||||
|
@ -90,6 +91,7 @@ Like JavaScript's Number type, there are `toExponential`, `toFixed` and `toPreci
|
|||
x.toExponential(5) // "2.55500e+2"
|
||||
x.toFixed(5) // "255.50000"
|
||||
x.toPrecision(5) // "255.50"
|
||||
x.toNumber() // 255.5
|
||||
|
||||
and a base can be specified for `toString`.
|
||||
|
||||
|
@ -188,7 +190,7 @@ I.e. minify.
|
|||
|
||||
For Node, if uglify-js is installed globally ( `npm install uglify-js -g` ) then
|
||||
|
||||
uglifyjs -o ./bignumber.min.js ./bignumber.js
|
||||
npm run build
|
||||
|
||||
will create *bignumber.min.js*.
|
||||
|
||||
|
@ -211,6 +213,9 @@ See LICENCE.
|
|||
|
||||
## Change Log
|
||||
|
||||
####1.4.0
|
||||
* 08/05/2014 Added `toNumber`.
|
||||
|
||||
####1.3.0
|
||||
* 08/11/2013 Ensure correct rounding of `sqrt` in all, rather than almost all, cases.
|
||||
* Maximum radix to 64.
|
||||
|
|
19
bignumber.js
19
bignumber.js
|
@ -1,13 +1,10 @@
|
|||
/* bignumber.js v1.3.0 https://github.com/MikeMcl/bignumber.js/LICENCE */
|
||||
|
||||
/*jslint ass: true, bitwise: true, eqeq: true, plusplus: true, sub: true, white: true, maxerr: 500 */
|
||||
/*global module, define */
|
||||
/*! bignumber.js v1.4.0 https://github.com/MikeMcl/bignumber.js/LICENCE */
|
||||
|
||||
;(function ( global ) {
|
||||
'use strict';
|
||||
|
||||
/*
|
||||
bignumber.js v1.3.0
|
||||
bignumber.js v1.4.0
|
||||
A JavaScript library for arbitrary-precision arithmetic.
|
||||
https://github.com/MikeMcl/bignumber.js
|
||||
Copyright (c) 2012 Michael Mclaughlin <M8ch88l@gmail.com>
|
||||
|
@ -1969,6 +1966,18 @@
|
|||
};
|
||||
|
||||
|
||||
/*
|
||||
* Return the value of this BigNumber converted to a number primitive.
|
||||
*
|
||||
*/
|
||||
P['toNumber'] = P['toN'] = function () {
|
||||
var x = this;
|
||||
|
||||
// Ensure zero has correct sign.
|
||||
return +x || ( x['s'] ? 0 * x['s'] : NaN );
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* Return as toString, but do not accept a base argument.
|
||||
*/
|
||||
|
|
File diff suppressed because one or more lines are too long
23
doc/API.html
23
doc/API.html
|
@ -119,6 +119,7 @@ code,pre{font-family:Monaco,Consolas,"Lucida Console",monospace;
|
|||
<li><a href="#lte" >lessThanOrEqualTo</a></li>
|
||||
<li><a href="#toE" >toExponential</a></li>
|
||||
<li><a href="#toF" >toFixed</a></li>
|
||||
<li><a href="#toN" >toNumber</a></li>
|
||||
<li><a href="#toP" >toPrecision</a></li>
|
||||
<li><a href="#toS" >toString</a></li>
|
||||
<li><a href="#valueOf">valueOf</a></li>
|
||||
|
@ -208,7 +209,7 @@ code,pre{font-family:Monaco,Consolas,"Lucida Console",monospace;
|
|||
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 ediiting the DIGITS variable near the top of the
|
||||
(this can be changed by ediiting the DIGITS variable near the top of the
|
||||
source file).</i>
|
||||
</dd>
|
||||
</dl>
|
||||
|
@ -1165,6 +1166,26 @@ y.toF(3) // '45.600'</pre>
|
|||
|
||||
|
||||
|
||||
<h5 id="toN">toNumber<code class='inset'>.toN() ⇒ <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="toP">
|
||||
toPrecision<code class='inset'>.toP([significant_figures]) ⇒
|
||||
<i>string</i></code>
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "bignumber.js",
|
||||
"description": "A library for arbitrary-precision decimal and non-decimal arithmetic",
|
||||
"version": "1.3.0",
|
||||
"version": "1.4.0",
|
||||
"keywords": [
|
||||
"arbitrary",
|
||||
"precision",
|
||||
|
@ -31,6 +31,6 @@
|
|||
"license": "MIT",
|
||||
"scripts": {
|
||||
"test": "node ./test/every-test.js",
|
||||
"build": "uglifyjs -o ./bignumber.min.js ./bignumber.js"
|
||||
"build": "uglifyjs bignumber.js -c -m -o bignumber.min.js --preamble '/* bignumber.js v1.4.0 https://github.com/MikeMcl/bignumber.js/LICENCE */'"
|
||||
}
|
||||
}
|
|
@ -34,6 +34,7 @@
|
|||
'toExponential',
|
||||
'toFixed',
|
||||
'toFraction',
|
||||
'toNumber',
|
||||
'toPrecision',
|
||||
'toString'
|
||||
];
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
<!-- <script src='../toExponential.js'></script> -->
|
||||
<!-- <script src='../toFixed.js'></script> -->
|
||||
<!-- <script src='../toFraction.js'></script> -->
|
||||
<!-- <script src='../toNumber.js'></script> -->
|
||||
<!-- <script src='../toPrecision.js'></script> -->
|
||||
<!-- <script src='../toString.js'></script> -->
|
||||
</body>
|
||||
|
|
|
@ -26,6 +26,7 @@ console.log( '\n STARTING TESTS...\n' );
|
|||
'toExponential',
|
||||
'toFixed',
|
||||
'toFraction',
|
||||
'toNumber',
|
||||
'toPrecision',
|
||||
'toString'
|
||||
]
|
||||
|
|
|
@ -0,0 +1,93 @@
|
|||
var count = (function toNumber(BigNumber) {
|
||||
var start = +new Date(),
|
||||
log,
|
||||
error,
|
||||
undefined,
|
||||
passed = 0,
|
||||
total = 0;
|
||||
|
||||
if (typeof window === 'undefined') {
|
||||
log = console.log;
|
||||
error = console.error;
|
||||
} else {
|
||||
log = function (str) { document.body.innerHTML += str.replace('\n', '<br>') };
|
||||
error = function (str) { document.body.innerHTML += '<div style="color: red">' +
|
||||
str.replace('\n', '<br>') + '</div>' };
|
||||
}
|
||||
|
||||
if (!BigNumber && typeof require === 'function') BigNumber = require('../bignumber');
|
||||
|
||||
function assert(expected, actual) {
|
||||
total++;
|
||||
if (expected === actual || isNaN(expected) && isNaN(actual)) {
|
||||
passed++;
|
||||
//log('\n Expected and actual: ' + actual);
|
||||
} else {
|
||||
error('\n Test number: ' + total + ' failed');
|
||||
error(' Expected: ' + expected);
|
||||
error(' Actual: ' + actual);
|
||||
//process.exit();
|
||||
}
|
||||
}
|
||||
|
||||
function isMinusZero(n) {
|
||||
return 1 / n === -Infinity;
|
||||
}
|
||||
|
||||
function T(value, n) {
|
||||
assert(n, new BigNumber(value).toNumber());
|
||||
}
|
||||
|
||||
log('\n Testing toNumber...');
|
||||
|
||||
BigNumber.config({
|
||||
DECIMAL_PLACES: 20,
|
||||
ROUNDING_MODE: 4,
|
||||
ERRORS: true,
|
||||
RANGE: 1E9,
|
||||
EXPONENTIAL_AT: 1E9
|
||||
});
|
||||
|
||||
assert(false, isMinusZero(new BigNumber('0').toNumber()));
|
||||
assert(false, isMinusZero(new BigNumber('0.0').toNumber()));
|
||||
assert(false, isMinusZero(new BigNumber('0.000000000000').toNumber()));
|
||||
assert(false, isMinusZero(new BigNumber('0e+0').toNumber()));
|
||||
assert(false, isMinusZero(new BigNumber('0e-0').toNumber()));
|
||||
assert(false, isMinusZero(new BigNumber('1e-1000000000').toNumber()));
|
||||
|
||||
assert(true, isMinusZero(new BigNumber('-0').toNumber()));
|
||||
assert(true, isMinusZero(new BigNumber('-0.0').toNumber()));
|
||||
assert(true, isMinusZero(new BigNumber('-0.000000000000').toNumber()));
|
||||
assert(true, isMinusZero(new BigNumber('-0e+0').toNumber()));
|
||||
assert(true, isMinusZero(new BigNumber('-0e-0').toNumber()));
|
||||
assert(true, isMinusZero(new BigNumber('-1e-1000000000').toNumber()));
|
||||
|
||||
T(1, 1);
|
||||
T('1', 1);
|
||||
T('1.0', 1);
|
||||
T('1e+0', 1);
|
||||
T('1e-0', 1);
|
||||
T(12345.6789, 12345.6789);
|
||||
|
||||
T(-1, -1);
|
||||
T('-1', -1);
|
||||
T('-1.0', -1);
|
||||
T('-1e+0', -1);
|
||||
T('-1e-0', -1);
|
||||
|
||||
T(Infinity, 1 / 0);
|
||||
T('Infinity', 1 / 0);
|
||||
T(-Infinity, -1 / 0);
|
||||
T('-Infinity', -1 / 0);
|
||||
T(NaN, NaN);
|
||||
T('NaN', NaN);
|
||||
|
||||
T('9.999999e+1000000000', 1 / 0);
|
||||
T('-9.999999e+1000000000', -1 / 0);
|
||||
T('1e-1000000000', 0);
|
||||
T('-1e-1000000000', -0);
|
||||
|
||||
log('\n ' + passed + ' of ' + total + ' tests passed in ' + (+new Date() - start) + ' ms \n');
|
||||
return [passed, total];
|
||||
})(this.BigNumber);
|
||||
if (typeof module !== 'undefined' && module.exports) module.exports = count;
|
Loading…
Reference in New Issue