JavaScript 显示精确到小数点后两位的浮点数
我想显示一个精确到小数点后两位的数字。
我想我可以在 JavaScript 中使用 toPrecision(2)
。
但是,如果数字是 0.05
,我会得到 0.0500
。我宁愿它保持不变。
请在 JSbin 上查看。
最好的方法是什么?
我可以考虑编写一些解决方案,但我想(我希望)内置了这样的东西?
I wanted to display a number to 2 decimal places.
I thought I could use toPrecision(2)
in JavaScript .
However, if the number is 0.05
, I get 0.0500
. I'd rather it stay the same.
See it on JSbin.
What is the best way to do this?
I can think of coding a few solutions, but I'd imagine (I hope) something like this is built in?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
注意:
toFixed()
将舍入或用零填充。Note:
toFixed()
will round or pad with zeros if necessary to meet the specified length.您可以使用
toFixed
函数来完成此操作,但它在 IE 中存在错误。如果您想要可靠的解决方案,请在此处查看我的答案。You could do it with the
toFixed
function, but it's buggy in IE. If you want a reliable solution, look at my answer here.使用
toFixed
您可以像这样设置小数点长度:但是
toFixed
返回一个字符串,并且如果number
根本没有小数点它会添加多余的零。为了避免这种情况,您必须将结果转换为数字。你可以用这两种方法来做到这一点:
with
toFixed
you can set length of decimal points like this:but
toFixed
returns a string and also ifnumber
doesn't have decimal point at all it will add redundant zeros.to avoid this you have to convert the result to a number. you can do this with these two methods:
number.parseFloat(2)
有效,但它返回一个字符串。如果您想将其保留为数字类型,可以使用:
Math.round(number * 100) / 100
number.parseFloat(2)
works but it returns a string.If you'd like to preserve it as a number type you can use:
Math.round(number * 100) / 100
不知道我是如何想到这个问题的,但即使这个问题已经问了很多年了,我想添加一个我遵循的快速而简单的方法,它从未让我失望:
Don't know how I got to this question, but even if it's many years since this has been asked, I would like to add a quick and simple method I follow and it has never let me down:
尝试
toFixed
而不是toPrecision
。Try
toFixed
instead oftoPrecision
.函数round(值,小数){
return Number(Math.round(值+'e'+小数)+'e-'+小数);
}
答案如下: http://www. jacklmoore.com/notes/rounding-in-javascript/
function round(value, decimals) {
return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}
The answer is following this link: http://www.jacklmoore.com/notes/rounding-in-javascript/
如果你需要 2 位数字而不是字符串类型,我就用这种方法。
I used this way if you need 2 digits and not string type.
您可以尝试混合使用
Number()
和toFixed()
。将您的目标数字转换为具有 X 位数字的漂亮字符串,然后将格式化的字符串转换为数字。
Number( (myVar).toFixed(2) )
请参阅下面的示例:
You could try mixing
Number()
andtoFixed()
.Have your target number converted to a nice string with X digits then convert the formated string to a number.
Number( (myVar).toFixed(2) )
See example below:
这里的语法
digits 参数是可选的,默认为 0。返回类型是字符串而不是数字。但是您可以使用它将其转换为数字
它也可以抛出异常,例如
TypeError
、RangeError
这里是 完整详细信息以及浏览器中的兼容性。
and here is the syntax
digits argument is optional and by default is 0. And the return type is string not number. But you can convert it to number using
It also can throws exceptions like
TypeError
,RangeError
Here is the full detail and compatibility in the browser.
还有
Intl
API 可根据您的区域设置值设置小数格式。如果小数点分隔符不是点“.”,这一点尤其重要。而是用逗号“,”代替,就像德国的情况一样。请注意,这将最多四舍五入到小数点后 3 位,就像上面默认情况下建议的 round() 函数一样。如果您想自定义该行为以指定小数位数,可以选择最小和最大小数位数:
There's also the
Intl
API to format decimals according to your locale value. This is important specially if the decimal separator isn't a dot "." but a comma "," instead, like it is the case in Germany.Note that this will round to a maximum of 3 decimal places, just like the
round()
function suggested above in the default case. If you want to customize that behavior to specify the number of decimal places, there're options for minimum and maximum fraction digits:我已经做了这个功能。它工作正常但返回字符串。
I have made this function. It works fine but returns string.