1
0
mirror of https://github.com/MikeMcl/decimal.js.git synced 2024-09-28 22:40:48 +00:00
This commit is contained in:
Francis Stokes (he/him) 2022-12-04 13:04:04 +00:00 committed by GitHub
commit 141c1f6cf7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 1 deletions

View File

@ -2467,6 +2467,14 @@
return x.isNeg() ? '-' + str : str; return x.isNeg() ? '-' + str : str;
}; };
/*
* The toPrimitive method is used when attempting to perform comparison operations.
* This implementation provides a sensible and user-expected implementation of how
* a Decimal should be transformed into a comparable number
*/
P[Symbol.toPrimitive] = function () {
return Number(this.toString());
};
// Helper functions for Decimal.prototype (P) and/or Decimal methods, and their callers. // Helper functions for Decimal.prototype (P) and/or Decimal methods, and their callers.

View File

@ -0,0 +1,23 @@
if (typeof T === 'undefined') require('../setup');
T('Symbol.toPrimitive', function () {
function D(v) { return new Decimal(v); }
Decimal.config({
precision: 20,
rounding: 4,
toExpNeg: -9e15,
toExpPos: 9e15,
minE: -9e15,
maxE: 9e15
});
T.assert(D('1') < D('2'));
T.assert(D('100') > D('2'));
T.assert(D('-435435.232') < D('2.09802'));
T.assert(D('-1.987234') > D('-2.5473421'));
T.assert(D('10.3') >= D('10.3'));
T.assert(D('10.3') <= D('10.3'));
});

View File

@ -64,7 +64,8 @@ console.log('\n Testing decimal.js\n');
'toSD', 'toSD',
'toString', 'toString',
'trunc', 'trunc',
'valueOf' 'valueOf',
'toPrimitive',
] ]
.forEach(function (module) { .forEach(function (module) {
require('./modules/' + module); require('./modules/' + module);