shen1987 2006-9-23 11:14
什么函数可以将float转成字符串?
请注明一下参数及含义.谢谢!
Nothing 2006-9-23 20:08
sprintf
[code]
Example
/* SPRINTF.C: This program uses sprintf to format various
* data and place them in the string named buffer.
*/
#include <stdio.h>
void main( void )
{
char buffer[200], s[] = "computer", c = 'l';
int i = 35, j;
float fp = 1.7320534f;
/* Format and print various data: */
j = sprintf( buffer, "\tString: %s\n", s );
j += sprintf( buffer + j, "\tCharacter: %c\n", c );
j += sprintf( buffer + j, "\tInteger: %d\n", i );
j += sprintf( buffer + j, "\tReal: %f\n", fp );
printf( "Output:\n%s\ncharacter count = %d\n", buffer, j );
}
Output
Output:
String: computer
Character: l
Integer: 35
Real: 1.732053
character count = 71
[/code]
lb521200200 2007-1-17 15:58
atof()函数可以啊
lb521200200 2007-1-17 15:59
看错了需要ftoa()函数的详细可以查找MSDN
风雨声中 2007-4-28 13:52
[quote]原帖由 [i]chxchx[/i] 于 2007-4-28 10:38 发表 [url=http://www.lihuasoft.net/bbs/redirect.php?goto=findpost&pid=12892&ptid=3114][img]http://www.lihuasoft.net/bbs/images/common/back.gif[/img][/url]
我也想用这个功能,不知是否有人解决。。。。 [/quote]
上面的都已经给出答案了.
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[i]!='\0')
{
if(s[i]>=65&&s[i]<=90)
{
StrLower_ch[i]=s[i]+32;
}
else
{
StrLower_ch[i]=s[i];
}
i++;
}
StrLower_ch[i]='\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[i]!='\0')
{
if(s[i]>=97 && s[i]<=122)
{
StrUpper_ch[i]=s[i]-32;
}
else
{
StrUpper_ch[i]=s[i];
}
i++;
}
StrUpper_ch[i]='\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[i]=s[i];
if (s[i]=='\0')
{
break;
}
}
StrLeft_ch[i]='\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[i]!='\0')
{
StrRight_ch[k]=s[i];
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[i]!='\0' && (k<Len||Len==0))
{
StrMid_ch[k]=s[i];
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[i];
}
for(i=0;i<strlen(s2);i++)
{
StrAdd_ch[k++]=s2[i];
}
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系列