建议去javascript版问
<script>
String.prototype.fn = function(n)
{ s=""
for(i=0;i<n;i++)s+=this
return s
}
Number.prototype.fix = function(num)
{with(Math)return (round(this.valueOf()*pow(10,num))/pow(10,num)).toString().search(/\./i)==-1?(round(this.valueOf()*pow(10,num))/pow(10,num)).toString()+"."+"0".fn(num):(round(this.valueOf()*pow(10,num))/pow(10,num));
}
alert((5.31-1.31).fix(2));
</script>
上边的代码返回任意数的保留n位小数值,为了考虑货币的效果(xx.xx)形势,所以长了一点儿,因为4.00的格式是不能显示的(以数字方式),如果不用顾及那个用下边的就行
<script>
Number.prototype.fix = function(num)
{with(Math)return round(this.valueOf()*pow(10,num))/pow(10,num);
}
alert((5.31-1.31).fix(2));
</script>这是为了兼容ie5.0
如果不考虑
直接用num.toFixed(n)就行了
<script>
String.prototype.fn = function(n)
{ s=""
for(i=0;i<n;i++)s+=this
return s
}
Number.prototype.fix = function(num)
{with(Math)return (round(this.valueOf()*pow(10,num))/pow(10,num)).toString().search(/\./i)==-1?(round(this.valueOf()*pow(10,num))/pow(10,num)).toString()+"."+"0".fn(num):(round(this.valueOf()*pow(10,num))/pow(10,num));
}
alert((5.31-1.31).fix(2));
</script>
上边的代码返回任意数的保留n位小数值,为了考虑货币的效果(xx.xx)形势,所以长了一点儿,因为4.00的格式是不能显示的(以数字方式),如果不用顾及那个用下边的就行
<script>
Number.prototype.fix = function(num)
{with(Math)return round(this.valueOf()*pow(10,num))/pow(10,num);
}
alert((5.31-1.31).fix(2));
</script>这是为了兼容ie5.0
如果不考虑
直接用num.toFixed(n)就行了
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货