@@ -468,7 +468,7 @@ describe('Decimal128', function () {
468
468
469
469
it ( 'fromString from string round' , function ( done ) {
470
470
// Create decimal from string value 10E-6177
471
- let result = Decimal128 . fromString ( '10E-6177' ) ;
471
+ const result = Decimal128 . fromString ( '10E-6177' ) ;
472
472
const bytes = Buffer . from (
473
473
[
474
474
0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
@@ -478,9 +478,6 @@ describe('Decimal128', function () {
478
478
479
479
expect ( bytes ) . to . deep . equal ( result . bytes ) ;
480
480
481
- result = Decimal128 . fromString ( '37.499999999999999196428571428571375' ) ;
482
- expect ( result . toString ( ) ) . to . deep . equal ( '37.49999999999999919642857142857138' ) ;
483
-
484
481
// // Create decimal from string value 15E-6177
485
482
// result = Decimal128.fromString('15E-6177');
486
483
// bytes = Buffer.from(
@@ -1306,6 +1303,39 @@ describe('Decimal128', function () {
1306
1303
} ) ;
1307
1304
}
1308
1305
} ) ;
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
+ } ) ;
1309
1339
} ) ;
1310
1340
} ) ;
1311
1341
} ) ;
0 commit comments