将数字每隔3位用逗号隔开
实现数字每隔三位用逗号隔开,便于查看金额
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>Document</title> </head> <body> <table> <tr> <td class="mT"> 950000000.54 </td> </tr> </table> <script src="jquery.min.js"></script> <script> $(function() { $(".mT").each(function() { var num = parseFloat($(this).text()).toFixed(2);//获取文本框中的数字 var beforeNum = num.toString().split(".")[0];//获取小数点之前的数字 var afterNum =num.toString().split(".")[1];//获取小数点之后的数字 function toThousands(newnum) { //每隔3位,用逗号隔开 var result = [], counter = 0; newnum = (newnum || 0).toString().split(''); for(var i = newnum.length - 1; i >= 0; i--) { counter++; result.unshift(newnum[i]); if(!(counter % 3) && i != 0) { result.unshift(','); } } return result.join(''); } var lastNum = toThousands(beforeNum);//将小数点之前的整数用逗号隔开 $(this).text(lastNum.concat(".",afterNum));//将小数部分加在处理过的数字后边 }); }) </script> </body> </html>