Skip to content

Commit 9607e2e

Browse files
committed
Fix up toPrecision
1 parent c658605 commit 9607e2e

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

spec.emu

+27-25
Original file line numberDiff line numberDiff line change
@@ -1019,12 +1019,14 @@ location: https://github.com/tc39/proposal-decimal/
10191019
1. Let _O_ be the *this* value.
10201020
1. Perform ? RequireInternalSlot(_O_, [[Decimal128Data]]).
10211021
1. Let _precision_ be 0.
1022+
1. Let _roundingMode_ be *undefined*.
10221023
1. If _options_ is not *undefined*, then
10231024
1. If _options_ is not an Object, throw a *TypeError* exception.
10241025
1. Let _optPrecision_ be ? Get(_options_, *"precision"*).
10251026
1. If _optPrecision_ is neither *undefined* nor a Number, throw a *TypeError* exception.
10261027
1. If _optPrecision_ is *undefined*, return Decimal128ToDecimalString(_O_).
10271028
1. If _precision_ is *NaN*<sub>𝔽</sub>, *+∞*<sub>𝔽</sub> or *+∞*<sub>𝔽</sub>, throw a *RangeError* exception.
1029+
1. Set _roundingMode_ to ? Get(_options_, *"roundingMode"*).
10281030
1. Set _precision_ to ℝ(_precision_).
10291031
1. If _precision_ is not an integer, throw a *RangeError* exception.
10301032
1. If _precision_ < 1, throw a *RangeError* exception.
@@ -1034,33 +1036,33 @@ location: https://github.com/tc39/proposal-decimal/
10341036
1. If _d_ is *-∞*<sub>𝔻</sub>, return *"-Infinity"*.
10351037
1. If sign(_d_) = 1, let _signPrefix_ be *""*, else let _signPrefix_ be *"-"*.
10361038
1. If _d_ is *+0*<sub>𝔻</sub> or *-0*<sub>𝔻</sub>, then
1037-
1. If _precision_ = 0, then
1038-
1. Return *"0"*.
1039+
1. If _precision_ = 1, then
1040+
1. Return the string concatenation of _signPrefix_ and *"0"*.
1041+
1. Else,
1042+
1. Let _additionalZeroes_ be the string *"0"* repeated _precision_ times.
1043+
1. Return the string concatenation of *"0"*, *"."*, and _additionalZeroes_.
1044+
1. Let _q_ be the largest integer such that _d_ × 10<sup>−_q_</sup> is an integer.
1045+
1. Let _n_ be _d_ × 10<sup>−_q_</sup>.
1046+
1. Let _coefficient_ be the unique decimal representation of _n_ without leading zeroes.
1047+
1. Let _numDigits_ be the string length of _coefficient_.
1048+
1. If _numDigits_ < _precision_, then
1049+
1. Let _s_ be Decimal128ToDecimalString(_d_).
1050+
1. If _d_ is an integer, then
1051+
1. Let _additionalZeroes_ be the string *"0"* repeated _precision_ - _numDigits_ times.
1052+
1. Return the string concatenationof _coefficient_, *"."*, and _additionalZeroes_.
10391053
1. Otherwise:
1040-
1. Let _extraZeroes_ be the string *"0"* repeated _precision_ times.
1041-
1. Return the string concatenation of *"0"*, *"."*, and _extraZeroes_.
1042-
1. Set _d_ to abs(_d_).
1043-
1. Let _coefficient_ be _d_ × 10<sup>-q</sup>.
1044-
1. Assert: _coefficient_ is an integer.
1045-
1. Let _coefficientStr_ be the unique decimal string representation of _coefficient_ without leading zeroes.
1046-
1. Let _firstDigit_ be the substring of _coefficientStr_ from 0 to 1.
1047-
1. Let _remainingDigits_ be the substring of _coefficientStr_ from 1.
1048-
1. Let _numCoefficientDigits_ be the length of _coefficientStr_.
1049-
1. Let _m_ be the mantissa of _d_.
1050-
1. Let _scaledM_ be _m_ × 10<sup>_numCoefficientDigits_</sup>.
1051-
1. Let _fractionalPart_ be _scaledM_ mod 1.
1052-
1. Let _e_ be the exponent of _d_.
1053-
1. Let _eStr_ be the unique decimal string representation of _e_ without leading zeroes.
1054-
1. If _numCoefficientDigits_ ≤ _numCoefficientDigits_, then
1055-
1. Let _adjustedMantissa_ be _m_ - (_fractionalPart_ × 10<sup>-_numDigits</sup>).
1056-
1. Assert: _adjustedMantissa_ is an integer.
1057-
1. Let _adjustedMantissaStr_ be the unique decimal string representation of _adjustedMantissa_ without leading zeroes and without trailing zeroes.
1058-
1. If _e_ ≥ 0, set _eStr_ to the concation of *"+"* and _eStr_.
1059-
1. Return the concation of _signPrefix_, _adjustedMantissaStr_, *"e"*, and _eStr_.
1054+
1. Let _additionalZeroes_ be the string *"0"* repeated _precision_ - _numDigits_ times.
1055+
1. Return the string concatenationof _signPrefix, _s_ and _additionalZeroes_.
1056+
1. Else if _numDigits_ = _precision_, then
1057+
1. Return _coefficient_.
10601058
1. Otherwise:
1061-
1. Let _extraZeroes_ be the string *"0"* repeated _numCoefficientDigits_ - 1 - _numCoefficientDigits_ times.
1062-
1. Let _s_ be the string concatenation of _firstDigit_, *"."*, _remainingDigits_, _extraZeroes_, *"e"*, and _eStr_.
1063-
1. Return _s_.
1059+
1. Let _exp_ be _q_ + _precision_.
1060+
1. Let _scaled_ be _d_ × 10<sup>-_exp_</sup>.
1061+
1. Let _scaledAndRounded_ be ApplyRoundingModeToPositive(_scaled_, _roundingMode_).
1062+
1. Assert: _scaledAndRounded_ is not an integer.
1063+
1. Let _s_ be Decimal128ToDecimalString(_scaledAndRounded_).
1064+
1. Let _renderedQ_ be the unique decimal string representation of _q_.
1065+
1. Return the string concatenation of _signPrefix_, _s_, *"e"*, and _renderedQ_.
10641066
</emu-alg>
10651067
<emu-note>
10661068
<p>This operation follows the specification of the conversion of IEEE 754-2019 Decimal128 values to strings (external character sequences) discussed in Section 5.12 of <emu-xref href="#sec-bibliography">IEEE 754-2019</emu-xref>.</p>

0 commit comments

Comments
 (0)