PDA

Click to See Complete Forum and Search --> : [C++] - Trim / Currency Function


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;

}

CornedBee
Dec 20th, 2003, 02:46 PM
Here's comma adding:
template <typename _C, typename _Traits, typename _Alloc>
inline std::basic_string<_C, _Traits, _Alloc> &
add_commas(
const std::basic_string<_C, _Traits, _Alloc> & in,
_C comma,
std::basic_string<_C, _Traits, _Alloc> & out)
{
out.clear();
out.reserve(in.length()+(in.length()/3));
for(std::basic_string<_C, _Traits, _Alloc>::const_reverse_iterator i=in.rbegin();
i!=in.rend(); ++i)
{
std::iterator_traits<std::basic_string<_C, _Traits, _Alloc>::const_iterator>
::difference_type diff = i-in.rbegin();
if(diff>0 && diff%3==0)
out.push_back(comma);
out.push_back(*i);
}
std::reverse(out.begin(), out.end());
return out;
}
Not the most efficient, it's old code. I updated it a bit.

Here's a trim function.
template<typename _C>
inline std::basic_string<_C> & trim(std::basic_string<_C> &s, const std::basic_string<_C> &t)
{
std::basic_string<_C>::size_type st1, st2;
st1 = s.find_first_not_of(t);
if(st1 == std::basic_string<_C>::npos)
{
s.clear();
return;
}
st2 = s.find_last_not_of(t)+1;
s = s.substr(st1, st2-st1);
return s;
}

Plug'em together and you're good to go.
template<typename _C>
inline std::basic_string<_C> & currency(std::basic_string<_C> &str, const std::basic_string<_C> &trimChars, _C comma)
{
return add_commas(std::basic_string(trim(str, trimChars)), comma, str);
}

jsun9
Dec 22nd, 2003, 02:49 PM
so with your code you can pass a string that is infinite in size correct? I guess I could do that with mine, but due to the "source" variable it could take quite a while..

your code inspired me to make this:


while(strlen(tmp) >= (p4+p3))
{
p1=p1-3;
p2=p1-1;
p3=p3+1;
p4=p4+3;
for ( source = 75; source != p2; source--)
tmp[source +1] = tmp[source];
tmp[p1] = ',';
}


it's the same thing as before except now it's in a while loop and can hold up to 75 characters including commas!