使用指数的javascript数字文字 [英] javascript number literal using exponent
问题描述
console.log(2E-12);//返回 2E-12控制台日志(2E12);//返回 2000000000000
为什么第一行返回 2E-12 而与第二行不同.这是使用指数的非法方式吗?
7.如果0<n ≤ 21,返回由s的十进制表示的最高有效n位数字组成的String,后跟小数点'.',后跟s的十进制表示的剩余k-n位.
8.如果-6<n ≤ 0,返回由字符0"、小数点."、-n次出现的字符0"和s的十进制表示的k位组成的字符串.>
9.否则,如果 k = 1,则根据 n−1 是正数还是负数,返回由 s 的单个数字组成的字符串,后跟小写字符e",后跟加号+"或减号-", 后跟整数 abs(n−1) 的十进制表示(没有前导零).
n
实际上是数字的指数.所以这就是说如果指数在 -7
和 21
之间,数字应该正常显示,否则应该使用指数表示法.
console.log(2E-12); // returns 2E-12
console.log(2E12); // returns 2000000000000
Why does line one return 2E-12 and not the same as line two. Is this an illegal way of using the exponent?
From the ECMAScript specification of toString applied to the Number type:
7. If 0 < n ≤ 21, return the String consisting of the most significant n digits of the decimal representation of s, followed by a decimal point ‘.’, followed by the remaining k−n digits of the decimal representation of s.
8. If −6 < n ≤ 0, return the String consisting of the character ‘0’, followed by a decimal point ‘.’, followed by −n occurrences of the character ‘0’, followed by the k digits of the decimal representation of s.
9. Otherwise, if k = 1, return the String consisting of the single digit of s, followed by lowercase character ‘e’, followed by a plus sign ‘+’ or minus sign ‘−’ according to whether n−1 is positive or negative, followed by the decimal representation of the integer abs(n−1) (with no leading zeroes).
n
is effectively the exponent of the number. So this says that if the exponent is between -7
and 21
the number should be displayed normally, otherwise exponential notation should be used.
这篇关于使用指数的javascript数字文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!