# jquery 千分位如何加減乘除？

jquery

``````function formatDollar(num) {
var p = num.toFixed(2).split(".");
return "\$" + p[0].split("").reverse().reduce(function(acc, num, i, orig) {
return num == "-" ? acc : num + (i && !(i % 3) ? "," : "") + acc;
}, "");
}``````

``\$391,610``

``\$391,61090``

4 个回答
• 106

``````  function formatDollar(n,b){
var aa = function(m){return String(m).split('').reverse().join('');};
var bb = parseInt(n.replace(/[^0-9]/ig,""))+parseInt(b);
return "\$"+aa(aa(bb).replace(/(\d{3})/g,'\$1,').replace(/\,\$/,''));
}

var val= "\$910,610";
var str = formatDollar(val,90);

console.log(str); //得到：\$910,700``````

``````  function formatDollar(n,b){
var aa = String(parseInt(n.replace(/[^\d|.]/g,''))+parseInt(b));
return "\$"+aa.replace(/(\d)(?=(\d{3})+\$)/ig,'\$1,');
}

var val= "\$910,610";
var str = formatDollar(val,90);

console.log(str); //得到：\$910,700``````

``````  function formatDollar(n,b){
var aa = parseFloat(n.replace(/[^\d|.]/g,''))+parseFloat(b);
var bb = String(aa).split(".");
var cc = bb[0].replace(/(\d)(?=(\d{3})+\$)/ig,'\$1,');
var dd = bb[1]?cc+"."+bb[1]:cc;

return "\$"+dd;
}

var val= "\$910,610.15";
var str = formatDollar(val,90.16);

console.log(str); //得到：\$910,700.31``````

``````  function formatDollar(n,b){
var aa = parseFloat(n.replace(/[^\d|.]/g,''))+parseFloat(b);
return "\$"+aa.toLocaleString();
}

var val= "\$910,610.15";
var str = formatDollar(val,90.16);

console.log(str); //得到：\$910,700.31``````

`\$391,610`这是你需要展示的，存为一个变量A。
`391610`这是你原始的数字，存为另一个变量B。就用这个来做运算操作，然后再通过千分位的方法，转成变量A。