JS格式化数据的问题? <script language="JavaScript">var x = 233213.321312;alert(x.toFixed(2))</script> 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1,xx=0.02018如果是保留3位小數alert(Math.round(parseFloat(xx)*1000)/1000)四位alert(Math.round(parseFloat(xx)*10000)/10000)以此類推2,toFixed(2)有版本限制,最好不要用(ie6有效)<SCRIPT LANGUAGE=javascript> alert(123.4556.toFixed(2)) </SCRIPT>3,<script language=JavaScript>function tofloat(f,dec) { if(dec<0) return "Error:dec<0!"; result=parseInt(f)+(dec==0?"":"."); f-=parseInt(f); if(f==0) for(i=0;i<dec;i++) result+='0'; else { for(i=0;i<dec;i++) f*=10; result+=parseInt(Math.round(f)); } return result; } alert(tofloat(11.20000000000000000001,2))</script> <script language="javascript">Number.prototype.toFixed = function(len){ //IE5.0不支持toFiexed,我们可以自己写函数实现. return Math.round(this * Math.pow(10,len))/Math.pow(10,len)}alert((20.52).toFixed(1))</script> 输出select option问题 时间控件问题,哪个大侠帮我下 如何用js实现对img路径的检测并添加属性? 关于 IE BACK 按钮问题 javascript:一個ifram問題求教(請高手快來) setAttributeNS和setAttribute有什么区别? JavaScript中引用CSS滤镜应用在隔行换表格的颜色中无效? 关于button和table的问题 关闭窗口的问题?怎样才有确认和取消 请问如何在多xsl中共用一javascript脚本库??在html中我已知。 一段javascript代码的提问,急!明白了马上给分!! 如何书写帧?
如果是保留3位小數
alert(Math.round(parseFloat(xx)*1000)/1000)
四位
alert(Math.round(parseFloat(xx)*10000)/10000)
以此類推2,
toFixed(2)有版本限制,最好不要用(ie6有效)
<SCRIPT LANGUAGE=javascript>
alert(123.4556.toFixed(2))
</SCRIPT>
3,
<script language=JavaScript>
function tofloat(f,dec) {
if(dec<0) return "Error:dec<0!";
result=parseInt(f)+(dec==0?"":".");
f-=parseInt(f);
if(f==0)
for(i=0;i<dec;i++) result+='0';
else {
for(i=0;i<dec;i++) f*=10;
result+=parseInt(Math.round(f));
}
return result;
}
alert(tofloat(11.20000000000000000001,2))
</script>
Number.prototype.toFixed = function(len)
{
//IE5.0不支持toFiexed,我们可以自己写函数实现.
return Math.round(this * Math.pow(10,len))/Math.pow(10,len)
}
alert((20.52).toFixed(1))
</script>