Include minus sign for valueOf() negative zero

This commit is contained in:
Michael Mclaughlin 2015-10-26 15:48:44 +00:00
parent f549554c8c
commit 1100ceea4d
5 changed files with 30 additions and 12 deletions

View File

@ -2353,10 +2353,7 @@
* Return the value of this BigNumber converted to a number primitive.
*/
P.toNumber = function () {
var x = this;
// Ensure zero has correct sign.
return +x || ( x.s ? x.s * 0 : NaN );
return +this;
};
@ -2485,10 +2482,23 @@
/*
* Return as toString, but do not accept a base argument.
* Return as toString, but do not accept a base argument, and include the minus sign for
* negative zero.
*/
P.valueOf = P.toJSON = function () {
return this.toString();
var str,
n = this,
e = n.e;
if ( e === null ) return n.toString();
str = coeffToString( n.c );
str = e <= TO_EXP_NEG || e >= TO_EXP_POS
? toExponential( str, e )
: toFixedPoint( str, e );
return n.s < 0 ? '-' + str : str;
};

File diff suppressed because one or more lines are too long

2
bignumber.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -1752,10 +1752,16 @@ y.trunc() // '-12'</pre>
<h5 id="valueOf">valueOf<code class='inset'>.valueOf() <i>&rArr; string</i></code></h5>
<p>As <code>toString</code>, but does not accept a base argument.</p>
<p>
As <code>toString</code>, but does not accept a base argument and includes the minus sign
for negative zero.
</p>
<pre>
x = new BigNumber('1.777e+457')
x.valueOf() // '1.777e+457'</pre>
x = new BigNumber('-0')
x.toString() // '0'
x.valueOf() // '-0'
y = new BigNumber('1.777e+457')
y.valueOf() // '1.777e+457'</pre>

View File

@ -189,7 +189,9 @@ var count = (function others(BigNumber) {
assert(false, n.lessThan(0, 36));
assert(true, n.lessThan(0.1));
assert(true, n.lessThanOrEqualTo(0));
assert(n.toString(), n.valueOf());
assert(true, n.valueOf() === '-0');
assert(true, n.toJSON() === '-0');
assert(true, n.toString() === '0');
n = new BigNumber('NaN');
assert(false, n.isFinite());