#88 Update doc and .mjs after toNearest change

pull/122/head
Michael Mclaughlin 7 years ago
parent c99bdef459
commit 6016146ef7

@ -2114,14 +2114,8 @@
/* /*
* Returns a new Decimal whose value is the nearest multiple of the magnitude of `y` to the value * Returns a new Decimal whose value is the nearest multiple of `y` in the direction of rounding
* of this Decimal. * mode `rm`, or `Decimal.rounding` if `rm` is omitted, to the value of this Decimal.
*
* If the value of this Decimal is equidistant from two multiples of `y`, the rounding mode `rm`,
* or `Decimal.rounding` if `rm` is omitted, determines the direction of the nearest multiple.
*
* In the context of this method, rounding mode 4 (ROUND_HALF_UP) is the same as rounding mode 0
* (ROUND_UP), and so on.
* *
* The return value will always have the same sign as this Decimal, unless either this Decimal * The return value will always have the same sign as this Decimal, unless either this Decimal
* or `y` is NaN, in which case the return value will be also be NaN. * or `y` is NaN, in which case the return value will be also be NaN.

@ -2110,16 +2110,9 @@ P.toHexadecimal = P.toHex = function (sd, rm) {
}; };
/* /*
* Returns a new Decimal whose value is the nearest multiple of the magnitude of `y` to the value * Returns a new Decimal whose value is the nearest multiple of `y` in the direction of rounding
* of this Decimal. * mode `rm`, or `Decimal.rounding` if `rm` is omitted, to the value of this Decimal.
*
* If the value of this Decimal is equidistant from two multiples of `y`, the rounding mode `rm`,
* or `Decimal.rounding` if `rm` is omitted, determines the direction of the nearest multiple.
*
* In the context of this method, rounding mode 4 (ROUND_HALF_UP) is the same as rounding mode 0
* (ROUND_UP), and so on.
* *
* The return value will always have the same sign as this Decimal, unless either this Decimal * The return value will always have the same sign as this Decimal, unless either this Decimal
* or `y` is NaN, in which case the return value will be also be NaN. * or `y` is NaN, in which case the return value will be also be NaN.
@ -2148,7 +2141,11 @@ P.toNearest = function (y, rm) {
rm = Ctor.rounding; rm = Ctor.rounding;
} else { } else {
y = new Ctor(y); y = new Ctor(y);
if (rm !== void 0) checkInt32(rm, 0, 8); if (rm === void 0) {
rm = Ctor.rounding;
} else {
checkInt32(rm, 0, 8);
}
// If x is not finite, return x if y is not NaN, else NaN. // If x is not finite, return x if y is not NaN, else NaN.
if (!x.d) return y.s ? x : y; if (!x.d) return y.s ? x : y;
@ -2163,7 +2160,6 @@ P.toNearest = function (y, rm) {
// If y is not zero, calculate the nearest multiple of y to x. // If y is not zero, calculate the nearest multiple of y to x.
if (y.d[0]) { if (y.d[0]) {
external = false; external = false;
if (rm < 4) rm = [4, 5, 7, 8][rm];
x = divide(x, y, 0, rm, 1).times(y); x = divide(x, y, 0, rm, 1).times(y);
external = true; external = true;
finalise(x); finalise(x);
@ -4329,7 +4325,7 @@ function clone(obj) {
Decimal.config = Decimal.set = config; Decimal.config = Decimal.set = config;
Decimal.clone = clone; Decimal.clone = clone;
Decimal.isDecimal = isDecimalInstance; Decimal.isDecimal = isDecimalInstance;
Decimal.abs = abs; Decimal.abs = abs;
Decimal.acos = acos; Decimal.acos = acos;

@ -2045,8 +2045,8 @@ x.toBinary(1) // '0b1p+8'</pre>
<p>Throws on an invalid <code>dp</code> or <code>rm</code> value.</p> <p>Throws on an invalid <code>dp</code> or <code>rm</code> value.</p>
<pre> <pre>
x = new Decimal(12.34567) x = new Decimal(12.34567)
x.toDecimalPlaces(0) // '12' x.toDecimalPlaces(0) // '12'
x.toDecimalPlaces(1, 0) // '12.3' x.toDecimalPlaces(1, Decimal.ROUND_UP) // '12.3'
y = new Decimal(9876.54321) y = new Decimal(9876.54321)
y.toDP(3) // '9876.543' y.toDP(3) // '9876.543'
@ -2083,15 +2083,15 @@ y.toDP(1, Decimal.ROUND_DOWN) // '9876.5'</pre>
<pre> <pre>
x = 45.6 x = 45.6
y = new Decimal(x) y = new Decimal(x)
x.toExponential() // '4.56e+1' x.toExponential() // '4.56e+1'
y.toExponential() // '4.56e+1' y.toExponential() // '4.56e+1'
x.toExponential(0) // '5e+1' x.toExponential(0) // '5e+1'
y.toExponential(0) // '5e+1' y.toExponential(0) // '5e+1'
x.toExponential(1) // '4.6e+1' x.toExponential(1) // '4.6e+1'
y.toExponential(1) // '4.6e+1' y.toExponential(1) // '4.6e+1'
y.toExponential(1, 1) // '4.5e+1' (ROUND_DOWN) y.toExponential(1, Decimal.ROUND_DOWN) // '4.5e+1'
x.toExponential(3) // '4.560e+1' x.toExponential(3) // '4.560e+1'
y.toExponential(3) // '4.560e+1'</pre> y.toExponential(3) // '4.560e+1'</pre>
@ -2131,14 +2131,14 @@ y.toExponential(3) // '4.560e+1'</pre>
<pre> <pre>
x = 3.456 x = 3.456
y = new Decimal(x) y = new Decimal(x)
x.toFixed() // '3' x.toFixed() // '3'
y.toFixed() // '3.456' y.toFixed() // '3.456'
y.toFixed(0) // '3' y.toFixed(0) // '3'
x.toFixed(2) // '3.46' x.toFixed(2) // '3.46'
y.toFixed(2) // '3.46' y.toFixed(2) // '3.46'
y.toFixed(2, 1) // '3.45' (ROUND_DOWN) y.toFixed(2, Decimal.ROUND_DOWN) // '3.45'
x.toFixed(5) // '3.45600' x.toFixed(5) // '3.45600'
y.toFixed(5) // '3.45600'</pre> y.toFixed(5) // '3.45600'</pre>
@ -2210,25 +2210,16 @@ x.toHex(1) // '0x1p+8'</pre>
<h5 id="toNearest"> <h5 id="toNearest">
toNearest<code class='inset'>.toNearest(n [, rm]) <i>&rArr; Decimal</i></code> toNearest<code class='inset'>.toNearest(x [, rm]) <i>&rArr; Decimal</i></code>
</h5> </h5>
<p> <p>
<code>x</code>: <i>number|string|Decimal</i><br /> <code>x</code>: <i>number|string|Decimal</i><br />
<code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive <code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive
</p> </p>
<p> <p>
Returns a new Decimal whose value is the nearest multiple of <code>x</code> to the value of Returns a new Decimal whose value is the nearest multiple of <code>x</code> in the direction
this Decimal. of rounding mode <code>rm</code>, or <a href='#rounding'><code>rounding</code></a> if
</p> <code>rm</code> is omitted, to the value of this Decimal.
<p>
If the value of this Decimal is equidistant from two multiples of <code>x</code>, the rounding
mode <code>rm</code>, or <a href='#rounding'><code>rounding</code></a> if <code>rm</code> is
omitted, determines the direction of the nearest.
</p>
<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, i.e. the rounding is either up, down, to ceil, to floor or to even.
</p> </p>
<p> <p>
The return value will always have the same sign as this Decimal, unless either this Decimal The return value will always have the same sign as this Decimal, unless either this Decimal
@ -2241,11 +2232,11 @@ x.toHex(1) // '0x1p+8'</pre>
</p> </p>
<pre> <pre>
x = new Decimal(1.39) x = new Decimal(1.39)
x.toNearest(0.25) // '1.5' x.toNearest(0.25) // '1.5'
y = new Decimal(0.75) // equidistant from 0.5 and 1 y = new Decimal(9.499)
y.toNearest(0.5, 0) // '1' (ROUND_UP) y.toNearest(0.5, Decimal.ROUND_UP) // '9.5'
y.toNearest(0.5, 1) // '0.5' (ROUND_DOWN)</pre> y.toNearest(0.5, Decimal.ROUND_DOWN) // '9'</pre>
@ -2393,8 +2384,8 @@ x.toPrecision() // '45.6'
y.toPrecision() // '45.6' y.toPrecision() // '45.6'
x.toPrecision(1) // '5e+1' x.toPrecision(1) // '5e+1'
y.toPrecision(1) // '5e+1' y.toPrecision(1) // '5e+1'
y.toPrecision(2, 0) // '4.6e+1' (ROUND_UP) y.toPrecision(2, Decimal.ROUND_UP) // '4.6e+1'
y.toPrecision(2, 1) // '4.5e+1' (ROUND_DOWN) y.toPrecision(2, Decimal.DOWN) // '4.5e+1'
x.toPrecision(5) // '45.600' x.toPrecision(5) // '45.600'
y.toPrecision(5) // '45.600'</pre> y.toPrecision(5) // '45.600'</pre>

Loading…
Cancel
Save