You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Support Infinity, NaN, and -0 as strings
* Update AO name
* Update spec.emu
Co-authored-by: Eemeli Aro <[email protected]>
* Update spec.emu
Co-authored-by: Eemeli Aro <[email protected]>
* Use 402's `StringIntlMV` SDO
* Use existing terminology
* Remove unnecessary assertions
* Support -0 with precision
Also, copy the definition of the `StringIntlMV` from 402.
* Borrow & use StringIntlMV from proposal-intl-keep-trailing-zeros
Ref: http://tc39.es/proposal-intl-keep-trailing-zeros/#sec-runtime-semantics-stringintlmv
* Calculate significant digits directly from the rounded value.
Also, remove `CountSignificantDigits` AO, which is no longer needed.
---------
Co-authored-by: Eemeli Aro <[email protected]>
Copy file name to clipboardExpand all lines: intl.emu
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -152,7 +152,7 @@
152
152
</dd>
153
153
</dl>
154
154
<emu-alg>
155
-
1. <ins>If _value_ has the [[Value]] and [[FractionDigits]] internal slots, let _primValue_ be RenderMVWithFractionDigits(_value_.[[Value]], _value_.[[FractionDigits]]) else</ins><del>Let</del> <ins>let</ins _primValue_ be ? ToPrimitive(_value_, ~number~).
155
+
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~).
156
156
1. If _primValue_ is a BigInt, return ℝ(_primValue_).
conversion of a |StringNumericLiteral| to a Number value is similar overall to the determination of the NumericValue of a |NumericLiteral| (see <emu-xref href="#sec-literals-numeric-literals"></emu-xref>), but some of the details are different.</p>
84
+
</del>
85
+
<ins class="block">
86
+
<p>
87
+
The conversion of a |StringNumericLiteral| to a mathematical value and a precision is similar overall to the determination of the NumericValue of a |NumericLiteral| (see <emu-xref href="#sec-literals-numeric-literals"></emu-xref>), but some of the details are different.
88
+
The result of StringIntlMV is a List value with two elements, a mathematical value and the count of decimal digits in the source text.
<dd>It renders the given mathematical value with a given number of fractional digits, possibly rounding if necessary, using the given rounding mode, which, if missing, is *"halfEven"*.</dd>
270
+
<dd>It renders the given mathematical value or a suitable enum (~positive-infinity~, ~negative-infinity~, ~not-a-number~, or ~minus-zero~) with a given number of fractional digits, possibly rounding if necessary, using the given rounding mode, which, if missing, is *"halfEven"*.</dd>
215
271
</dl>
216
272
<emu-alg>
273
+
1. If _v_ is ~not-a-number~, return *"NaN"*.
274
+
1. If _v_ is ~negative-infinity~, return *"-Infinity"*.
275
+
1. If _v_ is ~positive-infinity~, return *"Infinity"*.
217
276
1. If _roundingMode_ is *undefined*, let _mode_ be *"halfEven"*, else let _mode_ be _roundingMode_.
218
277
1. If _v_ < 0, let _prefix_ be *"-"*, else let _prefix_ be "".
1. Else if _x_ is a Number, set _toParse_ to Number::toString(_x_, 10).
276
335
1. Otherwise, if _x_ is a String, set _toParse_ to _x_.
277
336
1. If _toParse_ is not a String, throw a *TypeError* exception.
278
-
1. If _toParse_ is in « *"NaN"*, *"Infinity"*, *"-Infinity"* », throw a *RangeError* exception.
279
-
1. Let _parseResult_ be ParseText(_toParse_, |StrDecimalLiteral|).
337
+
1. Let _intlMV_ be the StringIntlMV of _toParse_.
338
+
1. Let _amountValue_ be _intlMV_[0].
339
+
1. Let _numDigits_ be _intlMV_[1].
280
340
1. Let _validatedOpts_ be ? GetAmountOptions(_opts_).
281
-
1. If _parseResult_ is a List of errors, throw a *SyntaxError* exception.
282
-
1. Let _amountValue_ be ? StringDecimalValue of _parseResult_.
283
341
1. Let _fractionDigits_ be _validatedOpts_.[[FractionDigits]].
284
342
1. Let _roundingMode_ be _validatedOpts_.[[RoundingMode]].
285
343
1. Let _significantDigits_ be _validatedOpts_.[[SignificantDigits]].
286
344
1. Let _unit_ be _validatedOpts_.[[Unit]].
287
-
1. Let _roundedValue_ be _amountValue_.
288
-
1. Assert: If _fractionDigits_ is not *undefined*, then _significantDigits_ is *undefined*.
289
-
1. Assert: If _significantDigits_ is not *undefined*, then _fractionDigits_ is *undefined*.
290
-
1. If both _significantDigits_ and _fractionDigits_ are *undefined*, then
291
-
1. Set _significantDigits_ to CountSignificantDigits(_toParse_).
292
-
1. Set _fractionDigits_ to CountFractionDigits(_toParse_).
293
-
1. Else if _significantDigits_ is *undefined*, then
294
-
1. Set _roundedValue_ be RoundToFractionDigits(_amountValue_, _fractionDigits_, _roundingMode_).
295
-
1. Let _digitStr_ be the unique decimal string representation of _roundedValue_ without leading zeroes.
296
-
1. Set _significantDigits_ to CountSignificantDigits(_digitStr_).
297
-
1. Otherwise:
298
-
1. Set _roundedValue_ be RoundToSignificantDigits(_amountValue_, _significantDigits_, _roundingMode_).
299
-
1. Let _digitStr_ be the unique decimal string representation of _roundedValue_ without leading zeroes.
300
-
1. Set _fractionDigits_ to CountFractionDigits(_digitStr_).
301
345
1. Let _O_ be OrdinaryObjectCreate(%Amount.prototype%, « [[FractionDigits]], [[SignificantDigits]], [[Unit]], [[Value]] »).
302
-
1. Set _O_.[[Value]] to _roundedValue_.
303
-
1. Set _O_.[[SignificantDigits]] to _significantDigits_.
304
-
1. Set _O_.[[FractionDigits]] to _fractionDigits_.
346
+
1. If _amountValue_ is a mathematical value, then
347
+
1. Let _roundedValue_ be _amountValue_.
348
+
1. If both _significantDigits_ and _fractionDigits_ are *undefined*, then
349
+
1. Set _significantDigits_ to _numDigits_.
350
+
1. Set _fractionDigits_ to CountFractionDigits(_toParse_).
351
+
1. Else if _significantDigits_ is *undefined*, then
352
+
1. Set _roundedValue_ be RoundAmountValueToFractionDigits(_amountValue_, _fractionDigits_, _roundingMode_).
353
+
1. Let _e_ be the smallest non-negative integer such that _roundedValue_ × 10<sup>-_e_</sup> is an integer.
354
+
1. Let _scaledRoundedValue_ be _roundedValue_ × 10<sup>-_e_</sup>.
355
+
1. If _scaledRoundedValue_ = 0, then
356
+
1. Set _significantDigits_ to 1.
357
+
1. Else,
358
+
1. Let _l_ be the log-10 of abs(_scaledRoundedValue_).
359
+
1. Set _significantDigits_ to floor(_l_) + 1.
360
+
1. Otherwise:
361
+
1. Set _roundedValue_ be RoundToSignificantDigits(_amountValue_, _significantDigits_, _roundingMode_).
362
+
1. Let _digitStr_ be the unique decimal string representation of _roundedValue_ without leading zeroes.
363
+
1. Set _fractionDigits_ to CountFractionDigits(_digitStr_).
364
+
1. Set _O_.[[Value]] to _roundedValue_.
365
+
1. Set _O_.[[SignificantDigits]] to _significantDigits_.
366
+
1. Set _O_.[[FractionDigits]] to _fractionDigits_.
367
+
1. Else if _amountValue_ is ~minus-zero~, then
368
+
1. Set _O_.[[Value]] to ~minus-zero~.
369
+
1. Set _O_.[[SignificantDigits]] to _numDigits_.
370
+
1. Assert: _numDigits_ ≥ 1.
371
+
1. Set _O_.[[FractionDigits]] to _numDigits_ - 1.
372
+
1. Otherwise:
373
+
1. Set _O_.[[Value]] to _amountValue_.
374
+
1. Set _O_.[[SignificantDigits]] to _numDigits_..
375
+
1. Set _O_.[[FractionDigits]] to 0.
305
376
1. If _unit_ is not *undefined*, set _O_.[[Unit]] to _unit_.
306
377
1. Return _O_.
307
378
</emu-alg>
308
379
<emu-note>
309
380
<p>Given a Number argument, the constructor converts it to a String using the <emu-xref href="#sec-number.prototype.tostring">toString</emu-xref> method (with no arguments). In some cases, this may not be desired; consider passing in a String form of a Number using the <emu-xref href="#sec-number.prototype.tofixed">toFixed</emu-xref> or <emu-xref href="#sec-number.prototype.toprecision">toPrecision</emu-xref> methods.</p>
0 commit comments