jsun9
Dec 19th, 2003, 12:23 PM
This function is called like this:
tmp = currency("$", "12345678901234", ".")
or
tmp = currency("", "0001234 ", "")
or something similiar. there are leading zeros and trailing spaces (or even spaces before the zeros).
This code removes all leading zeros, all spaces (anywhere), adds the appropriate dollar sign, decimal point and commas then returns the fully formated string.
It seems like a lot of code to do something so simple.. are there any better or faster ways of doing this?
USERDLL_API char * currency(char *string,char *stringTwo, char *stringThree, char *stringFour)
{
static char tmp[STMAX];
static char symbol[1];
static char decimal[1];
register int source;
int x,y;
int p1,p2,p3;
strncpy(symbol, string, 1);
strncpy(tmp, stringTwo, STMAX -1);
strncpy(decimal, stringThree, 1);
///////SPACE REMOVER///////////////////////////
for(x=0; x<=strlen(tmp); x++)
{
if(tmp[x] == ' ')
{
for(y=x; y<strlen(tmp); y++)
{
tmp[y] = tmp[y+1];
}
strncpy(tmp, tmp, (strlen(tmp)-1));
}
}
///////////////////////////////////////////////
///////LEADING ZERO REMOVER////////////////////
for(x=0; x<=strlen(tmp); x++)
{
if(tmp[0] == '0')
{
for(y=0; y<strlen(tmp); y++)
{
tmp[y] = tmp[y+1];
}
strncpy(tmp, tmp, (strlen(tmp)-1));
}
}
///////////////////////////////////////////////
p1=strlen(tmp);
p2=p1-1;
p3=0;
if(*decimal == '.')
{
p1=p1-2;
p2=p1-1;
p3=p3+1;
for ( source = 20; source != p2; source--)
tmp[source +1] = tmp[source];
tmp[p1] = *decimal;
}
else
{
p3=p3-2;
}
if(strlen(tmp) >= (6+p3))
{
p1=p1-3;
p2=p1-1;
p3=p3+1;
for ( source = 20; source != p2; source--)
tmp[source +1] = tmp[source];
tmp[p1] = ',';
}
if(strlen(tmp) >= (9+p3))
{
p1=p1-3;
p2=p1-1;
p3=p3+1;
for ( source = 20; source != p2; source--)
tmp[source +1] = tmp[source];
tmp[p1] = ',';
}
if(strlen(tmp) >= (12+p3))
{
p1=p1-3;
p2=p1-1;
p3=p3+1;
for ( source = 20; source != p2; source--)
tmp[source +1] = tmp[source];
tmp[p1] = ',';
}
if(strlen(tmp) >= (15+p3))
{
p1=p1-3;
p2=p1-1;
for ( source = 20; source != p2; source--)
tmp[source +1] = tmp[source];
tmp[p1] = ',';
}
if(*symbol == '$')
{
for ( source = 20; source !=-1; source--)
tmp[source +1] = tmp[source];
tmp[0] = *symbol;
}
return tmp;
}
tmp = currency("$", "12345678901234", ".")
or
tmp = currency("", "0001234 ", "")
or something similiar. there are leading zeros and trailing spaces (or even spaces before the zeros).
This code removes all leading zeros, all spaces (anywhere), adds the appropriate dollar sign, decimal point and commas then returns the fully formated string.
It seems like a lot of code to do something so simple.. are there any better or faster ways of doing this?
USERDLL_API char * currency(char *string,char *stringTwo, char *stringThree, char *stringFour)
{
static char tmp[STMAX];
static char symbol[1];
static char decimal[1];
register int source;
int x,y;
int p1,p2,p3;
strncpy(symbol, string, 1);
strncpy(tmp, stringTwo, STMAX -1);
strncpy(decimal, stringThree, 1);
///////SPACE REMOVER///////////////////////////
for(x=0; x<=strlen(tmp); x++)
{
if(tmp[x] == ' ')
{
for(y=x; y<strlen(tmp); y++)
{
tmp[y] = tmp[y+1];
}
strncpy(tmp, tmp, (strlen(tmp)-1));
}
}
///////////////////////////////////////////////
///////LEADING ZERO REMOVER////////////////////
for(x=0; x<=strlen(tmp); x++)
{
if(tmp[0] == '0')
{
for(y=0; y<strlen(tmp); y++)
{
tmp[y] = tmp[y+1];
}
strncpy(tmp, tmp, (strlen(tmp)-1));
}
}
///////////////////////////////////////////////
p1=strlen(tmp);
p2=p1-1;
p3=0;
if(*decimal == '.')
{
p1=p1-2;
p2=p1-1;
p3=p3+1;
for ( source = 20; source != p2; source--)
tmp[source +1] = tmp[source];
tmp[p1] = *decimal;
}
else
{
p3=p3-2;
}
if(strlen(tmp) >= (6+p3))
{
p1=p1-3;
p2=p1-1;
p3=p3+1;
for ( source = 20; source != p2; source--)
tmp[source +1] = tmp[source];
tmp[p1] = ',';
}
if(strlen(tmp) >= (9+p3))
{
p1=p1-3;
p2=p1-1;
p3=p3+1;
for ( source = 20; source != p2; source--)
tmp[source +1] = tmp[source];
tmp[p1] = ',';
}
if(strlen(tmp) >= (12+p3))
{
p1=p1-3;
p2=p1-1;
p3=p3+1;
for ( source = 20; source != p2; source--)
tmp[source +1] = tmp[source];
tmp[p1] = ',';
}
if(strlen(tmp) >= (15+p3))
{
p1=p1-3;
p2=p1-1;
for ( source = 20; source != p2; source--)
tmp[source +1] = tmp[source];
tmp[p1] = ',';
}
if(*symbol == '$')
{
for ( source = 20; source !=-1; source--)
tmp[source +1] = tmp[source];
tmp[0] = *symbol;
}
return tmp;
}