javascript把数字格式化转换成货币型


原文:http://www.juyimeng.com/javascript-number-format-currency.html

 

 

function outputMoney(number) {

	number=number.replace(/\,/g,"");

	if (isNaN(number)||number=="") return "";

		number = Math.round(number*100) /100;

	if(number<0)

		return '-'+outputDollars(Math.floor(Math.abs(number)-0) + '') + outputCents(Math.abs(number) - 0);

	else

		return outputDollars(Math.floor(number-0) + '') + outputCents(number - 0);

}

function outputDollars(number)

{

	if (number.length<= 3)

		return (number == '' ? '0' : number);

	else{

		var mod = number.length%3;

		var output = (mod == 0 ? '' : (number.substring(0,mod)));

		for (i=0 ; i< Math.floor(number.length/3) ; i++)

		{

		  if ((mod ==0) && (i ==0))

		  output+= number.substring(mod+3*i,mod+3*i+3);

		  else

		  output+= ',' + number.substring(mod+3*i,mod+3*i+3);

		}

		return (output);

	}

}

function outputCents(amount){

	amount = Math.round( ( (amount) - Math.floor(amount) ) *100);

	return (amount<10 ? '.0' + amount : '.' + amount);

}

 

你可能感兴趣的:(JavaScript)