3,881
社区成员
发帖
与我相关
我的任务
分享void FormatNumber(double dblNumber, LPTSTR pszText, int nMaxCount)
{
ASSERT(pszText != NULL);
ASSERT(nMaxCount > 0);
if (dblNumber < 1000.00f)
{
::_stprintf(pszText, _T("%.2f"), dblNumber);
return;
}
LPTSTR lpszBuffer = new TCHAR[nMaxCount];
::memset(lpszBuffer, 0, sizeof(TCHAR) * nMaxCount);
::_stprintf(lpszBuffer, _T("%.2f"), dblNumber);
int nTextLen = ::lstrlen(lpszBuffer);
int nIntLen = nTextLen - 3;
int nSection = nIntLen / 3;
nSection += (nIntLen % 3) == 0 ? -1 : 0;
int nCounter = 0;
int j = nTextLen;
int nNewLen = nTextLen + nSection;
if (nMaxCount < nNewLen)
return;
for (int i = nTextLen - 1; i >= 0; i--)
{
nCounter++;
if ((nCounter % 3) == 0 && lpszBuffer[i] != '.')
{
pszText[i + nSection] = lpszBuffer[--j];
if ((i + nSection - 1) < 0)
break;
pszText[i + --nSection] = ',';
nCounter = 0;
}
else
{
pszText[i + nSection] = lpszBuffer[--j];
}
}
pszText[nNewLen] = '\0';
delete[] lpszBuffer;
}char * dcomma(double d, int n)
{
int i, j,
flag = 0; /* 整数部分 */
char *p, t[50];
static char s[50];
if( n > 8 ) n = 8;
else if( n < 0 ) n = 0;
if( n == 0 ) flag = 1;
s[sizeof(s) - 1] = 0;
sprintf(t, "%.*lf", n, d);
p = t + strlen(t) - 1;
for( i = sizeof(s) - 1, j = 0; p - t >= 0; p-- )
{
if( flag == 1 ) j++;
else if( *p == '.' ) flag = 1;
s[i--] = *p;
if( j == 3 && p - t && *(p-1) != '-' )
{
j = 0;
s[i--] = ',';
}
}
return(s + i + 1);
}