嗨我在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 ? "-" : "",
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();">
但我需要在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,''));