這篇文章主要介紹了js中將數字格式化為金額的方法,使用加減逗號處理金額,一個格式化金額的代碼,有需要的朋友參考下
例1,使數字1111111變成11,111,111.00,保留兩位小數。 代碼如下: <html> <head> <script type="text/javascript"> 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); } </script> </head> <body> <input type=text maxlength="8" id="test" onblur="this.value=outputmoney(this.value);" > </body> </html> 在線運行: <html> <head> <script type="text/javascript"> 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); } </script> </head> <body> <input type=text maxlength="8" id="test" onblur="this.value=outputmoney(this.value);" > </body> </html> [Ctrl+A 全選 注:如需引入外部Js需刷新才能執行] 例2,使數字1111111變成11,111,111---和使11,111,111變成1111111---整數-- 代碼如下: <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=gb2312" /> <title>js 格式化金額的代碼---www.jb51.net</title> <script language="javascript"> 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); } </script> <style type="text/css"> <!-- body,td,th,input { font-size: 12px; } --> </style></head> <body> <input name="tt" type="text" id="tt" size="80" onkeyup="tran(this)" /> <br/> <input name="tt" type="text" id="tt2" size="80" onkeyup="tran2(this)" /> </body> </html> 運行測試: <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=gb2312" /> <title>js 格式化金額的代碼---www.jb51.net</title> <script language="javascript"> 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); } </script> <style type="text/css"> <!-- body,td,th,input { font-size: 12px; } --> </style></head> <body> <input name="tt" type="text" id="tt" size="80" onkeyup="tran(this)" /> <input name="tt" type="text" id="tt2" size="80" onkeyup="tran2(this)" /> </body> </html> [Ctrl+A 全選 注:如需引入外部Js需刷新才能執行]