标题:
什么函数可以将float转成字符串?
[打印本页]
作者:
shen1987
时间:
2006-9-23 11:14
标题:
什么函数可以将float转成字符串?
请注明一下参数及含义.谢谢!
作者:
Nothing
时间:
2006-9-23 20:08
sprintf<
作者:
lb521200200
时间:
2007-1-17 15:58
atof()函数可以啊
作者:
lb521200200
时间:
2007-1-17 15:59
看错了需要ftoa()函数的详细可以查找MSDN
作者:
chxchx
时间:
2007-4-28 10:38
我也想用这个功能,不知是否有人解决。。。。
作者:
风雨声中
时间:
2007-4-28 13:52
引用:
原帖由
chxchx
于 2007-4-28 10:38 发表
我也想用这个功能,不知是否有人解决。。。。
上面的都已经给出答案了.
作者:
huatao
时间:
2007-12-29 20:17
标题:
回复 1# 的帖子
例如:
float num = 0.5;
CString m_strNum;
m_strNum.Format("%f",num);
作者:
ldy216
时间:
2011-1-14 11:34
标题:
自己写个函数 FloatToStr
char *FloatToStr_ch;
char *FloatToStr(float x)
{
int j,k=0,Sign=0,Exp=0;
unsigned long *p=(unsigned long *)&x,Base=0;
free(FloatToStr_ch);
FloatToStr_ch=(char *)malloc(15);
if((*p)==0||(*p)==0x80000000)//0处理
{
FloatToStr_ch[0]='0';
FloatToStr_ch[1]='\0';
return FloatToStr_ch;
}
else if(((*p)>>23&0xff)==0xff)//无穷大(小)处理
{
if((*p)&0x80000000)
{
strcpy(FloatToStr_ch,"-1.#");
}
else
{
strcpy(FloatToStr_ch,"1.#");
}
if( (*p)&0x7fffff)
{
strcat(FloatToStr_ch,"QNAN");
}
else
{
strcat(FloatToStr_ch,"INFO");
}
return FloatToStr_ch;
}
else
{
if((*p)&0x80000000)
{
x=-x;
FloatToStr_ch[k++]='-';
Sign=1;
}
//下面直到if行可有可无,用于加速运算
Exp=(((*p)>>23)&0xff)-0x7f;
Exp=(int)(Exp*LOG2);
if(Exp>0 && Exp<sizeof(EXP10)/sizeof(float))
{
x/=(float)EXP10[Exp];
}
else if (-Exp>0 && -Exp<sizeof(EXP10)/sizeof(float))
{
x*=(float)EXP10[-Exp];
}
else
{
Exp=0;
}
if(x==0)
{
FloatToStr_ch[0]='0';
FloatToStr_ch[1]='\0';
return FloatToStr_ch;
}
while(x>=10)
{
x/=10;
Exp++;
}
while(x<1)
{
x*=10;
Exp--;
}
}
Base=((*p)&0x7fffff)|0x800000;
j=(((*p)>>23)&0xff)-0x7f;
if(j>0)
{
Base=(Base<<j);
}
Base=Base<<1;
while (Base!=0 && k<Sign+9)
{
if(k==Sign+1)
{
FloatToStr_ch[k++]='.';
}
FloatToStr_ch[k++]=(char)((Base>>24)+'0');
Base&=0xffffff;
Base=Base*10;
}
while(FloatToStr_ch[k-1]=='0')//去掉0
{
k--;
}
if(Exp!=0)
{
FloatToStr_ch[k++]='E';
if(Exp<0)
{
Exp=-Exp;
FloatToStr_ch[k++]='-';
}
if(Exp>10)
{
FloatToStr_ch[k++]=(Exp/10)+'0';
FloatToStr_ch[k++]=(Exp%10)+'0';
}
else
{
FloatToStr_ch[k++]=Exp+'0';
}
}
FloatToStr_ch[k]='\0';
//sprintf(FloatToStr_ch,"%f",x);
return FloatToStr_ch;
}
作者:
ldy216
时间:
2011-1-14 11:37
标题:
再来几个函数
#define LOG2 0.301
const double EXP10[]={1,1e1,1e2,1e3,1e4,1e5,1e6,1e7,1e8,1e9,
1e10,1e11,1e12,1e13,1e14,1e15,1e16,1e17,1e18,1e19,
1e20,1e21,1e22,1e23,1e24,1e25,1e26,1e27,1e28,1e29,
1e30,1e31,1e32,1e33,1e34,1e35,1e36,1e37,1e38,1e39};
//---------------------------------------------------------------
char *StrLower_ch;
char *StrLower(char *s)
{
int i=0;
free(StrLower_ch);
StrLower_ch=(char *)malloc(strlen(s)+1);
while (s
!='\0')
{
if(s
>=65&&s
<=90)
{
StrLower_ch
=s
+32;
}
else
{
StrLower_ch
=s
;
}
i++;
}
StrLower_ch
='\0';
return StrLower_ch;
}
//---------------------------------------------------------------
char *StrUpper_ch;
char *StrUpper(char *s)
{
int i=0;
free(StrUpper_ch);
StrUpper_ch=(char *)malloc(strlen(s)+1);
while (s
!='\0')
{
if(s
>=97 && s
<=122)
{
StrUpper_ch
=s
-32;
}
else
{
StrUpper_ch
=s
;
}
i++;
}
StrUpper_ch
='\0';
return StrUpper_ch;
}
//---------------------------------------------------------------
char * StrLeft_ch;
char * StrLeft(char *s,int Len)
{
int i;
free(StrLeft_ch);
StrLeft_ch=(char *)malloc(Len+1);
for( i=0;i<Len;i++)
{
StrLeft_ch
=s
;
if (s
=='\0')
{
break;
}
}
StrLeft_ch
='\0';
return StrLeft_ch;
}
//---------------------------------------------------------------
char * StrRight_ch;
char * StrRight(char *s,unsigned int Len)
{
int i=0,k=0;
free(StrRight_ch);
StrRight_ch=(char *)malloc(Len+1);
if(strlen(s)>=Len)
{
i=strlen(s)-Len;
}
while(s
!='\0')
{
StrRight_ch[k]=s
;
i++;
k++;
}
StrRight_ch[k]='\0';
return StrRight_ch;
}
//---------------------------------------------------------------
char * StrMid_ch;
char * StrMid(char *s,unsigned int Pos,unsigned int Len)
{
unsigned int i=0,k=0;
free(StrMid_ch);
StrMid_ch=(char *)malloc(Len+1);
if(strlen(s)<=Pos)
{
StrMid_ch[0]='\0';
}
else
{
i=Pos;
while(s
!='\0' && (k<Len||Len==0))
{
StrMid_ch[k]=s
;
i++;
k++;
}
StrMid_ch[k]='\0';
}
return StrMid_ch;
}
//---------------------------------------------------------------
char *StrAdd_ch;
char *StrAdd(char *s1,char *s2)
{
unsigned int i,k=0;
free(StrAdd_ch);
StrAdd_ch=(char *)malloc(strlen(s1)+strlen(s2));
for (i=0;i<strlen(s1);i++)
{
StrAdd_ch[k++]=s1
;
}
for(i=0;i<strlen(s2);i++)
{
StrAdd_ch[k++]=s2
;
}
StrAdd_ch[k]='\0';
return StrAdd_ch;
}
//---------------------------------------------------------------
char *IntToStr_ch;
char *IntToStr(int x)
{
char ch;
unsigned int i=0,Sign=0,k=0;
free(IntToStr_ch);
IntToStr_ch=(char *)malloc(6);
if (x<0)
{
x=-x;
IntToStr_ch[k++]='-';
Sign=1;
}
while(x>0)
{
IntToStr_ch[k++]=(x%10)+'0';
x=x/10;
}
if(k==0)
{
IntToStr_ch[k++]='0';
}
for(i=0;i<(k-Sign)/2;i++)
{
ch=IntToStr_ch[Sign+i];
IntToStr_ch[Sign+i]=IntToStr_ch[k-1-i];
IntToStr_ch[k-1-i]=ch;
}
IntToStr_ch[k]='\0';
return IntToStr_ch;
}
//---------------------------------------------------------------
char *LongToStr_ch;
char *LongToStr(long x)
{
char ch;
unsigned int i=0,Sign=0,k=0;
free(LongToStr_ch);
LongToStr_ch=(char *)malloc(11);
if (x<0)
{
x=-x;
LongToStr_ch[k++]='-';
Sign=1;
}
while(x>0)
{
LongToStr_ch[k++]=(x%10)+'0';
x=x/10;
}
if(k==0)
{
LongToStr_ch[k++]='0';
}
for(i=0;i<(k-Sign)/2;i++)
{
ch=LongToStr_ch[Sign+i];
LongToStr_ch[Sign+i]=LongToStr_ch[k-1-i];
LongToStr_ch[k-1-i]=ch;
}
LongToStr_ch[k]='\0';
return LongToStr_ch;
}
作者:
ldy216
时间:
2011-1-14 11:39
标题:
FloatToStr 中的常数
#define LOG2 0.301
const double EXP10[]={1,1e1,1e2,1e3,1e4,1e5,1e6,1e7,1e8,1e9,
1e10,1e11,1e12,1e13,1e14,1e15,1e16,1e17,1e18,1e19,
1e20,1e21,1e22,1e23,1e24,1e25,1e26,1e27,1e28,1e29,
1e30,1e31,1e32,1e33,1e34,1e35,1e36,1e37,1e38,1e39};
作者:
ldy216
时间:
2011-1-14 11:43
标题:
引用的举例,非常方便
char *s;
long k;
s=LongToStr(StrToLong("123")+StrToLong("0x123.78"));
我还有各种高精度运算,汇编级代码的数学函数等等
自定义无限运算等
QQ:184324486
作者:
crack
时间:
2013-4-10 20:02
IntelliWave Intellium Z40 Intellium Z100 interferometer
Intellium Z30
OPTICAD系列
OPTICAD EquationCAD
SIEMENS,HONEYWELL,JOHNSON CONTROL
Primavera Project
Primavera Expedition 10
Primavera Project Planner for Enterprise
光通讯: ██需要购买软件请【联系QQ: 630971588电话:13726719966】
Optiwave系列
欢迎光临 编程开发论坛 (http://bbs.lihuasoft.net/)
Powered by Discuz! 6.0.0