gpt4 book ai didi

javascript - 什么时候通过 .toString() 将数字转换为指数表示法?

转载 作者:行者123 更新时间:2023-11-29 23:25:20 24 4
gpt4 key购买 nike

我目前正在调查 NodeJS 中一个可能存在的小错误/意外行为。

调查包括通过 .toString() 将数字转换为字符串.
我注意到对于满足 n <= 1e21 (roughly) 的 n , n 被转换为具有完整形式的数字(例如 999999999999999900000 ),但从大约 n === 1e21 开始, .toString()方法返回指数/科学计数法 (a * 10 ^ b)。

这似乎与我测试过的所有 JS 环境(Node、Chrome、Firefox)一致。

我认为这可能与 Number.MAX_SAFE_INTEGER 有关但似乎没有任何链接。

这个阈值是在哪里定义的?实际阈值是多少,为什么是这个数字?

最佳答案

经过更多挖掘,这似乎是在 ECMAScript Language Specification 中定义的.

a number will be rendered in exponential format if and only if the number's absolute value is greater than or equal to 10^21 or strictly smaller than 10^-6.
-(CRice)

完整的 ECMA 规范引用:

7.1.12.1 ToString Applied to the Number Type The abstract operation

ToString converts a Number m to String format as follows:

  1. If m is NaN, return the String "NaN".
  2. If m is +0 or −0, return the String "0".
  3. If m is less than zero, return the String concatenation of the String "-" and ToString(−m).
  4. If m is +∞, return the String "Infinity".
  5. Otherwise, let n, k, and s be integers such that k ≥ 1, 10k−1 ≤ s < 10k, the Number value for s × 10n−k is m, and k is as small aspossible. Note that k is the number of digits in the decimalrepresentation of s, that s is not divisible by 10, and that the leastsignificant digit of s is not necessarily uniquely determined by thesecriteria.
  6. If k ≤ n ≤ 21, return the String consisting of the code units of the kdigits of the decimal representation of s (in order, with no leadingzeroes), followed by n−k occurrences of the code unit 0x0030 (DIGITZERO).
  7. If 0 < n ≤ 21, return the String consisting of the code unitsof the most significant n digits of the decimal representation of s,followed by the code unit 0x002E (FULL STOP), followed by the codeunits of the remaining k−n digits of the decimal representation of s.
  8. If −6 < n ≤ 0, return the String consisting of the code unit 0x0030(DIGIT ZERO), followed by the code unit 0x002E (FULL STOP), followedby −n occurrences of the code unit 0x0030 (DIGIT ZERO), followed bythe code units of the k digits of the decimal representation of s.
  9. Otherwise, if k = 1, return the String consisting of the code unit ofthe single digit of s, followed by code unit 0x0065 (LATIN SMALLLETTER E), followed by the code unit 0x002B (PLUS SIGN) or the codeunit 0x002D (HYPHEN-MINUS) according to whether n−1 is positive ornegative, followed by the code units of the decimal representation ofthe integer abs(n−1) (with no leading zeroes).
  10. Return the Stringconsisting of the code units of the most significant digit of thedecimal representation of s, followed by code unit 0x002E (FULL STOP),followed by the code units of the remaining k−1 digits of the decimalrepresentation of s, followed by code unit 0x0065 (LATIN SMALL LETTERE), followed by code unit 0x002B (PLUS SIGN) or the code unit 0x002D(HYPHEN-MINUS) according to whether n−1 is positive or negative,followed by the code units of the decimal representation of theinteger abs(n−1) (with no leading zeroes).

关于javascript - 什么时候通过 .toString() 将数字转换为指数表示法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49523105/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com