现在的位置: 首页 > 综合 > 正文

js数字格式化

2013年10月08日 ⁄ 综合 ⁄ 共 598字 ⁄ 字号 评论关闭

 

<script>
	var format = function(num, options) {
options.point=options.point||'.';
options.group=options.group||',';
options.suffix=options.suffix||'';
options.prefix=options.prefix||'';
if (typeof(options.places)=="undefined")
{	
	options.places=2;
} 
regex = /(\d+)(\d{3})/;
result = ((isNaN(num) ? 0 : Math.abs(num)).toFixed(options.places)) + '';
for (result = result.replace('.', options.point); regex.test(result) && options.group; result=result.replace(regex, '$1'+options.group+'$2')) {
	console.debug(regex.exec(result));
	
	};
return (num < 0 ? '-' : '') + options.prefix + result + options.suffix;
};
alert(format(12345678.34555,{places:2}));
</script>

抱歉!评论已关闭.