Avoid potential confusion over the round method: ceil, floor, round and
trunc no longer accept arguments and so they match their JS Math object
equivalents. Removed toInteger as round now handles rounding to integer. Added
toSignificantDigits as round no longer rounds to precision. Updated tests
accordingly. Calling config without argument no longer throws.
pull/3/head
Michael Mclaughlin 10 years ago
parent 714eb63369
commit b141f3480d

@ -13,7 +13,7 @@ An arbitrary-precision Decimal type for JavaScript.
- Works with numbers with or without fraction digits in bases from 2 to 64 inclusive
- Stores values in an accessible decimal floating-point format
- No dependencies
- Uses JavaScript 1.5 (ECMAScript 3) features only
- Wide platform compatibility: uses JavaScript 1.5 (ECMAScript 3) features only
- Comprehensive [documentation](http://mikemcl.github.io/decimal.js/) and test set
- 8 KB minified and gzipped
@ -111,25 +111,27 @@ and `isNaN` and `isFinite` methods, as `NaN` and `Infinity` are valid `Decimal`
y = new Decimal(Infinity) // 'Infinity'
x.isNaN() && !y.isNaN() && !x.isFinite() && !y.isFinite() // true
All calculations are rounded to the number of significant digits specified by the `precision` property
of the Decimal constructor and rounded using the rounding mode specified by the `rounding` property.
As mentioned above, multiple Decimal constructors can be created, each with their own independent
configuration which applies to all Decimal numbers created from it.
All calculations are rounded to the number of significant digits specified by the `precision` property
of each Decimal constructor and rounded using the rounding mode specified by the `rounding` property.
Decimal.config({ precision: 5, rounding: 4 })
D = Decimal.constructor() // constructor is a factory method
D.config({ precision: 10, rounding: 1 })
// constructor is a factory method and it can also accept a configuration object
Decimal10 = Decimal.constructor({ precision: 10, rounding: 1 })
x = new Decimal(5)
y = new D(5)
y = new Decimal10(5)
x.div(3) // '1.6667'
y.div(3) // '1.666666666'
Decimal.precision // 5
D.precision // 10
Decimal10.precision // 10
Many of the methods of JavaScript's Math object are also replicated
@ -178,8 +180,7 @@ then
will create *decimal.min.js*.
The *decimal.min.js* already present was created with *Microsoft Ajax Minifier 5.8* (in protest at
uglify-js failing to preserve licensing comments).
The *decimal.min.js* already present was created with *Microsoft Ajax Minifier 5.8*.
## Feedback
@ -199,8 +200,13 @@ See LICENCE.
## Change Log
####2.0.0
* 10/04/2014 Added `toSignificantDigits`
* Removed `toInteger`
* No arguments to `ceil`, `floor`, `round` and `trunc`
####1.0.1
* 07/04/2014 Minor documentation clean-up.
* 07/04/2014 Minor documentation clean-up
####1.0.0
* 02/04/2014 Initial release

@ -1,10 +1,10 @@
/*! decimal.js v1.0.1 https://github.com/MikeMcl/decimal.js/LICENCE */
/*! decimal.js v2.0.0 https://github.com/MikeMcl/decimal.js/LICENCE */
;(function (global) {
'use strict';
/*
* decimal.js v1.0.1
* decimal.js v2.0.0
* An arbitrary-precision Decimal type for JavaScript.
* https://github.com/MikeMcl/decimal.js
* Copyright (c) 2014 Michael Mclaughlin <M8ch88l@gmail.com>
@ -28,7 +28,7 @@
/*
The limit on the value of #precision, and on the argument to #toDecimalPlaces,
#toExponential, #toFixed, #toPrecision, #ceil, #floor, #round, and #truncate.
#toExponential, #toFixed, #toFormat, #toPrecision and #toSignificantDigits.
*/
MAX_DIGITS = 1E9, // 0 to 1e+9
@ -62,19 +62,13 @@
/*
* Return a new Decimal whose value is this Decimal rounded to a maximum of #d significant
* digits using rounding mode ROUND_CEIL, or to an integer if #d is omitted.
*
* [d] {number} Significant digits. Integer, 1 to MAX_DIGITS inclusive.
*
* 'ceil() digits out of range: {d}'
* 'ceil() digits not an integer: {d}'
* Return a new Decimal whose value is the value of this Decimal rounded to a whole number in
* the direction of positive Infinity.
*
*/
P['ceil'] = function (d) {
P['ceil'] = function () {
return rnd( new this['constructor'](this), d == null ||
!checkArg( this, d, 'ceil', 1 ) ? this['e'] + 1 : d | 0, 2 );
return rnd( new this['constructor'](this), this['e'] + 1, 2 );
};
@ -222,19 +216,13 @@
/*
* Return a new Decimal whose value is the value of this Decimal rounded to a maximum of #d
* significant digits using rounding mode ROUND_FLOOR, or to an integer if #d is omitted.
*
* [d] {number} Significant digits. Integer, 1 to MAX_DIGITS inclusive.
*
* 'floor() digits out of range: {d}'
* 'floor() digits not an integer: {d}'
* Return a new Decimal whose value is the value of this Decimal rounded to a whole number in
* the direction of negative Infinity.
*
*/
P['floor'] = function (d) {
P['floor'] = function () {
return rnd( new this['constructor'](this), d == null ||
!checkArg( this, d, 'floor', 1 ) ? this['e'] + 1 : d | 0, 3 );
return rnd( new this['constructor'](this), this['e'] + 1, 3 );
};
@ -902,27 +890,15 @@
/*
* Return a new Decimal whose value is this Decimal rounded to a maximum of #d significant
* digits using rounding mode #rm, or to #precision and #rounding respectively if omitted.
*
* [d] {number} Significant digits. Integer, 1 to MAX_DIGITS inclusive.
* [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
*
* 'round() digits out of range: {d}'
* 'round() digits not an integer: {d}'
* 'round() rounding mode not an integer: {rm}'
* 'round() rounding mode out of range: {rm}'
* Return a new Decimal whose value is the value of this Decimal rounded to a whole number using
* rounding mode #rounding.
*
*/
P['round'] = function ( d, rm ) {
P['round'] = function () {
var x = this,
Decimal = x['constructor'];
x = new Decimal(x);
return d == null || !checkArg( x, d, 'round', 1 )
? rnd( x, Decimal['precision'], Decimal['rounding'] )
: rnd( x, d | 0, checkRM( x, rm, 'round' ) );
return rnd( new Decimal(x), x['e'] + 1, Decimal['rounding'] );
};
@ -1376,23 +1352,6 @@
};
/*
* Return a new Decimal whose value is the value of this Decimal rounded to an integer using
* rounding mode #rm or #rounding if #rm is omitted.
*
* [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
*
* 'toInteger() rounding mode not an integer: {rm}'
* 'toInteger() rounding mode out of range: {rm}'
*
*/
P['toInteger'] = P['toInt'] = function (rm) {
var x = this;
return rnd( new x['constructor'](x), x['e'] + 1, checkRM( x, rm, 'toInteger' ) );
};
/*
* Returns a new Decimal whose value is the nearest multiple of the magnitude of #n to the value
* of this Decimal.
@ -1702,6 +1661,31 @@
};
/*
* Return a new Decimal whose value is this Decimal rounded to a maximum of #d significant
* digits using rounding mode #rm, or to #precision and #rounding respectively if omitted.
*
* [d] {number} Significant digits. Integer, 1 to MAX_DIGITS inclusive.
* [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
*
* 'toSD() digits out of range: {d}'
* 'toSD() digits not an integer: {d}'
* 'toSD() rounding mode not an integer: {rm}'
* 'toSD() rounding mode out of range: {rm}'
*
*/
P['toSignificantDigits'] = P['toSD'] = function ( d, rm ) {
var x = this,
Decimal = x['constructor'];
x = new Decimal(x);
return d == null || !checkArg( x, d, 'toSD', 1 )
? rnd( x, Decimal['precision'], Decimal['rounding'] )
: rnd( x, d | 0, checkRM( x, rm, 'toSD' ) );
};
/*
* Return a string representing the value of this Decimal in base #b, or base 10 if #b is
* omitted. If a base is specified, including base 10, round to #precision significant digits
@ -1791,20 +1775,12 @@
/*
* Return a new Decimal whose value is the value of this Decimal truncated to a maximum of #d
* significant digits, or to an integer if #d is omitted, using rounding mode ROUND_DOWN, i.e.
* towards zero.
*
* [d] {number} Significant digits. Integer, 1 to MAX_DIGITS inclusive.
*
* 'trunc() digits out of range: {d}'
* 'trunc() digits not an integer: {d}'
* Return a new Decimal whose value is the value of this Decimal truncated to a whole number.
*
*/
P['truncated'] = P['trunc'] = function (d) {
P['truncated'] = P['trunc'] = function () {
return rnd( new this['constructor'](this), d == null ||
!checkArg( this, d, 'trunc', 1 ) ? this['e'] + 1 : d | 0, 1 );
return rnd( new this['constructor'](this), this['e'] + 1, 1 );
};
@ -2725,8 +2701,8 @@
c = 'config',
parse = Decimal['errors'] ? parseInt : parseFloat;
if ( obj == u || typeof obj != 'object' ) {
ifExceptionsThrow( Decimal, 'object expected', obj, c );
if ( obj == u || typeof obj != 'object' &&
!ifExceptionsThrow( Decimal, 'object expected', obj, c ) ) {
return Decimal;
}
@ -3287,7 +3263,7 @@
str = rand( c[0] + 1 ) + rand();
do {
ld = c[i]; // ##limit digit
ld = c[i]; // #limit digit
rd = str.charAt(i++); // random digit
} while ( ld == rd );
} while ( rd > ld || i > n || rd == '' );
@ -3466,9 +3442,7 @@
return;
} else if ( b == 10 ) {
return rnd(
new Decimal(n), Decimal['precision'], Decimal['rounding']
);
return rnd( new Decimal(n), Decimal['precision'], Decimal['rounding'] );
} else {
n += '';
}
@ -3548,7 +3522,6 @@
Decimal['maxE'] = EXP_LIMIT; // 1 to EXP_LIMIT
// Whether Decimal Errors are ever thrown.
// Change #parseInt to #parseFloat if changing #errors to false.
Decimal['errors'] = true; // true/false
// Whether to use cryptographically-secure random number generation, if available.

4
decimal.min.js vendored

File diff suppressed because one or more lines are too long

@ -82,7 +82,7 @@ li span{float:right;margin-right:10px;color:#c0c0c0}
<a href="#constructor-properties">Properties</a>
<ul>
<li><a href="#one" >ONE</a></li>
<li class='spacer' >&nbsp;</li>
<li class='spacer'>&nbsp;</li>
<li><a href="#precision">precision</a></li>
<li><a href="#rounding" >rounding</a></li>
<li><a href="#minE" >minE</a></li>
@ -92,7 +92,7 @@ li span{float:right;margin-right:10px;color:#c0c0c0}
<li><a href="#errors" >errors</a></li>
<li><a href="#modulo" >modulo</a></li>
<li><a href="#crypto" >crypto</a></li>
<li class='spacer' >&nbsp;</li>
<li class='spacer'>&nbsp;</li>
<li><a href="#modes">ROUND_UP</a></li>
<li><a href="#modes">ROUND_DOWN</a></li>
<li><a href="#modes">ROUND_CEIL</a></li>
@ -142,12 +142,12 @@ li span{float:right;margin-right:10px;color:#c0c0c0}
<li><a href="#toFi" >toFixed </a> </li>
<li><a href="#toFo" >toFormat </a> </li>
<li><a href="#toFr" >toFraction </a> </li>
<li><a href="#toInt" >toInteger </a><span>toInt</span> </li>
<li><a href="#toJSON" >toJSON </a> </li>
<li><a href="#toNear" >toNearest </a> </li>
<li><a href="#toNum" >toNumber </a> </li>
<li><a href="#pow" >toPower </a><span>pow</span> </li>
<li><a href="#toP" >toPrecision </a> </li>
<li><a href="#toSD" >toSignificantDigits </a><span>toSD</span> </li>
<li><a href="#toS" >toString </a> </li>
<li><a href="#trunc" >truncated </a><span>trunc</span> </li>
<li><a href="#valueOf">valueOf </a> </li>
@ -307,8 +307,7 @@ new Decimal('ff.8', 16) // '255.5'</pre>
</p>
<pre>Decimal.config({ precision: 5 })
new Decimal(1.23456789) // '1.23456789'
new Decimal(1.23456789, 10) // '1.2346'
new Decimal(1.23456789).round() // '1.2346'</pre>
new Decimal(1.23456789, 10) // '1.2346'</pre>
@ -379,15 +378,15 @@ y.div(3) // 0.333333333
// D9 = Decimal.constructor({ precision: 9 }) is equivalent to:
D9 = Decimal.constructor()
D9.config({ precision: 9 })</pre>
<p>
It is not inefficient in terms of memory usage to use multiple Decimal constructors as
functions are shared between them.
</p>
<p>
<code>constructor</code> is a factory method so it is not necessary or desirable to use
<code>new</code> but it will do no harm.
</p>
<pre>D = new Decimal.constructor()</pre>
<p>
It is not inefficient in terms of memory usage to use multiple Decimal constructors as
functions are shared between them.
</p>
@ -616,12 +615,12 @@ Decimal.precision = 0
conversion.
</p>
<p>
All methods which return a Decimal will round the value to be returned to
<code>precision</code> significant digits except
<a href='#abs'><code>absoluteValue</code></a>, <a href='#ceil'><code>ceil</code></a>,
<a href='#floor'><code>floor</code></a>, <a href='#neg'><code>negated</code></a>,
<a href='#toDP'><code>toDecimalPlaces</code></a> and
<a href='#toNear'><code>toNearest</code></a>.
All methods which return a Decimal will round the return value to <code>precision</code>
significant digits except <a href='#abs'><code>absoluteValue</code></a>,
<a href='#ceil'><code>ceil</code></a>, <a href='#floor'><code>floor</code></a>,
<a href='#neg'><code>negated</code></a>, <a href='#round'><code>round</code></a>,
<a href='#toDP'><code>toDecimalPlaces</code></a>, <a href='#toNear'><code>toNearest</code></a>
and <a href='#trunc'><code>truncated</code></a>.
</p>
<p>
A Decimal constructor will also not round to <code>precision</code> unless a base is
@ -638,11 +637,14 @@ Decimal.precision // 5</pre>
Default value: <code>4</code> <a href="#h-up">(<code>ROUND_HALF_UP</code>)</a>
</p>
<p>
The default rounding mode used when rounding to
<code><a href='#precision'>precision</a></code> and when rounding
<a href='#round'><code>round</code></a>, <a href='#toDP'><code>toDecimalPlaces</code></a>,
The default rounding mode used when rounding the result of a calculation or base conversion to
<code><a href='#precision'>precision</a></code> significant digits, and when rounding the
return value of the <a href='#round'><code>round</code></a>,
<a href='#toDP'><code>toDecimalPlaces</code></a>,
<a href='#toE'><code>toExponential</code></a>, <a href='#toFi'><code>toFixed</code></a>,
<a href='#toFo'><code>toFormat</code></a>, and <a href='#toP'><code>toPrecision</code></a>.
<a href='#toFo'><code>toFormat</code></a>, <a href='#toNear'><code>toNearest</code></a>,
<a href='#toP'><code>toPrecision</code></a> and
<a href='#toSD'><code>toSignificantDigits</code></a> methods.
</p>
<p>
The <a href='#modes'>rounding modes</a> are available as enumerated properties of the
@ -959,15 +961,10 @@ z = y.abs() // '0.8'</pre>
<h5 id="ceil">ceil<code class='inset'>.ceil([sd]) <i>&rArr; Decimal</i></code></h5>
<p><code>sd</code>: <i>number</i>: integer, 1 to 1e+9 inclusive.</p>
<p>
Returns a new Decimal whose value is the value of this Decimal rounded to <code>sd</code>
significant digits in the direction of positive <code>Infinity</code>.
</p>
<h5 id="ceil">ceil<code class='inset'>.ceil() <i>&rArr; Decimal</i></code></h5>
<p>
If <code>sd</code> is omitted or is <code>null</code> or undefined, the return value is
the value of this Decimal rounded to a whole number.
Returns a new Decimal whose value is the value of this Decimal rounded to a whole number in
the direction of positive <code>Infinity</code>.
</p>
<p>The return value is not rounded to <a href='#precision'><code>precision</code></a>.</p>
<pre>
@ -1113,15 +1110,10 @@ y.exp() // '7.3890560989306502272'</pre>
<h5 id="floor">floor<code class='inset'>.floor([sd]) <i>&rArr; Decimal</i></code></h5>
<p><code>sd</code>: <i>number</i>: integer, 1 to 1e+9 inclusive.</p>
<p>
Returns a new Decimal whose value is the value of this Decimal rounded to <code>sd</code>
significant digits in the direction of negative <code>Infinity</code>.
</p>
<h5 id="floor">floor<code class='inset'>.floor() <i>&rArr; Decimal</i></code></h5>
<p>
If <code>sd</code> is omitted or is <code>null</code> or undefined, the return value is
the value of this Decimal rounded to a whole number.
Returns a new Decimal whose value is the value of this Decimal rounded to a whole number in
the direction of negative <code>Infinity</code>.
</p>
<p>The return value is not rounded to <a href='#precision'><code>precision</code></a>.</p>
<pre>
@ -1455,41 +1447,23 @@ y.sd(true) // '6'</pre>
<h5 id="round">round<code class='inset'>.round([sd [, rm]]) <i>&rArr; Decimal</i></code></h5>
<p>
<code>sd</code>: <i>number</i>: integer, 1 to 1e+9 inclusive.<br />
<code>rm</code>: <i>number</i>: integer, 0 to 8 inclusive.
</p>
<p>
Returns a new Decimal whose value is the value of this Decimal rounded to <code>sd</code>
significant digits using rounding mode <code>rm</code>.
</p>
<p>
If <code>sd</code> is omitted or is <code>null</code> or undefined, the return value will
be rounded to <a href='#precision'><code>precision</code></a> significant digits.
</p>
<h5 id="round">round<code class='inset'>.round() <i>&rArr; Decimal</i></code></h5>
<p>
if <code>rm</code> is omitted or is <code>null</code> or undefined, rounding mode
<a href='#rounding'><code>rounding</code></a> will be used.
</p>
<p>
See <a href='#Errors'>Errors</a> for the treatment of other non-integer or out of range
<code>sd</code> or <code>rm</code> values.
Returns a new Decimal whose value is the value of this Decimal rounded to a whole number using
rounding mode <a href='#rounding'><code>rounding</code></a>.
</p>
<p>
This method differs completely from <code>Math.round</code>, which rounds to
an integer. See <a href='#toInt'><code>toInteger</code></a>.
To emulate <code>Math.round</code>, set <a href='#rounding'><code>rounding</code></a> to
<code>7</code>, i.e. <a href='#modes'><code>ROUND_HALF_CEIL</code></a>.
</p>
<pre>
Decimal.config({ precision: 5, rounding: 4 })
x = new Decimal(9876.54321)
Decimal.config({ rounding: 4 })
x = 1234.5
x.round() // '1235'
x.round() // '9876.5'
x.round(6) // '9876.54'
x.round(6, Decimal.ROUND_UP) // '9876.55'
x.round(2) // '9900'
x.round(2, 1) // '9800'
x // '9876.54321'</pre>
Decimal.rounding = Decimal.ROUND_DOWN
x.round() // '1234'
x // '1234.5'</pre>
@ -1561,12 +1535,13 @@ x.times('-a', 16) // '-6'</pre>
</p>
<pre>
x = new Decimal(12.24567)
x.toDecimalPlaces(0) // '12'
x.toDecimalPlaces(1, 0) // '12.3'
x.toDecimalPlaces(0) // '12'
x.toDecimalPlaces(1, 0) // '12.3'
y = new Decimal(9876.54321)
y.toDP(3) // '9876.543'
y.toDP(1, 0) // '9876.6'
y.toDP(1, 1) // '9876.5'</pre>
y.toDP(3) // '9876.543'
y.toDP(1, 0) // '9876.6'
y.toDP(1, Decimal.ROUND_DOWN) // '9876.5'</pre>
@ -1742,31 +1717,6 @@ pi.toFraction(1) // '3, 1'</pre>
<h5 id="toInt">toInteger<code class='inset'>.toInt([rm]) <i>&rArr; Decimal</i></code></h5>
<p><code>rm</code>: <i>number</i>: integer, 0 to 8 inclusive.</p>
<p>
Returns a new Decimal whose value is the value of this Decimal rounded to a whole number using
rounding mode <code>rm</code>, or <a href='#rounding'><code>rounding</code></a>
if <code>rm</code> is omitted or is <code>null</code> or undefined.
</p>
<p>
See <a href='#Errors'>Errors</a> for the treatment of other non-integer or out of range
<code>rm</code> values.
</p>
<pre>
Decimal.config({ rounding: 4 })
x = 1234.5
x.toInteger() // '1235'
Decimal.config({ rounding: 1 })
x.toInt() // '1234'
x.toInteger(Decimal.ROUND_HALF_CEIL) // '1235'
x.toInt(6) // '1234'
x // '1234.56'</pre>
<h5 id="toJSON">toJSON<code class='inset'>.toJSON() <i>&rArr; string</i></code></h5>
<p>As <code>valueOf</code>.</p>
<pre>
@ -1812,7 +1762,7 @@ JSON.parse(str, function (key, val) {
<p>
In this context, rounding mode <a href='#rounding'><code>ROUND_HALF_UP</code></a> is
interpreted the same as rounding mode <a href='#rounding'><code>ROUND_UP</code></a>, and so
on. The rounding is either up, down, to ceil, to floor or to even.
on. I.e. the rounding is either up, down, to ceil, to floor or to even.
</p>
<p>
The return value will always have the same sign as this Decimal, unless either this Decimal
@ -1868,7 +1818,7 @@ z = new Decimal(-0)
</p>
<pre>
Math.pow(0.7, 2) // 0.48999999999999994
x = new Decimal(0.7)
x = new Decimal(0.7)
x.toPower(2) // '0.49'
new Decimal(3).pow(-2) // '0.11111111111111111111'
@ -1959,6 +1909,42 @@ y.toPrecision(5) // '45.600'</pre>
<h5 id="toSD">
toSignificantDigits<code class='inset'>.toSD([sd [, rm]]) <i>&rArr; Decimal</i></code>
</h5>
<p>
<code>sd</code>: <i>number</i>: integer, 1 to 1e+9 inclusive.<br />
<code>rm</code>: <i>number</i>: integer, 0 to 8 inclusive.
</p>
<p>
Returns a new Decimal whose value is the value of this Decimal rounded to <code>sd</code>
significant digits using rounding mode <code>rm</code>.
</p>
<p>
If <code>sd</code> is omitted or is <code>null</code> or undefined, the return value will
be rounded to <a href='#precision'><code>precision</code></a> significant digits.
</p>
<p>
if <code>rm</code> is omitted or is <code>null</code> or undefined, rounding mode
<a href='#rounding'><code>rounding</code></a> will be used.
</p>
<p>
See <a href='#Errors'>Errors</a> for the treatment of other non-integer or out of range
<code>sd</code> or <code>rm</code> values.
</p>
<pre>
Decimal.config({ precision: 5, rounding: 4 })
x = new Decimal(9876.54321)
x.toSignificantDigits() // '9876.5'
x.toSignificantDigits(6) // '9876.54'
x.toSignificantDigits(6, Decimal.ROUND_UP) // '9876.55'
x.toSD(2) // '9900'
x.toSD(2, 1) // '9800'
x // '9876.54321'</pre>
<h5 id="toS">toString<code class='inset'>.toString([base]) <i>&rArr; string</i></code></h5>
<p><code>base</code>: <i>number</i>: integer, 2 to 64 inclusive</p>
<p>
@ -2003,14 +1989,9 @@ z.toString(10) // '1.2346'</pre>
<h5 id="trunc">truncated<code class='inset'>.trunc([sd]) <i>&rArr; Decimal</i></code></h5>
<h5 id="trunc">truncated<code class='inset'>.trunc() <i>&rArr; Decimal</i></code></h5>
<p>
Returns a new Decimal whose value is the value of this Decimal truncated to to <code>sd</code>
significant digits.
</p>
<p>
If <code>sd</code> is omitted or is <code>null</code> or undefined, the return value is
the value of this Decimal truncated to a whole number.
Returns a new Decimal whose value is the value of this Decimal truncated to a whole number.
</p>
<p>The return value is not rounded to <a href='#precision'><code>precision</code></a>.</p>
<pre>
@ -2147,7 +2128,7 @@ x // '0.00001234'</pre>
<pre>
x = new Number(-0) // 0
1 / x == -Infinity // true
y = new Decimal(-0) // '0'
y.c // '0' ( [0].toString() )
y.e // 0
@ -2170,21 +2151,6 @@ y.s // -1</pre>
<th>errors: true<br />Throw Decimal Error</th>
<th>errors: false<br />Action on invalid argument</th>
</tr>
<tr>
<td rowspan=2>
<code>
ceil<br />
floor<br />
truncated
</code>
</td>
<td>argument not an integer</td>
<td>Truncate to integer.<br />Ignore if not a number</td>
</tr>
<tr>
<td>argument out of range</td>
<td>Ignore</td>
</tr>
<tr>
<td rowspan=5>
<code>
@ -2226,11 +2192,7 @@ y.s // -1</pre>
<td>Substitute <code>NaN</code></td>
</tr>
<tr>
<td rowspan=17><code>config</code></td>
<td>object expected</td>
<td>Ignore</td>
</tr>
<tr>
<td rowspan=16><code>config</code></td>
<td><code>precision</code> not an integer</td>
<td>Truncate to integer.<br />Ignore if not a number</td>
</tr>
@ -2279,11 +2241,11 @@ y.s // -1</pre>
<td>Ignore</td>
</tr>
<tr>
<td><code>errors</code> not a boolean<br />or binary digit</td>
<td><code>errors</code> not a boolean or binary digit</td>
<td>Ignore</td>
</tr>
<tr>
<td><code>crypto</code> not a boolean<br />or binary digit</td>
<td><code>crypto</code> not a boolean or binary digit</td>
<td>Ignore</td>
</tr>
<tr>
@ -2301,17 +2263,17 @@ y.s // -1</pre>
</tr>
<tr>
<td><code>precision</code></td>
<td>argument not a boolean<br />or binary digit</td>
<td>argument not a boolean or binary digit</td>
<td>Ignore</td>
</tr>
<tr>
<td rowspan=4>
<code>
round<br />
toDecimalPlaces<br />
toExponential<br />
toFixed<br />
toPrecision
toPrecision<br />
toSignificantDigits
</code>
</td>
<td>argument not an integer</td>
@ -2341,11 +2303,6 @@ y.s // -1</pre>
<tr>
<td>max denominator out of range</td>
<td>Ignore</td>
</tr>
<tr>
<td><code>toInteger</code></td>
<td>rounding mode out of range</td>
<td>Ignore</td>
</tr>
<tr>
<td rowspan=2><code>toNearest</code></td>

@ -1,7 +1,7 @@
{
"name": "decimal.js",
"description": "An arbitrary-precision Decimal type for JavaScript.",
"version": "1.0.1",
"version": "2.0.0",
"keywords": [
"arbitrary",
"precision",
@ -31,6 +31,6 @@
"license": "MIT",
"scripts": {
"test": "node ./test/every-test.js",
"build": "uglifyjs decimal.js -c -m -o decimal.min.js --preamble '/* decimal.js v1.0.0 https://github.com/MikeMcl/decimal.js/LICENCE */'"
"build": "uglifyjs decimal.js -c -m -o decimal.min.js --preamble '/* decimal.js v2.0.0 https://github.com/MikeMcl/decimal.js/LICENCE */'"
}
}

@ -46,10 +46,10 @@
'toFixed',
'toFormat',
'toFraction',
'toInt',
'toNearest',
'toNumber',
'toPrecision',
'toSD',
'toStringEtc',
'trunc'
];

@ -39,10 +39,10 @@
<!-- <script src='../toFixed.js'></script> -->
<!-- <script src='../toFormat.js'></script> -->
<!-- <script src='../toFraction.js'></script> -->
<!-- <script src='../toInt.js'></script> -->
<!-- <script src='../toNearest.js'></script> -->
<!-- <script src='../toNumber.js'></script> -->
<!-- <script src='../toPrecision.js'></script> -->
<!-- <script src='../toSD.js'></script> -->
<!-- <script src='../toStringEtc.js'></script> -->
<!-- <script src='../trunc.js'></script> -->

@ -49,8 +49,8 @@ var count = (function ceil(Decimal) {
}
}
function T(expected, value, n) {
assert(String(expected), new Decimal(String(value)).ceil(n).toString());
function T(expected, value) {
assert(String(expected), new Decimal(String(value)).ceil().toString());
}
log('\n Testing ceil...');
@ -58,8 +58,8 @@ var count = (function ceil(Decimal) {
Decimal.config({
precision: 20,
rounding: 4,
toExpNeg: -9e15,
toExpPos: 9e15,
toExpNeg: -1e3,
toExpPos: 1e3,
minE: -9e15,
maxE: 9e15,
errors: true
@ -105,6 +105,11 @@ var count = (function ceil(Decimal) {
T('2949426983040960', '2949426983040959.8911208825380208568451907');
T('25167', '25166.125888418871654557352055849116604612621573251770362');
T('4560569286496', '4560569286495.98300685103599898554605198');
T('11', '10.5');
T('0', '-0.0101');
T('-1', '-1.0101');
T('98743655', '98743654.999999999999999999');
T('-98743654', '-98743654.999999999999999999');
T('14', '13.763105480576616251068323541559825687');
T('176037174185746614410406167888', '176037174185746614410406167887.42317518');
T('9050999219307', '9050999219306.7846946346757664893036971777');
@ -223,34 +228,17 @@ var count = (function ceil(Decimal) {
// ------------------------------------------------------------------ v8 end
assertException(function () {new Decimal('0.6').ceil(0)}, "ceil(0)");
assertException(function () {new Decimal('1.6').ceil(0)}, "ceil(0)");
T('0.7', '0.66', 1);
T('2', '1.66', 1);
T('0.6667', '0.66666', 4);
T('1.667', '1.66666', 4);
T('0.6', '0.6', 1);
T('2', '1.6', 1);
T('0.67', '0.666', 2);
T('1.7', '1.66', 2);
T('0.66667', '0.666666', 5);
T('1.6667', '1.66666', 5);
T('-1', '-1.0', 1);
T('-1', '-1.00001', 1);
T('-1', '-1.010203', 2);
T('-2.99', '-2.999', 3);
T('-0.0001', '-0.000123', 1);
T('-0.000123', '-0.000123', 4);
T('0.010203', '0.010203', 5);
T('0.0102031', '0.01020301', 6);
T('-9.999', '-9.999', 6);
T('-9.9999', '-9.999900000001', 6);
assert('2', new Decimal('1.0000000000000000001').ceil().toString());
T('1', '1e-9000000000000000');
T('0', '-1e-9000000000000000');
T('0', '-9.9e-9000000000000001');
T('9.999999e+9000000000000000', '9.999999e+9000000000000000');
T('-9.999999e+9000000000000000', '-9.999999e+9000000000000000');
T('Infinity', '1E9000000000000001');
T('-Infinity', '-1e+9000000000000001');
T('5.5879983320336874473209567979e+287894365', '5.5879983320336874473209567979e+287894365');
T('-5.5879983320336874473209567979e+287894365', '-5.5879983320336874473209567979e+287894365');
log('\n ' + passed + ' of ' + total + ' tests passed in ' + (+new Date() - start) + ' ms \n');
return [passed, total];

@ -91,10 +91,11 @@ var count = (function config(Decimal) {
modulo: 4
});
// Throw if no object is passed.
assertException(function () {Decimal.config()}, "config()");
assertException(function () {Decimal.config(undefined)}, "config(undefined)");
assertException(function () {Decimal.config(null)}, "config(null)");
assert(true, Decimal.config() === Decimal);
assert(true, Decimal.config(undefined) === Decimal);
assert(true, Decimal.config(null) === Decimal);
// Throw if not an object object.
assertException(function () {Decimal.config('')}, "config('')");
assertException(function () {Decimal.config('hi')}, "config('hi')");
assertException(function () {Decimal.config(4)}, "config(4)");

@ -124,8 +124,8 @@ var count = (function constructor(Decimal) {
assert(false, new Decimal(9.99).eq(new D3('-9.99')));
assert(true, new Decimal(9.99).eq(new D5('9.99')));
assert(false, new Decimal(123.456789).round().eq(new D3('123.456789').round()));
assert(true, new Decimal(123.456789).round(5).eq(new D3('123.456789').round(5)));
assert(false, new Decimal(123.456789).toSD().eq(new D3('123.456789').toSD()));
assert(true, new Decimal(123.456789).round().eq(new D3('123.456789').round()));
log('\n ' + passed + ' of ' + total + ' tests passed in ' + (+new Date() - start) + ' ms \n');
return [passed, total];

@ -38,10 +38,10 @@ console.log( '\n STARTING TESTS...\n' );
'toFixed',
'toFormat',
'toFraction',
'toInt',
'toNearest',
'toNumber',
'toPrecision',
'toSD',
'toStringEtc',
'trunc'
]

@ -48,8 +48,8 @@ var count = (function floor(Decimal) {
}
}
function T(expected, value, n) {
assert(String(expected), new Decimal(String(value)).floor(n).toString());
function T(expected, value) {
assert(String(expected), new Decimal(String(value)).floor().toString());
}
log('\n Testing floor...');
@ -57,8 +57,8 @@ var count = (function floor(Decimal) {
Decimal.config({
precision: 20,
rounding: 4,
toExpNeg: -9e15,
toExpPos: 9e15,
toExpNeg: -1e3,
toExpPos: 1e3,
minE: -9e15,
maxE: 9e15,
errors: true
@ -80,7 +80,8 @@ var count = (function floor(Decimal) {
T('70591', '70591.2244675522123484658978887');
T('4446128540401735117', '4446128540401735117.435836700611264749985822486641350492901');
T('-597273', '-597273');
T('729117', '729117');
T('729117', '729117.5');
T('-729118', '-729117.001');
T('4803729546823170064608098091', '4803729546823170064608098091');
T('-6581532150677269472830', '-6581532150677269472829.38194951340848938896000325718062365494');
T('2949426983040959', '2949426983040959.8911208825380208568451907');
@ -88,7 +89,7 @@ var count = (function floor(Decimal) {
T('4560569286495', '4560569286495.98300685103599898554605198');
T('13', '13.763105480576616251068323541559825687');
T('9050999219306', '9050999219306.7846946346757664893036971777');
T('39900924', '39900924');
T('39900924', '39900924.00000005');
T('115911043168452445', '115911043168452445');
T('20962819101135667464733349383', '20962819101135667464733349383.8959025798517496777183');
T('4125789711001606948191', '4125789711001606948191.4707575965791242737346836');
@ -99,6 +100,7 @@ var count = (function floor(Decimal) {
T('657', '657.0353902852');
T('0', '0.00000000000000000000000017921822306362413915');
T('1483059355427939255846407887', '1483059355427939255846407887.011361095342689876');
T('7.722e+999999999999999', '7.722e+999999999999999');
T('7722', '7722');
T('0', '0.00000005');
T('8551283060956479352', '8551283060956479352.5707396');
@ -206,37 +208,8 @@ var count = (function floor(Decimal) {
// ------------------------------------------------------------------ v8 end
assertException(function () {new Decimal('0.6').floor(0)}, "floor(0)");
assertException(function () {new Decimal('1.6').floor(0)}, "floor(0)");
T('0.6', '0.66', 1);
T('1', '1.66', 1);
T('0.6666', '0.66666', 4);
T('1.666', '1.66666', 4);
T('0.6', '0.6', 1);
T('1', '1.6', 1);
T('0.66', '0.666', 2);
T('1.6', '1.66', 2);
T('0.66666', '0.666666', 5);
T('1.6666', '1.66666', 5);
assert('1', new Decimal('1.9999999999').floor().toString());
T('-1', '-1.0', 1);
T('-2', '-1.00001', 1);
T('-1.1', '-1.010203', 2);
T('-3', '-2.999', 3);
T('-0.0002', '-0.000123', 1);
T('-0.000123', '-0.000123', 4);
T('0.010203', '0.010203', 5);
T('0.010203', '0.01020301', 6);
T('-9.999', '-9.999', 6);
T('-9.99991', '-9.999900000001', 6);
T('0', '1e-9000000000000000');
T('-1', '-1e-9000000000000000');
T('0', '-9.9e-9000000000000001');

@ -320,7 +320,6 @@ var count = (function others(Decimal) {
assert(true, new Decimal(0).lessThanOrEqualTo('Infinity', 36));
assert(false, new Decimal(0).greaterThanOrEqualTo('Infinity', 36));
assert(false, new Decimal(10).lessThanOrEqualTo(20, 4));
assert(true, new Decimal(10).lessThanOrEqualTo(20, 5));
assert(false, new Decimal(10).greaterThanOrEqualTo(20, 6));
assert(false, new Decimal(1.23001e-2).lessThan(1.23e-2));

@ -152,6 +152,7 @@ var count = (function minMax(Decimal) {
T(-Infinity, 1, [1, '-1e+9000000000000001', -1e200]);
T(0, 1, [1, '1e-9000000000000001', 1e-200]);
T(0, 1, [1, '-1e-9000000000000001', 1e-200]);
T(-3, 3, [1, '2', 3, '-1', -2, '-3']);
log('\n ' + passed + ' of ' + total + ' tests passed in ' + (+new Date() - start) + ' ms \n');
return [passed, total];

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -48,9 +48,9 @@ var count = (function trunc(Decimal) {
}
}
function T(expected, value, n) {
assert(String(expected), new Decimal(String(value)).trunc(n).toString());
//assert(String(expected), new Decimal(String(value)).truncated(n).toString());
function T(expected, value) {
assert(String(expected), new Decimal(String(value)).trunc().toString());
//assert(String(expected), new Decimal(String(value)).truncated().toString());
}
log('\n Testing trunc...');
@ -58,8 +58,8 @@ var count = (function trunc(Decimal) {
Decimal.config({
precision: 20,
rounding: 4,
toExpNeg: -9e15,
toExpPos: 9e15,
toExpNeg: -1e3,
toExpPos: 1e3,
minE: -9e15,
maxE: 9e15,
errors: true
@ -74,13 +74,11 @@ var count = (function trunc(Decimal) {
T('-3633239209', '-3633239209.654526163275621746013315304191073405508491056');
T('-23970335459820625362', '-23970335459820625362');
T('131869457416154038', '131869457416154038');
T('-2685', '-2685');
T('-4542227860', '-4542227860.9511298545226');
T('2416872281', '2416872281.963955669484225137349193306323379254936827');
T('-757684868752087594264588207655', '-757684868752087594264588207655.27838048392835556');
T('-438798503526', '-438798503526.2317623894721299587561697');
T('801625782231888715214665', '801625782231888715214665');
T('-91881984778675238', '-91881984778675238');
T('327765350218284325239839632046', '327765350218284325239839632046.91682741746683081459605386');
T('-7469045007691432294', '-7469045007691432294.362757245');
T('8365540212937142194319515218789', '8365540212937142194319515218789.4106658678537421977827');
@ -96,14 +94,12 @@ var count = (function trunc(Decimal) {
T('4446128540401735117', '4446128540401735117.435836700611264749985822486641350492901');
T('-597273', '-597273');
T('729117', '729117');
T('504', '504');
T('-6581532150677269472829', '-6581532150677269472829.38194951340848938896000325718062365494');
T('-131279182164804751', '-131279182164804751.430589952021038264');
T('2949426983040959', '2949426983040959.8911208825380208568451907');
T('25166', '25166.125888418871654557352055849116604612621573251770362');
T('4560569286495', '4560569286495.98300685103599898554605198');
T('13', '13.763105480576616251068323541559825687');
T('176037174185746614410406167887', '176037174185746614410406167887.42317518');
T('9050999219306', '9050999219306.7846946346757664893036971777');
T('20962819101135667464733349383', '20962819101135667464733349383.8959025798517496777183');
T('4125789711001606948191', '4125789711001606948191.4707575965791242737346836');
@ -131,7 +127,6 @@ var count = (function trunc(Decimal) {
T('0', '0.00000000000000000000004');
T('0', '0.000250427721966583680168028884692015623739');
T('0', '0.000000000001585613219016120158734661293405081934');
T('0', '0.00009');
T('0', '0.000000090358252973411013592234');
T('276312604693909858427', '276312604693909858427.21965306055697011390137926559');
T('0', '0.0000252');
@ -176,38 +171,9 @@ var count = (function trunc(Decimal) {
T(Infinity, Infinity);
T(-Infinity, -Infinity);
assertException(function () {new Decimal('0.6').trunc(0)}, "trunc(0)");
assertException(function () {new Decimal('1.6').trunc(0)}, "trunc(0)");
T('0.6', '0.66', 1);
T('1', '1.66', 1);
T('0.6666', '0.66666', 4);
T('1.666', '1.66666', 4);
T('0.6', '0.6', 1);
T('1', '1.6', 1);
T('0.66', '0.666', 2);
T('1.6', '1.66', 2);
T('0.66666', '0.666666', 5);
T('1.6666', '1.66666', 5);
assert('1', new Decimal('1.9999999999').trunc().toString());
assert('-1', new Decimal('-1.9999999999').trunc().toString());
T('-1', '-1.0', 1);
T('-1', '-1.00001', 1);
T('-1', '-1.010203', 2);
T('-2.99', '-2.999', 3);
T('-0.0001', '-0.000123', 1);
T('-0.000123', '-0.000123', 4);
T('0.010203', '0.010203', 5);
T('0.010203', '0.01020301', 6);
T('-9.999', '-9.999', 6);
T('-9.9999', '-9.999900000001', 6);
log('\n ' + passed + ' of ' + total + ' tests passed in ' + (+new Date() - start) + ' ms \n');
return [passed, total];
})(this.Decimal);

Loading…
Cancel
Save