js 银行卡四位一隔,钱三位用逗号隔开并强制保留两位小数
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <script> /** * 格式化money,(123456=>123,456.00) * num:string or number */ formatMoney = (num) => { var end = parseFloat((num/100).toFixed(2)).toLocaleString(); var len = end.substr(end.indexOf('.')+1,end.length).length; if(end.indexOf('.')==-1){ end+='.00'; }else if(len<2){ end+='0'; } return end; } console.log(formatMoney(12340)) /** * 格式化银行卡 或者订单号(四位一隔开)111111111111=>1111 1111 1111 * bank(String) */ formatBank = (bank) => { return bank.replace(/[\s]/g, 'string').replace(/(\d{4})(?=\d)/g, "$1 "); } console.log(formatBank('1232398989898')); </script> </body> </html>