代码收藏

收集网站开发时常用到的代码(asp,php,js等)

« getcon - 截取函数(itlearner原创)JSON-PHP是一个自动完成JSON编码和解码的php开源代码工具 »

javascript计算小数保留两位小数,多位小数的方法

<SCRIPT LANGUAGE="JavaScript">
<!--
function formatFloat(src, pos)
{
    return Math.round(src*Math.pow(10, pos))/Math.pow(10, pos);
}

alert(formatFloat("1212.2323", 2));
//-->
</SCRIPT>


还有如下几种方法:

var test=88888.234; 
alert(test.toFixed(2)); 

// Float数据四舍五入到2位小数; 
function to2bits(flt) { 
if(parseFloat(flt) == flt) 
return Math.round(flt * 100) / 100; 
// 到4位小数, return Math.round(flt * 10000) / 10000; 
else 
return 0; 

  • 相关文章:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新评论及回复

最近发表

Powered By Z-Blog 1.8 Spirit Build 80605

Copyright 2008 ITlearner. Some Rights Reserved.