toFixed()和toPrecision()之间的区别? [英] Difference between toFixed() and toPrecision()?
问题描述
我是JavaScript的新手,刚刚发现 toFixed()
和 toPrecision()
来回合数字。但是,我无法弄清楚两者之间的区别是什么。
I'm new to JavaScript and just discovered toFixed()
and toPrecision()
to round numbers. However, I can't figure out what the difference between the two is.
number.toFixed()$之间的区别是什么? c $ c>和
number.toPrecision()
?
推荐答案
toFixed(n)
在小数点后提供 n
长度; toPrecision(x)
提供 x
总长度。
toFixed(n)
provides n
length after the decimal point; toPrecision(x)
provides x
total length.
参考w3schools: toFixed 和 toPrecision
Ref at w3schools: toFixed and toPrecision
编辑:
我回过头来了解到w3schools并不是最好的来源,但我忘了这个答案,直到我看到kzh's,呃,热情的评论。以下是Mozilla Doc Center的其他参考资料toFixed( )
和toPrecision()
。对于我们所有人来说幸运的是,在这种情况下,MDC和w3schools彼此同意。
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()
只返回没有格式化的原始数字。
For completeness, I should mention that toFixed()
is equivalent to toFixed(0)
and toPrecision()
just returns the original number with no formatting.
这篇关于toFixed()和toPrecision()之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!