科学计数法 C++ 中的字符串到双重转换 [英] String in scientific notation C++ to double conversion

查看:81
本文介绍了科学计数法 C++ 中的字符串到双重转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库,里面装满了像下面这样的双精度数:

I've got a database filled up with doubles like the following one:

1.60000000000000000000000000000000000e+01

有人知道如何在 C++ 中将这样的数字转换为双精度数吗?

Does anybody know how to convert a number like that to a double in 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;
}

谢谢!

推荐答案

像这样?这将是C++"的做法......

Something like this? This would be the "C++" way of doing it...

#include <sstream>
using namespace std;

// ...

    string s = "1.60000000000000000000000000000000000e+01";
    istringstream os(s);
    double d;
    os >> d;
    cout << d << endl;

打印 16.

这篇关于科学计数法 C++ 中的字符串到双重转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆