Jump to content

Code Issue


ainoy31

Recommended Posts

Hello-

 

I have a script that formats a number with commas such as 888888888 will be 888,888,888.  Here is the code:

 

function formatNumber(amount)
{
var delimiter = ",";
var a = amount.split('.',2)
var d = a[1];
var i = parseInt(a[0]);
if(isNaN(i)) 
{ 
	return ''; 
}
var minus = '';
if(i < 0) 
{ 
	minus = '-'; 
}
i = Math.abs(i);
var n = new String(i);
var a = [];
while(n.length > 3)
{
	var nn = n.substr(n.length-3);
	a.unshift(nn);
	n = n.substr(0,n.length-3);
}
if(n.length > 0) 
{ 
	a.unshift(n); 
}
n = a.join(delimiter);
if(d.length < 1) 
{ 
	amount = n; 
}
else 
{ 
	amount = n + '.' + d; 
}
amount = minus + amount;
return amount;
}

 

var num = formatNumber(amount);

 

I get the error message that amount.split is not a function.  Much appreciation.  AM

Link to comment
https://forums.phpfreaks.com/topic/82684-code-issue/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.