inline long double MyPow(long double x, unsigned int y)
{
long double result = 1;
int i = 0;
while(i<y)
{
result *= x;
i++;
}
return result;
}
int ftoa(TCHAR *p,float f,int ndec=6)
{
//存储整数部分
int integ = f;
float ff = f;
int ppart = 0;
int effiBinCnt = 23; //小数部分占的有效二进制位数,初使为23
int effiDecCnt = 0;
int nndec = ndec;
TCHAR temChar[100] = {0};
int a = 1;
while (integ>a)
{
a *= 2;
effiBinCnt--;
}
int b;
b = 2<<effiBinCnt;
a = 10;
while (a<=b)
{
effiDecCnt++;
a *= 10;
}
//存储小数部分
ff = ff-integ;
wsprintf(p,_T("%d."),integ);
if (nndec>effiDecCnt)
{
ff = ff+MyPow(0.1,effiDecCnt)*0.4;
}
else
{
ff = ff+MyPow(0.1,nndec)*0.4;
}
while (nndec>0)
{
if (effiDecCnt==0)
{
ff = 0.0;
}
ff *= 10;
wsprintf(p+_tcslen(p),_T("%d"),int(ff));
ff -= (int)ff;
effiDecCnt--;
nndec--;
}
return _tcsclen(p);
}