1
0
mirror of https://github.com/MikeMcl/decimal.js.git synced 2024-10-27 20:34:12 +00:00

#88 Update doc and .mjs after toNearest change

This commit is contained in:
Michael Mclaughlin 2018-03-10 19:43:59 +00:00
parent c99bdef459
commit 6016146ef7
3 changed files with 39 additions and 58 deletions

View File

@ -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.

View File

@ -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);

View File

@ -2046,7 +2046,7 @@ x.toBinary(1) // '0b1p+8'</pre>
<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'
@ -2089,7 +2089,7 @@ 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>
@ -2136,7 +2136,7 @@ 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
@ -2243,9 +2234,9 @@ x.toHex(1) // '0x1p+8'</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>