当前位置: 代码迷 >> JavaScript >> JS 将整数转化作货币格式的函数
  详细解决方案

JS 将整数转化作货币格式的函数

热度:403   发布时间:2012-11-23 00:03:43.0
JS 将整数转化为货币格式的函数
//将数字转为货币格式
function toMoeny(price, chars)
{
	chars = chars ? chars.toString() : '¥';
	if(price > 0)
	{
		var priceString = price.toString();
		var priceInt = parseInt(price);
		var len = priceInt.toString().length;
		var num = len / 3;
		var remainder = len % 3;
		var priceStr = '';
		for(var i = 1; i <= len; i++)
		{
			priceStr += priceString.charAt(i-1);
			if(i == (remainder) && len > remainder) priceStr += ',';
			if((i - remainder) % 3 == 0 && i < len && i > remainder) priceStr += ',';
		}
		if(priceString.indexOf('.') < 0)
		{
			priceStr = priceStr + '.00';
		} else {
			priceStr += priceString.substr(priceString.indexOf('.'));
			if(priceString.length - priceString.indexOf('.') - 1  < 2)
			{
				priceStr = priceStr + '0';
			}
		}
		return chars + priceStr;
	}
	else{
		return chars + price;	
	}
}
  相关解决方案