怎样把浮点数转为字符串?

来源:百度知道 编辑:UC知道 时间:2024/05/30 08:30:03
怎样把浮点数转为字符串?c++内有无现成的函数啊,如果无,怎样写这个函数啊?

不需要函数。
早期C++使用ostrstream流,如:
char buf[20];
ostrstream os(buf);
double pi = 3.14;
int a = 12;
os<<pi<<" "<<a;
cout<<buf<<endl; // show "3.14 12"
头文件
#include <strstream.h>
vc 为
#include <strstrea.h>

新的C++标准使用 ostringstream代替上面的ostrstream流
头文件
#include <sstream>

也可以使用C语言的sprintf函数,如:
char buf[20];
sprintf(buf, "%lf %d", 3.14, 12);