正の数でうまく機能する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/の例を次に示します。
助けてくれてありがとう