不使用科学计数法显示的JavaScript编号可以有多小? [英] How small can a JavaScript number be without displaying in scientific notation?

查看:79
本文介绍了不使用科学计数法显示的JavaScript编号可以有多小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一些代码,可以将数字四舍五入到给定的小数位数,但是在发生下溢时会使用更多的位,以避免四舍五入到零,这对于用户来说不够有用.

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?

(我知道我可以将数字转换为字符串,并检查它是否具有"e",但我也是想知道任何仅数学解决方案的类型.)

(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.)

推荐答案

使用科学计数法显示数字有以下几个原因:

There are a few reasons the number is displayed in scientific notation:

  • 如果数字中有效数字的长度大于21.
  • 如果非零数字之前的零个数大于6(例如0.00000001)

中指定了将数字转换为科学计数法的方法.ECMA-262第9.8.1节.

可以在 V8中找到该规范的实现引擎.

这篇关于不使用科学计数法显示的JavaScript编号可以有多小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆