toFixed()和toPrecision()之间的区别? [英] Difference between toFixed() and toPrecision()?

查看:218
本文介绍了toFixed()和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() 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 for toFixed() and for toPrecision(). 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屋!

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