急想知道c++中怎么把小数放到字符串里

bingfeng100 2008-09-14 09:01:48
请问怎么把一个位数很多的小数放进 char* a=new char;里
...全文
291 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
luoye 2008-11-10
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 Chiyer 的回复:]
C/C++ code
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
double d = 0.0;
cin>>d;
stringstream ss;
ss<<setprecision(32)<<d;
string str;
ss>>str;
cout<<str<<endl;

return 0;
}

---

123.12345678912345
123.12345678912345
请按任意键继续. . .
[/Quote]
这个方法很好
可是我想在我用MFC做过的一个程序里用它该怎么办呢? VC6.0的MFC 默认情况下是不用namespace 的
难道要把所有的“.h”都去掉?
whuyotc 2008-09-15
  • 打赏
  • 举报
回复
setprecision是起什么作用的啊?
是设置精度么??
为什么我使用setprecision(32)的输出精度不是32呢?在10位的时候就已经开始四舍五入了,然后,后面全是0
如果setprecision(100)是不是我就可以设置为100位精度呢?

[Quote=引用 12 楼 Chiyer 的回复:]
C/C++ code
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
double d = 0.0;
cin>>d;
stringstream ss;
ss<<setprecision(32)<<d;
string str;
ss>>str;
cout<<str<<endl;

return 0;
}

---

123.12345678912345
123.12345678912345
请按任意键继续. . .
[/Quote]
clover8 2008-09-15
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 clover8 的回复:]
引用 12 楼 Chiyer 的回复:
C/C++ code
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
double d = 0.0;
cin>>d;
stringstream ss;
ss < <setprecision(32) < <d;
string str;
ss>>str;
cout < <str < <endl;

return 0;
}

---

123.12345678912345
123.12345678912345
请按…
[/Quote]

哦,刚才弄错了,星羽这个是可以的,不过,有这样的情况:。。。

123.123456789444
123.12345678944401
请按任意键继续. . .
clover8 2008-09-15
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 Chiyer 的回复:]
C/C++ code
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
double d = 0.0;
cin>>d;
stringstream ss;
ss<<setprecision(32)<<d;
string str;
ss>>str;
cout<<str<<endl;

return 0;
}

---

123.12345678912345
123.12345678912345
请按任意键继续. . .
[/Quote]
星羽的编译器是vs的吧?用dev c++跑你这个是有问题的。
oo_v_oo 2008-09-15
  • 打赏
  • 举报
回复
double最多也就18位精度
LZ所说的位数很多的小数使用什么保存的,
难道是火星浮点
fallening 2008-09-15
  • 打赏
  • 举报
回复
use boost::lexical_cast
fengyizi 2008-09-15
  • 打赏
  • 举报
回复
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
double a=123.45678;
string a;
stringstream ss;
ss<<a;
cout <<ss.str()<<endl;
return 0;
}
skineffect 2008-09-15
  • 打赏
  • 举报
回复
#include<iostream>
#include<sstream>
#include<iomanip>
using namespace std;
int main(){
double pi=3.1415926;
string a;
stringstream ss;
ss<<setprecision(12)<<pi;
a=ss.str();
cout<<a<<endl;
system("pause");
}
简单地输出pi。
另外setprecision不宜设得太大,否则后面的数位由于近似的原因在若干位0之后会有一个1。
星羽 2008-09-15
  • 打赏
  • 举报
回复

#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
double d = 0.0;
cin>>d;
stringstream ss;
ss<<setprecision(32)<<d;
string str;
ss>>str;
cout<<str<<endl;

return 0;
}

---

123.12345678912345
123.12345678912345
请按任意键继续. . .





bingfeng100 2008-09-14
  • 打赏
  • 举报
回复
你们的好像数字的位数多了以后就有偏差了耶
yangkunhenry 2008-09-14
  • 打赏
  • 举报
回复
哎,差距啊……sprintf以前都没怎么见过,学习了……
yangkunhenry 2008-09-14
  • 打赏
  • 举报
回复
汗……我想多了
yangkunhenry 2008-09-14
  • 打赏
  • 举报
回复

#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace std;
void change(char *a,double n,int x)
{
long m;
int len;
int i,cnt=0;
m=n*pow(10,x);
_itoa(m,a,10);
len=strlen(a);
for(i=len-1;cnt<5;i--)
{
a[i+1]=a[i];
cnt++;
}
a[i+1]='.';
a[len+1]='\0';
}
int main()
{
double n=234.87739;
char *a=new char[10];
change(a,n,5);
cout<<a<<endl;
delete []a;
return 0;
}
jay的Fans 2008-09-14
  • 打赏
  • 举报
回复
:-)借鉴了4楼大哥昨天的一个帖子。
HelloDan 2008-09-14
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 tjianli 的回复:]
#include <iostream>

using namespace std;

const int SIZE = 100;

int main()
{
char *a = new char[SIZE];

double num;
num = 11.123123;
sprintf(a , "%g" , num);
cout < < a < < endl;

delete []a;
return 0;
}

[/Quote]

这个是个不错的方法,在C++里面也有相应的东西,如下:

// using stringstream constructors.
#include <iostream>
#include <sstream>
#include<string>
using namespace std;

int main ()
{
double val=12.3456789;

stringstream oss;
string mystr;

oss << val;
mystr=oss.str();

cout << mystr.c_str()<<endl;;

return 0;
}

wangdeqie 2008-09-14
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;

void main()
{
double n;
cin>>n;
char* a=new char[10];
sprintf(a,"%g",n);
cout<<a<<endl;
}
wangdeqie 2008-09-14
  • 打赏
  • 举报
回复

//可以这么写
#include <windows.h>
#include <iostream>
using namespace std;

void main()
{
int n;
cin>>n;
char* a=new char[10];
itoa(n,a,10);
cout<<a<<endl;
}
jay的Fans 2008-09-14
  • 打赏
  • 举报
回复
#include <iostream>

using namespace std;

const int SIZE = 100;

int main()
{
char *a = new char[SIZE];

double num;
num = 11.123123;
sprintf(a , "%g" , num);
cout << a << endl;

delete []a;
return 0;
}
yangkunhenry 2008-09-14
  • 打赏
  • 举报
回复
先把小数乘以10的N次方变为整数,
把整数放进字符串里后,
再在相应的位置插入小数点。
不知楼主是否满意
lzr4304061988012 2008-09-14
  • 打赏
  • 举报
回复
这样吗? 12.3456789===》“12.3456789”;

64,651

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧