科学记数法中的字符串 C++ 到双重转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1710447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String in scientific notation C++ to double conversion
提问by miya
I've got a database filled up with doubles like the following one:
我有一个充满双打的数据库,如下所示:
1.60000000000000000000000000000000000e+01
Does anybody know how to convert a number like that to a double in C++?
有人知道如何在 C++ 中将这样的数字转换为双精度数吗?
Is there a "standard" way to do this type of things? Or do I have to roll my own function?
有没有一种“标准”的方式来做这种事情?还是我必须推出自己的功能?
Right now I'm doing sth like this:
现在我正在做这样的事情:
#include <string>
#include <sstream>
int main() {
std::string s("1.60000000000000000000000000000000000e+01");
std::istringstream iss(s);
double d;
iss >> d;
d += 10.303030;
std::cout << d << std::endl;
}
Thanks!
谢谢!
回答by Thomas
Something like this? This would be the "C++" way of doing it...
像这样的东西?这将是“C++”的做法......
#include <sstream>
using namespace std;
// ...
string s = "1.60000000000000000000000000000000000e+01";
istringstream os(s);
double d;
os >> d;
cout << d << endl;
Prints 16.
打印 16。