Skip to content

Commit ec3fef6

Browse files
committed
test(NODE-5594): add tests to check rounding behaviour
1 parent fa5fcd9 commit ec3fef6

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

test/node/decimal128.test.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ describe('Decimal128', function () {
468468

469469
it('fromString from string round', function (done) {
470470
// Create decimal from string value 10E-6177
471-
let result = Decimal128.fromString('10E-6177');
471+
const result = Decimal128.fromString('10E-6177');
472472
const bytes = Buffer.from(
473473
[
474474
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -478,9 +478,6 @@ describe('Decimal128', function () {
478478

479479
expect(bytes).to.deep.equal(result.bytes);
480480

481-
result = Decimal128.fromString('37.499999999999999196428571428571375');
482-
expect(result.toString()).to.deep.equal('37.49999999999999919642857142857138');
483-
484481
// // Create decimal from string value 15E-6177
485482
// result = Decimal128.fromString('15E-6177');
486483
// bytes = Buffer.from(
@@ -1306,6 +1303,40 @@ describe('Decimal128', function () {
13061303
});
13071304
}
13081305
});
1306+
1307+
context('when the input has more than 34 significant digits', function () {
1308+
it('does not throw an error', function () {
1309+
expect(() =>
1310+
Decimal128.fromStringWithRounding('37.499999999999999196428571428571375')
1311+
).to.not.throw();
1312+
});
1313+
context('when the digit to round is >= 5', function () {
1314+
it('rounds up correctly', function () {
1315+
const result = Decimal128.fromStringWithRounding(
1316+
'37.499999999999999196428571428571375'
1317+
);
1318+
expect(result.toString()).to.deep.equal('37.49999999999999919642857142857138');
1319+
});
1320+
});
1321+
context('when the digit to round is < 5', function () {
1322+
it('rounds down correctly', function () {
1323+
const result = Decimal128.fromStringWithRounding(
1324+
'37.499999999999999196428571428571374'
1325+
);
1326+
expect(result.toString()).to.deep.equal('37.49999999999999919642857142857137');
1327+
});
1328+
});
1329+
1330+
context('when the digit to round is 9', function () {
1331+
it('rounds up and carries correctly', function () {
1332+
const result = Decimal128.fromStringWithRounding(
1333+
'37.4999999999999999196428571428571399'
1334+
);
1335+
expect(result.toString()).to.deep.equal('37.49999999999999991964285714285714');
1336+
});
1337+
});
1338+
});
13091339
});
13101340
});
13111341
});
1342+

0 commit comments

Comments
 (0)