例1,使数字1111111变成11,111,111.00,保留两位小数。 复制代码 代码如下: <BR>function outputmoney(number) {<BR>number = number.replace(/\,/g, "");<BR>if(isNaN(number) || number == "")return "";<BR>number = Math.round(number * 100) / 100;<BR> if (number < 0)<BR> return '-' + outputdollars(Math.floor(Math.abs(number) - 0) + '') + outputcents(Math.abs(number) - 0);<BR> else<BR> return outputdollars(Math.floor(number - 0) + '') + outputcents(number - 0);<BR>} <BR>//格式化金额<BR>function outputdollars(number) {<BR> if (number.length <= 3)<BR> return (number == '' ? '0' : number);<BR> else {<BR> var mod = number.length % 3;<BR> var output = (mod == 0 ? '' : (number.substring(0, mod)));<BR> for (i = 0; i < Math.floor(number.length / 3); i++) {<BR> if ((mod == 0) && (i == 0))<BR> output += number.substring(mod + 3 * i, mod + 3 * i + 3);<BR> else<BR> output += ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);<BR> }<BR> return (output);<BR> }<BR>}<BR>function outputcents(amount) {<BR> amount = Math.round(((amount) - Math.floor(amount)) * 100);<BR> return (amount < 10 ? '.0' + amount : '.' + amount);<BR>}<BR> 在线运行: function outputmoney(number) { number = number.replace(/\,/g, ""); if(isNaN(number) || number == "")return ""; number = Math.round(number * 100) / 100; if (number < 0) return '-' + outputdollars(Math.floor(Math.abs(number) - 0) + '') + outputcents(Math.abs(number) - 0); else return outputdollars(Math.floor(number - 0) + '') + outputcents(number - 0); } //格式化金额 function outputdollars(number) { if (number.length <= 3) return (number == '' ? '0' : number); else { var mod = number.length % 3; var output = (mod == 0 ? '' : (number.substring(0, mod))); for (i = 0; i < Math.floor(number.length / 3); i++) { if ((mod == 0) && (i == 0)) output += number.substring(mod + 3 * i, mod + 3 * i + 3); else output += ',' + number.substring(mod + 3 * i, mod + 3 * i + 3); } return (output); } } function outputcents(amount) { amount = Math.round(((amount) - Math.floor(amount)) * 100); return (amount < 10 ? '.0' + amount : '.' + amount); } [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行] 例2,使数字1111111变成11,111,111---和使11,111,111变成1111111---整数-- 复制代码 代码如下: js