例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 '-' outputdols(Mathath. number) - 0) '') outputcents(Math.abs(number) - 0);<BR> else<BR> return outputdollars(Math.floor(number - out0) '' >} <BR>//計算金額<BR>function outputdollars(number) {<BR> if (number.length <= 3)<BR> > else {<BR> var mod = number.length % 3;<BR> var 輸出= ( = 0 ; i <Math.floor(number.length / 3); i ) {<BR> if ((mod == 0) && (i == 0))<BR> i, mod 3 * i 3);<BR> else<BR> }<BR> return (輸出);<BR> }<BR>}<BR>函數輸出cents(金額) {<BR> amount = Math.round(((金額) - Math.floor(金額)) * 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); } function tran(id) { var v, j, sj, rv = ""; v = id.value.replace(/,/g,"").split("."); j = v[0].length % 3; sj = v[0].substr(j).toString(); for (var i = 0; i < sj.length; i++) { rv = (i % 3 == 0) ? rv + "," + sj.substr(i, 1): rv + sj.substr(i, 1); } var rvalue = (v[1] == undefined) ? v[0].substr(0, j) + rv: v[0].substr(0, j) + rv + "." + v[1]; if (rvalue.charCodeAt(0) == 44) { rvalue = rvalue.substr(1); } id.value = rvalue; } function tran2(id) { var v; v = id.value.replace(/,/g,""); alert(v); } 程式碼如下: 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