Game Development Community

dev|Pro Game Development Curriculum

Comma formatted text

by Peter Simard · 04/08/2008 (2:16 pm) · 1 comments

This code will parse an integer value and return it with commas for display. Add this code inside any script file that executes.

function addCommas(%value)
{
     %groups = mCeil(strlen(%value) / 3);
     for(%x=1; %x<%groups+1; %x++)
     {
          if(%x > 1)
               %ret = "," @ %ret;
               
          %start = (strlen(%value) - (%x * 3));
          %len = 3;
          if(%start < 0)
          {
               %len = %start + 3;
               %start = 0;
          }
          %words = getSubStr(%value, %start, %len);
          %ret = %words @ %ret;
     }
     
     return %ret;
}

Example:
echo(addCommas("10000"));
==>10,000

#1
08/08/2008 (5:21 am)
Nice catch...

hehe