有狠你就进来回复
有什么较好的方法??????将 double型数转化为字符串str,譬如:-6.7543 转换后,str【0】=‘-’;str[1]='6',str[2]='.',……
还有一种,感觉还行!
程序代码:
#include <iostream>
#include <sstream>
#include <limits>
using namespace std;
int main()
{
int prec = numeric_limits<double>::digits10;
cout<<prec<<endl;
double a = -123.343224553;
stringstream ss;
ss.precision(prec);
ss << a;
cout << ss.str() << " = " << ss.str().length();
return 0;
}
今天有知道几种方法,如下:
程序代码:1,利用宏转化为字符串
#include <iostream>
using namespace std;
class Double
{
private:
char* string;
double m;
public:
Double(double m1,char* str)
{
int k=strlen(str);
string=new char[k+1];
strcpy(string,str);
m=m1;
}
int String_len()
{
return strlen(string);
}
void Output()
{
int i=0;
while(string[i]!='\0')
{
cout<<string[i]<<endl;
i++;
}
}
};
#define DOUBLE(m) Double(m,#m)
int main()
{
Double s=DOUBLE(999999999.0000001);
s.Output();
cout<<s.String_len()<<endl;
}
利用sprintf函数实现,总达不到所要的结果!
程序代码:#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
double m;
cout<<"please input a double :"<<endl;
cin>>m;
char* str=new char[20];
sprintf(str,"%10.15f",m);
int i=0;
while(str[i]!='\0')
{
cout<<str[i];
i++;
}
}

[ 本帖最后由 m21wo 于 2010-10-24 18:13 编辑 ]





