String in scientific notation C++ to double conversion(科学计数法 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++ 中的字符串到双重转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:科学计数法 C++ 中的字符串到双重转换


基础教程推荐
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- CString 到 char* 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16