javascript - toFixed() 和 toPrecision() 之间的区别?

标签 javascript rounding precision

我是 JavaScript 的新手,刚刚发现了 toFixed()toPrecision() 来对数字进行舍入。但是,我无法弄清楚两者之间的区别是什么。

number.toFixed()number.toPrecision() 有什么区别?

最佳答案

toFixed(n) 提供小数点后的n长度; toPrecision(x) 提供 x 总长度。

w3schools 的引用资料:toFixedtoPrecision

EDIT:
I learned a while back that w3schools isn't exactly the best source, but I forgot about this answer until I saw kzh's, uh, "enthusiastic" comment. Here are additional refs from Mozilla Doc Center for toFixed() and for toPrecision(). Fortunately for all of us, MDC and w3schools agree with each other in this case.

为了完整性,我应该提到 toFixed() 等同于 toFixed(0)toPrecision() 只是返回原始数字没有格式。

关于javascript - toFixed() 和 toPrecision() 之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3337849/

相关文章:

javascript - 尽管使用目标 ="_blank",但 React Markdown 链接不会在新选项卡中打开

math - ColdFusion 圆函数

c - 如何在 Linux 中创建高分辨率计时器来测量程序性能?

math - float 学有问题吗?

r - R 中浮点精度的极端数值

javascript - 使用 <a> 标签在 JqueryUI 选项卡中导航

javascript - CFM 文件中的 jQuery AJAX 和函数

javascript - 如何将 React 应用程序的生产 npm 版本部署到 Azure 应用服务

JavaScript - 将 3 个数字转换为百分比,不会产生 100% 的总数

r - 在 R 中使用 Rmpfr 进行精确舍入