怎样把一个字符串转化为double类型呢

Graveworm 2009-06-09 10:48:00
都说用atof(const char* str), 可是这个函数会损失精度,比如3.14就会变为3.1。
这个函数是一个c函数,c++有没有这样的函数呢?
...全文
932 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
Graveworm 2009-06-09
  • 打赏
  • 举报
回复
原来果然是输出的问题。。。。 20楼正解啊!
谢谢大家的关注和帮助!
hongzao 2009-06-09
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 Graveworm 的回复:]
引用 5 楼 steedhorse 的回复:
C/C++ code#include <iostream>usingnamespacestd;intmain() {
cout < <atof("3.14") < <endl;return0;
}

在我机器上输出3.14,没问题啊。WinXP + Visual Studio 2005。



难道是gcc编译器的问题?我用的是g++
[/Quote]

gcc也没那么低, 小数点有加个7,8位没问题,应该比7,8位还要高.除非你的环境有问题,只要不是对数据的精度过分的要求,atof的精度肯定是够用的.
别逗我乐 2009-06-09
  • 打赏
  • 举报
回复
shi得atof的精度没那么低的
mymtom 2009-06-09
  • 打赏
  • 举报
回复
atof过时了,又无法检测错误:strtod

#include <stdlib.h>
double strtod(const char * restrict nptr, char ** restrict endptr);
float strtof(const char * restrict nptr, char ** restrict endptr);
long double strtold(const char * restrict nptr, char ** restrict endptr);
吴丁 2009-06-09
  • 打赏
  • 举报
回复
不可能!我用它从来没有把后面给砍掉。。。
goodname 2009-06-09
  • 打赏
  • 举报
回复
应该是输出语句的事情

cout<<fixed<<setprecision(2)<<temp<<endl;//输出2位小数
晨星 2009-06-09
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 Graveworm 的回复:]
不只是输出格式的问题,我把temp存储到vector中,同样损失了精度
[/Quote]
你还是检查一下程序吧,vector只是个容器,数据进去是什么样,出来肯定还是什么样。
除非被存储的对象实现了一很变态的拷贝构造函数。double型又无所谓拷贝构造,不存在这种问题。
Graveworm 2009-06-09
  • 打赏
  • 举报
回复
        double temp ;
string out_text = "11374.15";
const char *str = out_text.c_str();
cout<<str<<endl;
temp = atof(str);
cout<<temp<<endl;


output:
11374.15
11374.1

好像大一点的数字就会出现精度损失,这样的话应该怎么处理呢?
ameyume 2009-06-09
  • 打赏
  • 举报
回复
double值本身是不精确地

string a = "4.42";
double s = atof(a.c_str());
cout<<s<<endl;

我这里debug时s=4.41999999999999
Graveworm 2009-06-09
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 ameyume 的回复:]
输出格式的问题吧
C/C++ codedoubletemp;constchar*str="27.25";
cout<<str<<endl;
temp=(double)atof(str);
cout<<temp<<endl;
以上代码,输出的temp是27,但是debug跟踪temp = 27.250000000000000
[/Quote]

不只是输出格式的问题,我把temp存储到vector中,同样损失了精度
ameyume 2009-06-09
  • 打赏
  • 举报
回复
输出格式的问题吧

double temp;
const char *str="27.25";
cout<<str<<endl;
temp = (double)atof(str);
cout<<temp<<endl;

以上代码,输出的temp是27,但是debug跟踪temp = 27.250000000000000
goodname 2009-06-09
  • 打赏
  • 举报
回复
那就是out_text有问题了,你怎么输入的
Graveworm 2009-06-09
  • 打赏
  • 举报
回复
真奇怪,测试下面的程序是没有问题的

  string a = "4.42";
double s = atof(a.c_str());
cout<<s<<endl;
fengrx 2009-06-09
  • 打赏
  • 举报
回复
楼主用这个先测试一下,确定下是哪儿出问题了:
cout << atof("3.14") << endl;
Graveworm 2009-06-09
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 goodname 的回复:]
是不是你得输出只输出了一位小数阿
[/Quote]

没有啊

        cout<<out_text<<endl;
temp = (double)atof(out_text.c_str());

cout<<temp<<endl;
goodname 2009-06-09
  • 打赏
  • 举报
回复
是不是你得输出只输出了一位小数阿
Graveworm 2009-06-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 steedhorse 的回复:]
C/C++ code#include<iostream>usingnamespacestd;intmain() {
cout<<atof("3.14")<<endl;return0;
}

在我机器上输出3.14,没问题啊。WinXP + Visual Studio 2005。
[/Quote]

难道是gcc编译器的问题?我用的是g++
healer_kx 2009-06-09
  • 打赏
  • 举报
回复
atof, 不可能那么低的。。。
youzhipeng 2009-06-09
  • 打赏
  • 举报
回复
问题不是atof的事情,而是楼主没考虑到。
楼主可能查看值的方法有问题。


double A = atof("3.74365");

你这样看看A的大小? 用VS2005的监视器看看。
Graveworm 2009-06-09
  • 打赏
  • 举报
回复

double temp;
string out_text;
...
cout<<out_text.c_str()<<endl; //out_text的内容来自文件
temp = (double)atof(out_text.c_str());


output:
27301.25
27301.2
50958.04
50958
3413.40
3413.4
加载更多回复(9)

65,203

社区成员

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

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