JavaScript displaying a float to 2 decimal places

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);

注意:如果需要, toFixed()将圆或填充零以符合指定的长度。


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代替toPrecision

链接地址: http://www.djcxy.com/p/77462.html

上一篇: 将数字显示为2位小数

下一篇: JavaScript显示浮点数到2个小数位