简体   繁体   English

如果不用科学记数法显示,JavaScript编号有多小?

[英]How small can a JavaScript number be without displaying in scientific notation?

I've made some code that rounds off a number to a given number of decimal places but will use more places when there is underflow to avoid rounding just to zero, which wouldn't be informative enough for the user. 我已经制作了一些代码,将一个数字四舍五入到给定的小数位数,但是当有下溢时会使用更多的位置以避免舍入到零,这对用户来说不够丰富。

But I've noticed that occasionally a small number is displayed in scientific notation. 但我注意到偶尔会用科学记数法显示一小部分。 My target users are not expected to be comfortable with scientific notation and in any case such numbers are too small even after handling the underflow and should be treated as zero. 我的目标用户不会对科学记数法感到满意,并且在任何情况下,即使在处理下溢之后这些数字也太小,应该被视为零。

How can I know when a number is so small that it would display in scientific notation? 我怎么知道数字何时小到能用科学记数法显示?

Is there a smallest number I can compare to and if so what is it? 我可以比较一个最小的数字,如果是,它是什么? Or is there a better way? 或者,还有更好的方法?

(I know I can convert the number to a string and check if it has an "e" but I'm the type who is curious to know of any math-only solution too.) (我知道我可以将数字转换为字符串并检查它是否有“e”但我是那种很想知道任何数学解决方案的类型。)

There are a few reasons the number is displayed in scientific notation: 这个数字用科学记数法显示有几个原因:

  • If the length of significant digits in the number is greater than 21. 如果数字中有效数字的长度大于21。
  • If the number of zeros before a nonzero number is greater than 6. (eg 0.00000001) 如果非零数字之前的零数大于6.(例如0.00000001)

The conversion to scientific notation in numbers is specified in Section 9.8.1 of ECMA-262 . ECMA-262的第9.8.1节规定了数字转换为科学记数法。

An implementation of this specification can be found in the V8 engine here . 此规范的实现可在此处V8引擎中找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM