且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

限制在小数点后面显示的数字的数量

更新时间:2023-02-21 16:22:28

You're looking for toFixed:

var x = 4.3455;
alert(x.toFixed(2)); // alerts 4.35 -- not what you wanted!

...but it looks like you want to truncate rather than rounding, so:

var x = 4.3455;
x = Math.floor(x * 100) / 100;
alert(x.toFixed(2)); // alerts 4.34