Jes*_*ica 115 javascript precision rounding
我是JavaScript的新手,刚刚发现toFixed()并toPrecision()编号.但是,我无法弄清楚两者之间的区别是什么.
number.toFixed()和之间有什么区别number.toPrecision()?
Pop*_*ops 125
toFixed(n)提供n小数点后的长度; toPrecision(x)提供x总长度.
参考w3schools:toFixed和toPrecision
编辑:
我回忆起w3schools并不是最好的来源,但我忘记了这个答案,直到我看到kzh's,呃,"热情"的评论.下面是从Mozilla的文档中心额外裁判的toFixed()和为toPrecision().幸运的是,对于我们所有人来说,MDC和w3schools在这种情况下相互认同.
为了完整性,我应该提到它toFixed()等同于toFixed(0)并且toPrecision()只返回没有格式化的原始数字.
Tom*_*Tom 55
我相信前者给你一个固定的小数位数,而后者给你一个固定的有效位数.
Math.PI.toFixed(2); // "3.14"
Math.PI.toPrecision(2); // "3.1"
Run Code Online (Sandbox Code Playgroud)
此外,如果数字中的整数位数多于指定的精度,toPrecision则会产生科学记数法.
(Math.PI * 10).toPrecision(2); // "31"
(Math.PI * 100).toPrecision(2); // "3.1e+2"
Run Code Online (Sandbox Code Playgroud)
编辑:哦,如果你是JavaScript的新手,我强烈推荐Douglas Crockford 撰写的" JavaScript:The Good Parts " 一书.
Dav*_*edy 10
我认为最好用一个例子来回答.
假设您有以下数据:
var products = [
{
"title": "Really Nice Pen",
"price": 150
},
{
"title": "Golf Shirt",
"price": 49.99
},
{
"title": "My Car",
"price": 1234.56
}
]
Run Code Online (Sandbox Code Playgroud)
您希望以标题和格式价格显示每个产品.我们toPrecision先试试吧:
document.write("The price of " + products[0].title + " is $" + products[0].price.toPrecision(5));
The price of Really Nice Pen is $150.00
Run Code Online (Sandbox Code Playgroud)
看起来不错,所以您可能会认为这也适用于其他产品:
document.write("The price of " + products[1].title + " is $" + products[2].price.toPrecision(5));
document.write("The price of " + products[2].title + " is $" + products[2].price.toPrecision(5));
The price of Golf Shirt is $49.990
The price of My Car is $1234.6
Run Code Online (Sandbox Code Playgroud)
不太好.我们可以通过更改每个产品的有效位数来解决这个问题,但是如果我们正在迭代可能很棘手的产品数组.让我们toFixed改用:
document.write("The price of " + products[0].title + " is $" + products[0].price.toFixed(2));
document.write("The price of " + products[1].title + " is $" + products[2].price.toFixed(2));
document.write("The price of " + products[2].title + " is $" + products[2].price.toFixed(2));
The price of Really Nice Pen is $150.00
The price of Golf Shirt is $49.99
The price of My Car is $1234.56
Run Code Online (Sandbox Code Playgroud)
这产生了你的期望.没有猜测工作,也没有四舍五入.
示例清楚地表明:
var A = 123.456789;
A.toFixed() // 123
A.toFixed(0) // 123
A.toFixed(1) // 123.5
A.toFixed(2) // 123.46
A.toFixed(3) // 123.457
A.toFixed(4) // 123.4568
A.toFixed(5) // 123.45679
A.toFixed(6) // 123.456789
A.toFixed(7) // 123.4567890
A.toFixed(8) // 123.45678900
A.toFixed(9) // 123.456789000
A.toFixed(10) // 123.4567890000
A.toFixed(11) // 123.45678900000
A.toPrecision() // 123.456789
A.toPrecision(0) // --- ERROR ---
A.toPrecision(1) // 1e+2
A.toPrecision(2) // 1.2e+2
A.toPrecision(3) // 123
A.toPrecision(4) // 123.5
A.toPrecision(5) // 123.46
A.toPrecision(6) // 123.457
A.toPrecision(7) // 123.4568
A.toPrecision(8) // 123.45679
A.toPrecision(9) // 123.456789
A.toPrecision(10) // 123.4567890
A.toPrecision(11) // 123.45678900
Run Code Online (Sandbox Code Playgroud)
小智 7
只是:
49.99.toFixed(5)
// ? "49.99000"
49.99.toPrecision(5)
// ? "49.990"
Run Code Online (Sandbox Code Playgroud)
在某些情况下,toPrecision()将返回指数符号,而toFixed()不会。
| 归档时间: |
|
| 查看次数: |
47827 次 |
| 最近记录: |