# Numbers and Commas

By Zalai Botond
on October 14, 2003

Version: 2.0

Type: Function

Category: Algorithms

Description: Syntax:

numcomma(“\$value”);

This function takes a numeric value, and adds commas where applicable. If you gave it the value of “2930829523”, then it would return “2,930,829,523”.

```function numcomma (\$value)
{
/*
coded diz coz noone had one that actually worked for me

[email protected]
*/

if(strpos(\$value,"."))
{
\$decimalval = substr(\$value,strpos(\$value,".")+1);
\$value = substr(\$value,0,strpos(\$value,"."));
}

\$length = strlen(\$value);

for(\$i=3;\$i<(\$length);\$i=\$i+3)
{
\$k = \$i*(-1);

\$chunks[count(\$chunks)] = substr(\$value,\$k,3);

}

\$inarray = count(\$chunks)*3;
\$leftout = \$length-\$inarray;
\$leftout = substr(\$value,0,\$leftout);

\$finaltext = \$leftout;

rsort(\$chunks);

for(\$i=0;\$i<count(\$chunks);\$i++)
{

\$finaltext .= "," .\$chunks[\$i];
}

if(strlen(\$decimalval)>0) \$finaltext .= "." .\$decimalval;

return \$finaltext;
}```
﻿