<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<a href="javascript:void(0);" onclick='ceshi();'>测试</a>
</body>
<script>
/**
js中的数字格式变成货币类型的格式
123456789=>1,234,567.89
**/
function ceshi(){
var number='123456789';
console.log(ChangeMoney(number));
}
function ChangeMoney(money){
money = money / 100;
return toCurrency(money.toString());
}
function toCurrency(money){
if (/[^0-9\.\-]/.test(money)){
return '0.00';
}
money = money.replace(/^([\-]?)(\d*)$/, "$1$2.");
money = (money + "00").replace(/(\d*\.\d\d)\d*/, "$1");
money = money.replace(".", ",");
var re = /(\d)(\d{3},)/;
while (re.test(money)) {
money = money.replace(re, "$1,$2");
}
money = money.replace(/,(\d\d)$/, ".$1");
return '' + money.replace(/^\./, "0.");
}
</script>
</html>