Include minus sign for valueOf() negative zero
This commit is contained in:
parent
f549554c8c
commit
1100ceea4d
22
bignumber.js
22
bignumber.js
|
@ -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
File diff suppressed because one or more lines are too long
12
doc/API.html
12
doc/API.html
|
@ -1752,10 +1752,16 @@ y.trunc() // '-12'</pre>
|
|||
|
||||
|
||||
<h5 id="valueOf">valueOf<code class='inset'>.valueOf() <i>⇒ 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>
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -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());
|
||||
|
|
Loading…
Reference in New Issue