@@ -325,8 +325,6 @@ Decimal128.fromString = function(string) {
325
325
}
326
326
}
327
327
328
- if ( significantDigits > 34 ) invalidErr ( string , 'Too many digits to represent accurately' ) ;
329
-
330
328
// Normalization of exponent
331
329
// Correct exponent based on radix position, and shift significand as needed
332
330
// to represent user input
@@ -392,7 +390,12 @@ Decimal128.fromString = function(string) {
392
390
// If we have seen a radix point, 'string' is 1 longer than we have
393
391
// documented with ndigits_read, so inc the position of the first nonzero
394
392
// digit and the position that digits are read to.
395
- if ( sawRadix && exponent === EXPONENT_MIN ) {
393
+ if ( sawRadix ) {
394
+ firstNonZero = firstNonZero + 1 ;
395
+ endOfString = endOfString + 1 ;
396
+ }
397
+ // if negative, we need to increment again to account for - sign at start.
398
+ if ( isNegative ) {
396
399
firstNonZero = firstNonZero + 1 ;
397
400
endOfString = endOfString + 1 ;
398
401
}
@@ -431,12 +434,8 @@ Decimal128.fromString = function(string) {
431
434
) ;
432
435
}
433
436
}
434
- } else {
435
- invalidErr ( string , 'overflow' ) ;
436
437
}
437
438
}
438
- } else {
439
- invalidErr ( string , 'overflow' ) ;
440
439
}
441
440
}
442
441
@@ -722,9 +721,19 @@ Decimal128.prototype.toString = function() {
722
721
// has trailing zeros. However, we *cannot* output these trailing zeros,
723
722
// because doing so would change the precision of the value, and would
724
723
// change stored data if the string converted number is round tripped.
725
-
726
724
if ( scientific_exponent >= 34 || scientific_exponent <= - 7 || exponent > 0 ) {
727
725
// Scientific format
726
+
727
+ // if there are too many significant digits, we should just be treating numbers
728
+ // as + or - 0 and using the non-scientific exponent (this is for the "invalid
729
+ // representation should be treated as 0/-0" spec cases in decimal128-1.json)
730
+ if ( significand_digits > 34 ) {
731
+ string . push ( 0 ) ;
732
+ if ( exponent > 0 ) string . push ( 'E+' + exponent ) ;
733
+ else if ( exponent < 0 ) string . push ( 'E' + exponent ) ;
734
+ return string . join ( '' ) ;
735
+ }
736
+
728
737
string . push ( significand [ index ++ ] ) ;
729
738
significand_digits = significand_digits - 1 ;
730
739
0 commit comments