超过位数的直接截取,采用四舍五入(若不采用四舍五入,截取2位,则把Math.round改为Math.floor)
例如
2 → 2.00
2.3 → 2.30
2.321 → 2.32
2.328 → 2.33
代码如下:
function changeTwoDecimal_f(x)
{
var f_x = parseFloat(x);
if (isNaN(f_x))
{
return 0;
}
var f_x = Math.round(x*100)/100;
var s_x = f_x.toString();
var pos_decimal = s_x.indexOf('.');
if (pos_decimal < 0)
{
pos_decimal = s_x.length;
s_x += '.';
}
while (s_x.length <= pos_decimal + 2)
{
s_x += '0';
}
return s_x;
}