JavaScript显示浮点数到2个小数位 [英] JavaScript displaying a float to 2 decimal places

查看:116
本文介绍了JavaScript显示浮点数到2个小数位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想我可以用 toPrecision(2)在JavaScript中。



然而,如果数字是 0.05 ,我得到 0.0500 。我宁愿保持不变。



上查看JSbin



最好的方法是做什么?

我可以考虑编码一些解决方案,但我想像(我希望)这样的东西内置?

解决方案

  float_num.toFixed(2); 

注意:

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?

解决方案

float_num.toFixed(2);

Note:toFixed() will round or pad with zeros if necessary to meet the specified length.

这篇关于JavaScript显示浮点数到2个小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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