Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion intl.emu
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
</dd>
</dl>
<emu-alg>
1. <ins>If _value_ has the [[Value]] and [[FractionDigits]] internal slots, let _primValue_ be RenderAmountValueWithFractionDigits(_value_.[[Value]], _value_.[[FractionDigits]]) else</ins><del>Let</del> <ins>let</ins _primValue_ be ? ToPrimitive(_value_, ~number~).
1. <ins>If _value_ has the [[Value]] and [[FractionDigits]] internal slots, let _primValue_ be MVtoDecimalString(_value_.[[Value]], _value_.[[FractionDigits]]) else</ins><del>Let</del> <ins>let</ins _primValue_ be ? ToPrimitive(_value_, ~number~).
1. If _primValue_ is a BigInt, return ℝ(_primValue_).
1. If _primValue_ is a String, then
1. Let _str_ be _primValue_.
Expand Down
168 changes: 104 additions & 64 deletions spec.emu
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,92 @@ location: https://github.com/tc39/proposal-amount/
</ul>
</emu-intro>

<emu-clause id="sec-numbers-and-dates">
<h1>Numbers and Dates</h1>
<emu-clause id="sec-number-objects">
<h1>Number Objects</h1>
<emu-clause id="sec-properties-of-the-number-prototype-object">
<h1>Properties of the Number Prototype Object</h1>
</emu-clause>
<emu-clause id="sec-number.prototype.tofixed">
<h1>Number.prototype.toFixed ( _fractionDigits_ )</h1>
<emu-note>
<p>This method returns a String containing this Number value represented in decimal fixed-point notation with _fractionDigits_ digits after the decimal point. If _fractionDigits_ is *undefined*, 0 is assumed.</p>
</emu-note>
<p>It performs the following steps when called:</p>
<emu-alg>
1. Let _x_ be ? ThisNumberValue(*this* value).
1. Let _f_ be ? ToIntegerOrInfinity(_fractionDigits_).
1. Assert: If _fractionDigits_ is *undefined*, then _f_ is 0.
1. If _f_ is not finite, throw a *RangeError* exception.
1. If _f_ &lt; 0 or _f_ > 100, throw a *RangeError* exception.
1. If _x_ is not finite, return Number::toString(_x_, 10).
1. <ins>Return MVtoDecimalString(ℝ(_x_), _f_).</ins>
1. <del>Set _x_ to ℝ(_x_).</del>
1. <del>Let _s_ be the empty String.</del>
1. <del>If _x_ &lt; 0, then</del>
1. <del>Set _s_ to *"-"*.</del>
1. <del>Set _x_ to -_x_.</del>
1. <del>If _x_ ≥ 10<sup>21</sup>, then</del>
1. <del>Let _m_ be ! ToString(𝔽(_x_)).</del>
1. <del>Else,</del>
1. <del>Let _n_ be an integer for which _n_ / 10<sup>_f_</sup> - _x_ is as close to zero as possible. If there are two such _n_, pick the larger _n_.</del>
1. <del>If _n_ = 0, let _m_ be *"0"*. Otherwise, let _m_ be the String value consisting of the digits of the decimal representation of _n_ (in order, with no leading zeroes).</del>
1. <del>If _f_ ≠ 0, then</del>
1. <del>Let _k_ be the length of _m_.</del>
1. <del>If _k_ ≤ _f_, then</del>
1. <del>Let _z_ be the String value consisting of _f_ + 1 - _k_ occurrences of the code unit 0x0030 (DIGIT ZERO).</del>
1. <del>Set _m_ to the string-concatenation of _z_ and _m_.</del>
1. <del>Set _k_ to _f_ + 1.</del>
1. <del>Let _a_ be the first _k_ - _f_ code units of _m_.</del>
1. <del>Let _b_ be the other _f_ code units of _m_.</del>
1. <del>Set _m_ to the string-concatenation of _a_, *"."*, and _b_.</del>
1. <del>Return the string-concatenation of _s_ and _m_.</del>
</emu-alg>
<emu-note>
<p>The output of `toFixed` may be more precise than `toString` for some values because toString only prints enough significant digits to distinguish the number from adjacent Number values. For example,</p>
<p>
`(1000000000000000128).toString()` returns *"1000000000000000100"*, while<br>
`(1000000000000000128).toFixed(0)` returns *"1000000000000000128"*.
</p>
</emu-note>
<emu-clause id="sec-mvtodecimalstring" type="abstract operation">
<h1>
MVtoDecimalString (
_x_: a mathematical value,
_fractionDigits_ : a non-negative integer
): a String
</h1>
<dl class="header">
<dt>description</dt>
<dd>It renders the given Intl mathematical value with a given number of fractional digits</dd>
</dl>
<emu-alg>
1. Let _s_ be the empty String.
1. If _x_ &lt; 0, then
1. Set _s_ to *"-"*.
1. Set _x_ to -_x_.
1. If _x_ ≥ 10<sup>21</sup>, then
1. Let _m_ be ! ToString(𝔽(_x_)).
1. Else,
1. Let _n_ be an integer for which _n_ / 10<sup>_fractionDigits_</sup> - _x_ is as close to zero as possible. If there are two such _n_, pick the larger _n_.
1. If _n_ = 0, let _m_ be *"0"*. Otherwise, let _m_ be the String value consisting of the digits of the decimal representation of _n_ (in order, with no leading zeroes).
1. If _fractionDigits_ ≠ 0, then
1. Let _k_ be the length of _m_.
1. If _k_ ≤ _fractionDigits_, then
1. Let _z_ be the String value consisting of _fractionDigits_ + 1 - _k_ occurrences of the code unit 0x0030 (DIGIT ZERO).
1. Set _m_ to the string-concatenation of _z_ and _m_.
1. Set _k_ to _fractionDigits_ + 1.
1. Let _a_ be the first _k_ - _fractionDigits_ code units of _m_.
1. Let _b_ be the other _fractionDigits_ code units of _m_.
1. Set _m_ to the string-concatenation of _a_, *"."*, and _b_.
1. Return the string-concatenation of _s_ and _m_.
</emu-alg>
</emu-clause>
</emu-clause>
</emu-clause>
</emu-clause>

<emu-clause id="sec-the-amount-object">
<h1>The Amount Object</h1>
<emu-intro id="sec-amount-intro">
Expand Down Expand Up @@ -262,79 +348,33 @@ location: https://github.com/tc39/proposal-amount/
</emu-alg>
</emu-clause>

<emu-clause id="sec-amount-rendermvwithfractiondigits" type="abstract operation">
<h1>RenderAmountValueWithFractionDigits (
_v_: an Intl mathematical value,
_numDigits_: an integer,
<emu-clause id="sec-amount-roundtosignificantdigits" type="abstract operation">
<h1>RoundToSignificantDigits (
_v_: a mathematical value,
_n_: a non-negative integer,
optional _roundingMode_: a rounding mode
): a String
): a mathematical value
</h1>
<dl class="header">
<dt>description</dt>
<dd>It renders the given Intl mathematical value with a given number of fractional digits, rounding, if necessary, using the given rounding mode, which, if missing, is *"halfEven"*.</dd>
<dd>It computes the closest approximation to a given mathematical value that has at most the given number of significant digits, rounding (if necessary) according to the given rounding mode.</dd>
</dl>
<emu-alg>
1. If _v_ is ~not-a-number~, return *"NaN"*.
1. If _v_ is ~negative-infinity~, return *"-Infinity"*.
1. If _v_ is ~positive-infinity~, return *"Infinity"*.
1. If _v_ is ~minus-zero~, then
1. If _numDigits_ < 0, then
1. Let _exp_ be the unique string representation of -_numDigits_ in base 10 without duplicate leading zeros.
1. Return the string-concatenation of *"0"*, *"e"*, and _exp_.
1. Otherwise:
1. Let _trailingZeroes_ be *"0"* repeated _numDigits_ times.
1. Let _z_ be *"-0"*.
1. If _numDigits_ = 0, return _z_.
1. Otherwise, return the string-concatenation of _z_, *"."* and _trailingZeroes_.
1. If _roundingMode_ is *undefined*, set _roundingMode_ to *"halfEven"*.
1. If _v_ < 0, let _prefix_ be *"-"*, else let _prefix_ be "".
1. If _v_ < 0, then
1. Set _v_ to -_v_.
1. Set _roundingMode_ to ReverseRoundingMode(_roundingMode_).
1. Let _rounded_ be ApplyRoundingModeToPositive(_v_ × 10<sup>_numDigits_</sup>, _roundingMode_).
1. Let _e_ be the smallest non-negative integer such that _rounded_ × 10<sup>-_numDigits_</sup> is an integer.
1. Let _s_ be the unique decimal string representation of _rounded_ without duplicate leading zeroes.
1. If _numDigits_ > _e_, then
1. If _v_ is an integer, return the string-concatenation of _prefix, _s_, *"."*, and *"0"* repeated _numDigits_ times.
1. Otherwise, return the string-concatenation of _prefix_, _s_ and *"0"* repeated _numDigits_ - _e_ times.
1. If 0 < _numDigits_, then
1. Return the string-concatenation of _prefix, _s_ and *"0"* repeated _e_ - _numDigits_ times.
1. Otherwise:
1. Assert: _rounded_ is an integer.
1. Let _firstDigit_ be the substring of _s_ from 0 to 1.
1. Let _p_ be the unique integer such that 1 ≤ _rounded_ × 10<sup>_p_</sup> < 10.
1. Let _n_ be _p_ + _numDigits_.
1. Assert: _n_ ≥ 0.
1. Let _exp_ be the unique string representation of _n_ in base-10 without duplicate leading zeros.
1. If _n_ = 0, then
1. Return the string-concatenation of _s_, *"e"*, and _exp_.
1. Otherwise:
1. Let _remainingDigits_ be the substring of _s_ from 1 to _n_.
1. Return the string-concatenation of _firstDigit_, *"."*, _remainingDigits_, *"e"*, and _exp_.
</emu-alg>
</emu-clause>

<emu-clause id="sec-amount-FractionToSignificantDigits" type="abstract operation">
<h1>FractionToSignificantDigits (
_value_: an Intl mathematical value,
_fractionDigits_: an integer
): a non-negative integer
</h1>
<dl class="header"></dl>
<emu-alg>
1. If _value_ is one of ~not-a-number~, ~positive-infinity~, or ~negative-infinity~, then
1. Return 0.
1. If _value_ is one of 0 or ~negative-zero~, then
1. If _fractionDigits_ < 0, return 1.
1. Let _integerDigits_ be 1.
1. Else,
1. Let _integerDigits_ be the smallest integer such that 10<sup>_integerDigits_</sup> > abs(_value_).
1. Assert: _integerDigits_ + _fractionDigits_ > 0.
1. Return _integerDigits_ + _fractionDigits_.
1. If _v_ = 0, return 0.
1. If v &lt; 0, then
1. Let _reverseRoundingMode_ be ReverseRoundingMode(_roundingMode_).
1. Let _d_ be RoundToSignificantDigits(–_v_, _n_, _reverseRoundingMode_).
1. Return –_d_.
1. Let _e_ be the unique integer such that 10<sup>_e_</sup> ≤ _v_ < 10<sup>_e_+1</sup>.
1. Let _pow_ be _e_ - _n_.
1. Let _m_ be _v_ × 10<sup>–_pow_</sup>.
1. Let _rounded_ be ApplyRoundingModeToPositive(_m_, _roundingMode_).
1. Return _rounded_ × 10<sup>_n_ + _e_</sup>.
</emu-alg>
</emu-clause>

<emu-clause id="sec-amount-SignificantToFractionDigits" type="abstract operation">
<emu-clause id="sec-amount-significanttofractiondigits" type="abstract operation">
<h1>SignificantToFractionDigits (
_value_: an Intl mathematical value,
_significantDigits_: a positive integer
Expand Down Expand Up @@ -481,7 +521,7 @@ location: https://github.com/tc39/proposal-amount/
1. Let _u_ be _O_.[[Unit]].
1. Let _v_ be _O_.[[Value]].
1. Let _fractionDigits_ be _O_.[[FractionDigits]].
1. Let _s_ be RenderAmountValueWithFractionDigits(_v_, _fractionDigits_).
1. Let _s_ be MVtoDecimalString(_v_, _fractionDigits_).
1. If _displayUnit_ is *"never"*, return _s_.
1. If _u_ is *undefined*, then
1. If _displayUnit_ is *"always"*, return the string-concatenation of _s_ and *"[1]"*.
Expand Down