diff --git a/intl.emu b/intl.emu index 4f3d209..221f438 100644 --- a/intl.emu +++ b/intl.emu @@ -152,7 +152,7 @@ - 1. If _value_ has the [[Value]] and [[FractionDigits]] internal slots, let _primValue_ be RenderAmountValueWithFractionDigits(_value_.[[Value]], _value_.[[FractionDigits]]) elseLet letIf _value_ has the [[Value]] and [[FractionDigits]] internal slots, let _primValue_ be MVtoDecimalString(_value_.[[Value]], _value_.[[FractionDigits]]) elseLet let + +

Numbers and Dates

+ +

Number Objects

+ +

Properties of the Number Prototype Object

+
+ +

Number.prototype.toFixed ( _fractionDigits_ )

+ +

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.

+
+

It performs the following steps when called:

+ + 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_ < 0 or _f_ > 100, throw a *RangeError* exception. + 1. If _x_ is not finite, return Number::toString(_x_, 10). + 1. Return MVtoDecimalString(ℝ(_x_), _f_). + 1. Set _x_ to ℝ(_x_). + 1. Let _s_ be the empty String. + 1. If _x_ < 0, then + 1. Set _s_ to *"-"*. + 1. Set _x_ to -_x_. + 1. If _x_ ≥ 1021, then + 1. Let _m_ be ! ToString(𝔽(_x_)). + 1. Else, + 1. Let _n_ be an integer for which _n_ / 10_f_ - _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 _f_ ≠ 0, then + 1. Let _k_ be the length of _m_. + 1. If _k_ ≤ _f_, then + 1. Let _z_ be the String value consisting of _f_ + 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 _f_ + 1. + 1. Let _a_ be the first _k_ - _f_ code units of _m_. + 1. Let _b_ be the other _f_ code units of _m_. + 1. Set _m_ to the string-concatenation of _a_, *"."*, and _b_. + 1. Return the string-concatenation of _s_ and _m_. + + +

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,

+

+ `(1000000000000000128).toString()` returns *"1000000000000000100"*, while
+ `(1000000000000000128).toFixed(0)` returns *"1000000000000000128"*. +

+
+ +

+ MVtoDecimalString ( + _x_: a mathematical value, + _fractionDigits_ : a non-negative integer + ): a String +

+
+
description
+
It renders the given Intl mathematical value with a given number of fractional digits
+
+ + 1. Let _s_ be the empty String. + 1. If _x_ < 0, then + 1. Set _s_ to *"-"*. + 1. Set _x_ to -_x_. + 1. If _x_ ≥ 1021, then + 1. Let _m_ be ! ToString(𝔽(_x_)). + 1. Else, + 1. Let _n_ be an integer for which _n_ / 10_fractionDigits_ - _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_. + +
+
+
+
+

The Amount Object

@@ -262,79 +348,33 @@ location: https://github.com/tc39/proposal-amount/
- -

RenderAmountValueWithFractionDigits ( - _v_: an Intl mathematical value, - _numDigits_: an integer, + +

RoundToSignificantDigits ( + _v_: a mathematical value, + _n_: a non-negative integer, optional _roundingMode_: a rounding mode - ): a String + ): a mathematical value

description
-
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"*.
+
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.
- 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_numDigits_, _roundingMode_). - 1. Let _e_ be the smallest non-negative integer such that _rounded_ × 10-_numDigits_ 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_p_ < 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_. - -
- - -

FractionToSignificantDigits ( - _value_: an Intl mathematical value, - _fractionDigits_: an integer - ): a non-negative integer -

-
- - 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_integerDigits_ > abs(_value_). - 1. Assert: _integerDigits_ + _fractionDigits_ > 0. - 1. Return _integerDigits_ + _fractionDigits_. + 1. If _v_ = 0, return 0. + 1. If v < 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_e_ ≤ _v_ < 10_e_+1. + 1. Let _pow_ be _e_ - _n_. + 1. Let _m_ be _v_ × 10–_pow_. + 1. Let _rounded_ be ApplyRoundingModeToPositive(_m_, _roundingMode_). + 1. Return _rounded_ × 10_n_ + _e_.
- +

SignificantToFractionDigits ( _value_: an Intl mathematical value, _significantDigits_: a positive integer @@ -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]"*.