12 12
发新话题
打印

什么函数可以将float转成字符串?

什么函数可以将float转成字符串?

请注明一下参数及含义.谢谢!

TOP

sprintf
复制内容到剪贴板
代码:
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

TOP

atof()函数可以啊

TOP

看错了需要ftoa()函数的详细可以查找MSDN

TOP

我也想用这个功能,不知是否有人解决。。。。

TOP

引用:
原帖由 chxchx 于 2007-4-28 10:38 发表
我也想用这个功能,不知是否有人解决。。。。
上面的都已经给出答案了.

TOP

回复 1# 的帖子

例如:
float num = 0.5;
CString m_strNum;
m_strNum.Format("%f",num);

TOP

自己写个函数 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;
}

TOP

再来几个函数

#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;
}

TOP

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};

TOP

 12 12
发新话题