科学记数法中的字符串 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:57:35  来源:igfitidea点击:

String in scientific notation C++ to double conversion

c++mathtype-conversionscientific-notation

提问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。

回答by Russell Newquist

You want the standard c function atof([A]SCII to [F]loat, but it actually uses doubles rather than floats).

您需要标准的 c 函数atof([A]SCII 到 [F]loat,但它实际上使用双精度数而不是浮点数)。