单击成本字段时,如果它们为空白,然后单击$0.00 displays
。如果用户没有输入任何值,我希望字段保持空白。有人可以帮我解决这个问题吗?下面列出的是我的代码:
var numberWithCommas = function(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
};
var removeCommas = function(x) {
return x.replace(/,/g, '');
};
var removeDollarSign = function(x) {
return x.indexOf('$') === 0 ? x.substr(1) : x;
};
var toNumber = function(num) {
return isNaN(num) || num === '' || num === null ? 0.00 : num;
};
var toValue = function(str) {
return toNumber(removeDollarSign(removeCommas(str)));
};
var formatCurrency = function(val) {
return '$' + numberWithCommas(parseFloat(toValue(val)).toFixed(2));
};
jQ('#costWages, #costExpenses').blur(function(event) {
event.target.value = formatCurrency(event.target.value);
});
答案 0 :(得分:1)
您可以使用Number.prototype.toLocaleString()
,例如:
export async function getData() {
try {
await request
.get(url, (err, res) => {
console.log('1', res.body);
return res.body;
}
} catch(e) {
return e;
}
}

答案 1 :(得分:0)
您的formatCurrency
可以简化为
var formatCurrency = function(val) {
val = val.replace(/[,$]/g, ''); //remove comma and dollar sign
return isNaN(val) ? "" : '$' + numberWithCommas(parseFloat(toValue(val)).toFixed(2));
};