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;
}

Popularity: 7%

No Responses to “javascript计算小数保留两位小数,多位小数的方法”

Leave a Reply:

Name (required):
Mail (will not be published) (required):
Website:
Comment (required):
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>