分类: C/C++
2007-07-27 22:55:42
可能有好多人,包括C语言老手都不知道如何将float数据转换为string,我就是这样,今天查了一下MSDN,才知道C提供了_gcvt函数实现这个功能,收获着实不小,为了方便自己查询,也为了那些像我这样的网友能够了解该函数的具体用法,我把MSDN的原文原封不动抄录如下:
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 |
|
Win 95, Win NT |
Example
/* _GCVT.C: This program converts -3.1415e5 * to its string representation.
*/
#include
#include
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.'