可能有好多人,包括C语言老手都不知道如何将float数据转换为string,我就是这样,今天查了一下MSDN,才知道C提供了_gcvt函数实现这个功能,收获着实不小,为了方便自己查询,也为了那些像我这样的网友能够了解该函数的具体用法,我把MSDN的原文原封不动抄录如下:

_gcvt

Converts a floating-point value to a string, which it stores in a buffer.

char *_gcvt( double value , int digits , char * buffer );

Routine Required Header Compatibility
_gcvt <stdlib.h> Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

_gcvt returns a pointer to the string of digits. There is no error return.

Parameters

value

Value to be converted

digits

Number of significant digits stored

buffer

Storage location for result

Remarks

The _gcvt function converts a floating-point value to a character string (which includes a decimal point and a possible sign byte) and stores the string in buffer. The buffer should be large enough to accommodate the converted value plus a terminating null character, which is appended automatically. If a buffer size of digits + 1 is used, the function overwrites the end of the buffer. This is because the converted string includes a decimal point and can contain sign and exponent information. There is no provision for overflow. _gcvt attempts to produce digits digits in decimal format. If it cannot, it produces digits digits in exponential format. Trailing zeros may be suppressed in the conversion.

Example

								/* _GCVT.C: This program converts -3.1415e5 * to its string representation. */#include <stdlib.h>#include <stdio.h>void main( void ){   char buffer[50];   double source = -3.1415e5;   _gcvt( source, 7, buffer );   printf( "source: %f  buffer: '%s'\n", source, buffer );   _gcvt( source, 7, buffer );   printf( "source: %e  buffer: '%s'\n", source, buffer );}
						

Output

								source: -314150.000000  buffer: '-314150.'source: -3.141500e+005  buffer: '-314150.'
						


Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=708935


[收藏到我的网摘]   skyman_2001发表于 2006年05月05日 11:28:00



#  lanno 发表于2006-05-05 18:26:00  IP: 222.35.68.*
长见识了,俺总是这样转化的:
char str[260];
float f = -3.1415926;
float f1 = 263e-5;
sprintf(str,"%f",f);
printf("%s\n",str);
sprintf(str,"%f",f1);
printf("%s\n",str);

#  Skyman 发表于2006-05-05 21:35:00  IP: 202.202.10.*
你这样做也行
但没有这样灵活
是吧?

#  zhnde 发表于2006-05-11 20:15:00  IP: 129.69.212.*
灵活吗? 能具体解释一下吗?

#  Skyman 发表于2006-05-11 22:03:00  IP: 202.202.10.*
至少可以方便的控制有效数字的位数啊。

#  ztwaker 发表于2006-07-28 12:40:00  IP: 61.144.207.*
/*C++ impl*/
string cvt(const float fval)
{
stringstream ss;
ss << fval;
return ss.str();
}

......///

#  ztwaker 发表于2006-07-28 12:46:00  IP: 61.144.207.*
加上有效数字位数控制

string cvt(const float f, const int prec)
{
stringstream ss;
ss.precision(prec);
ss << f;
return ss.str();
}

posted on 2006-12-15 11:18 清源游民 阅读(9148) 评论(1)  编辑 收藏 引用 所属分类: C++

FeedBack:
# re: 如何将float转换为string
2011-09-06 09:56 | 明样
不知道_gcvt是C标准函数还是只是MS的扩展?
如果不是标准函数的话还是不建议使用,没有移值性就谈不上灵活……  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2007年10月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

留言簿(35)

随笔分类(78)

随笔档案(74)

文章档案(5)

搜索

  •  

最新评论

阅读排行榜

评论排行榜