我有一个适用于正数的 javascript 函数,但是当输入负数时它会发出警报NaN
:
function formatMoney(number) {
number = parseFloat(number.toString().match(/^\d+\.?\d{0,2}/));
//Seperates the components of the number
var components = (Math.floor(number * 100) / 100).toString().split(".");
//Comma-fies the first part
components [0] = components [0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
//Combines the two sections
return components.join(".");
}
alert(formatMoney(-11));
这是 jsFiddle http://jsfiddle.net/longvu/wRYsU/中的示例
谢谢你的帮助