.
Environment: VC6 SP4, W2k SP2
Display of the most financial data is required to be formated with a thousand separator. Like: 1000000 to be formated and displayed as “1,000,000”
There are lots of ways to do this formating, but there is no default crt function to do this for you. Here I try to present 2 functions, with very good performance that can be used for this this type of formating. Here is the most simple implementation.
static char sDecimalSeparator = ‘.’; // dot by default
static char sThousantSeparator =’,’; // comma by default// call this to format a long to a string
static char* __stdcall long2str( char* szBuffer, long lValue )
{
char *p; // pointer to traverse string
char *firstdig; // pointer to first digit
char temp; // temp char
unsigned digval; // value of digit
unsigned long val;p = szBuffer;
if (lValue < 0 ) {
// negative, so output ‘-‘ and negate
*p++ = ‘-‘;
val = (unsigned long)(-(long)lValue); // make it positive!!
} else
val = lValue;firstdig = p; // save pointer to first digit
int iDecimalPos = 0;
do {
iDecimalPos ++;
if (iDecimalPos != 4)
{
digval = (unsigned) (val % 10);
val /= 10; // get next digit
*p++ = (char) (digval + ‘0’); // and store the digit
} else
{
*p++ = sThousantSeparator;
iDecimalPos = 0;
}
} while (val > 0);// We now have the digit of the number in the buffer,
// but in reverse order. Thus we reverse them now.*p– = ‘