我是 JavaScript 的新手,刚刚发现了 toFixed()
和 toPrecision()
来对数字进行舍入。但是,我无法弄清楚两者之间的区别是什么。
number.toFixed()
和 number.toPrecision()
有什么区别?
最佳答案
toFixed(n)
提供小数点后的n
长度; toPrecision(x)
提供 x
总长度。
w3schools 的引用资料:toFixed和 toPrecision
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 fortoFixed()
and fortoPrecision()
. 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/