Javascript 使用php具有逗号功能的尾随零
嗨,我用javascript编写的代码工作得很好,但是我想用php语言实现相同的功能。我在互联网上找到了不同的源代码,但没有成功 Javascript代码:Javascript 使用php具有逗号功能的尾随零,javascript,php,jquery,Javascript,Php,Jquery,嗨,我用javascript编写的代码工作得很好,但是我想用php语言实现相同的功能。我在互联网上找到了不同的源代码,但没有成功 Javascript代码: Number.prototype.formatMoney = function(c, d, t){ var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d ? "." : d, t = t ? "," : t, s = n < 0 ? "-
Number.prototype.formatMoney = function(c, d, t){
var n = this,
c = isNaN(c = Math.abs(c)) ? 2 : c,
d = d ? "." : d,
t = t ? "," : t,
s = n < 0 ? "-" : "",
i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "",
j = (j = i.length) > 3 ? j % 3 : 0;
return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};
function trailingZerosWithComma() {
var costAmount = $("#costAmount").val();
if(costAmount != "") {
var costAmountReplace = parseFloat(costAmount.replace(/\,/g,''));
$("#costAmount").val((costAmountReplace).formatMoney(2, '.', ','));
}
}
<input type="text" name="costAmount" id="costAmount" onblur="trailingZerosWithComma();">
Number.prototype.formatMoney=函数(c、d、t){
var n=这个,
c=isNaN(c=Math.abs(c))?2:c,
d=d?“:d,
t=t?,“:t,
s=n<0?-“:”,
i=parseInt(n=Math.abs(+n | | 0).toFixed(c))+“”,
j=(j=i.length)>3?j%3:0;
返回s+(j?i.substr(0,j)+t:)+i.substr(j).替换(/(\d{3})(?=\d)/g,“$1”+t)+(c?d+Math.abs(n-i).toFixed(c).slice(2):”);
};
函数trailingZerosWithComma(){
var costAmount=$(“#costAmount”).val();
如果(成本金额!=“”){
var costAmountReplace=parseFloat(costAmount.replace(/\,/g',);
$(“#costAmount”).val((costAmountReplace).formatMoney(2,,,,);
}
}
但是我需要php中的这个
Number.prototype.formatMoney = function(c, d, t){
var n = this,
c = isNaN(c = Math.abs(c)) ? 2 : c,
d = d ? "." : d,
t = t ? "," : t,
s = n < 0 ? "-" : "",
i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "",
j = (j = i.length) > 3 ? j % 3 : 0;
return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};
var costAmountReplace = parseFloat(costAmount.replace(/\,/g,''));
Number.prototype.formatMoney=函数(c、d、t){
var n=这个,
c=isNaN(c=Math.abs(c))?2:c,
d=d?“:d,
t=t?,“:t,
s=n<0?-“:”,
i=parseInt(n=Math.abs(+n | | 0).toFixed(c))+“”,
j=(j=i.length)>3?j%3:0;
返回s+(j?i.substr(0,j)+t:)+i.substr(j).替换(/(\d{3})(?=\d)/g,“$1”+t)+(c?d+Math.abs(n-i).toFixed(c).slice(2):”);
};
var costAmountReplace=parseFloat(costAmount.replace(/\,/g',);
好的,祝你好运,这不是免费的编码服务。谢谢你的建议……你为什么不使用money\u format()
函数。不能使用它。我在本地使用windows操作系统和wamp服务器。任何人都会帮我。。。