例1,使数字1111111变成11,111,111.00,保留两位小数。 复主代码 代码如下: <br>function outputmoney(number) {<br>number = number.replace(/,/g, "");<br>if(isNaN(number) || 숫자 == "")return "";<br>number = Math.round(number * 100) / 100;<br> if (number < 0)<BR> return '-' outputdollars(Math.floor (Math.abs(숫자) - 0) '') 출력센트(Math.abs(숫자) - 0);<BR> else<BR> return outputdollars(Math.floor(숫자 - 0) '') 출력센트(숫자 - 0);<BR>} <BR>//格式化金额<BR>function outputdollars(number) {<BR> if (number.length <= 3)<BR> return (number == '' ? '0 ' : 숫자);<BR> else {<BR> var mod = number.length % 3;<BR> var 출력 = (mod == 0 ? '' : (number.substring(0, mod)));<BR> for (i = 0; i < Math.floor(number.length / 3); i ) {<BR> if ((mod == 0) && (i == 0))<BR> 출력 = 숫자 .substring(mod 3 * i, mod 3 * i 3);<BR> else<BR> 출력 = ',' 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> 반품(금액 < 10? '.0' 금액 : '.' amount);<BR>}<BR> 지금 线运行: [Ctrl A 전체选 注:如需引入외부Js需刷新才能执行] 例2,使数字1111111变成11,111,111---和使11,111,111变成1111111---整数-- 复主代码 代码如下: 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); } js 格式化金额的代码---www.jb51.net<br>function tran(id)<br>{<br> var v, j, sj, rv = "";<br> v = id.value.replace(/,/g,"").split(".");<br> j = v[0].length % 3;<br> sj = v[0].substr(j).toString();<br> for (var i = 0; i < sj.length; i++)<BR> {<BR> rv = (i % 3 == 0) ? rv + "," + sj.substr(i, 1): rv + sj.substr(i, 1);<BR> }<BR> var rvalue = (v[1] == undefined) ? v[0].substr(0, j) + rv: v[0].substr(0, j) + rv + "." + v[1];<BR> if (rvalue.charCodeAt(0) == 44)<BR> {<BR> rvalue = rvalue.substr(1);<BR> }<BR> id.value = rvalue;<BR>}<BR>function tran2(id)<BR>{<BR> var v;<BR> v = id.value.replace(/,/g,"");<BR> alert(v);<BR>}<BR><br><!--<BR>body,td,th,input {<BR> font-size: 12px;<BR>}<BR>--><br> 运行测试: js