C++ std::string 浮动或加倍
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1012571/
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
std::string to float or double
提问by Max Frai
I'm trying to convert std::string
to float/double
.
I tried:
我正在尝试转换std::string
为float/double
. 我试过:
std::string num = "0.6";
double temp = (double)atof(num.c_str());
But it always returns zero. Any other ways?
但它总是返回零。还有其他方法吗?
回答by TimW
std::string num = "0.6";
double temp = ::atof(num.c_str());
Does it for me, it is a valid C++ syntax to convert a string to a double.
对我来说,将字符串转换为双精度值是有效的 C++ 语法。
You can do it with the stringstream or boost::lexical_cast but those come with a performance penalty.
您可以使用 stringstream 或 boost::lexical_cast 来实现,但它们会带来性能损失。
Ahaha you have a Qt project ...
啊哈哈你有一个Qt项目......
QString winOpacity("0.6");
double temp = winOpacity.toDouble();
Extra note:
If the input data is a const char*
, QByteArray::toDouble
will be faster.
额外注意:
如果输入数据是 a const char*
,QByteArray::toDouble
会更快。
回答by ManuelSchneid3r
The Standard Library (C++11) offers the desired functionality with std::stod
:
标准库 (C++11) 提供了所需的功能std::stod
:
std::string s = "0.6"
std::wstring ws = "0.7"
double d = std::stod(s);
double dw = std::stod(ws);
Generally for most other basic types, see <string>
. There are some new features for C strings, too. See <stdlib.h>
通常对于大多数其他基本类型,请参阅<string>
。C 字符串也有一些新功能。看<stdlib.h>
回答by Bill Lynch
Lexical cast is very nice.
词法转换非常好。
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <string>
using std::endl;
using std::cout;
using std::string;
using boost::lexical_cast;
int main() {
string str = "0.6";
double dub = lexical_cast<double>(str);
cout << dub << endl;
}
回答by Edison Gustavo Muenz
You can use std::stringstream:
您可以使用 std::stringstream:
#include <sstream>
#include <string>
template<typename T>
T StringToNumber(const std::string& numberAsString)
{
T valor;
std::stringstream stream(numberAsString);
stream >> valor;
if (stream.fail()) {
std::runtime_error e(numberAsString);
throw e;
}
return valor;
}
Usage:
用法:
double number= StringToNumber<double>("0.6");
回答by DaClown
Yes, with a lexical cast. Use a stringstream and the << operator, or use Boost, they've already implemented it.
是的,有一个词法转换。使用 stringstream 和 << 运算符,或者使用 Boost,他们已经实现了。
Your own version could look like:
您自己的版本可能如下所示:
template<typename to, typename from>to lexical_cast(from const &x) {
std::stringstream os;
to ret;
os << x;
os >> ret;
return ret;
}
回答by stefanB
You can use boost lexical cast:
您可以使用 boost 词法转换:
#include <boost/lexical_cast.hpp>
string v("0.6");
double dd = boost::lexical_cast<double>(v);
cout << dd << endl;
Note: boost::lexical_cast throws exception so you should be prepared to deal with it when you pass invalid value, try passing string("xxx")
注意:boost::lexical_cast 抛出异常,所以当你传递无效值时你应该准备好处理它,尝试传递 string("xxx")
回答by haavee
If you don't want to drag in all of boost, go with strtod(3)
from <cstdlib>
- it already returns a double.
如果您不想拖入所有 boost,请使用strtod(3)
from <cstdlib>
- 它已经返回一个双精度值。
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
using namespace std;
int main() {
std::string num = "0.6";
double temp = ::strtod(num.c_str(), 0);
cout << num << " " << temp << endl;
return 0;
}
Outputs:
输出:
$ g++ -o s s.cc
$ ./s
0.6 0.6
$
Why atof() doesn't work ... what platform/compiler are you on?
为什么 atof() 不起作用......你在什么平台/编译器上?
回答by kenn
I had the same problem in Linux
我在 Linux 中遇到了同样的问题
double s2f(string str)
{
istringstream buffer(str);
double temp;
buffer >> temp;
return temp;
}
it works.
有用。
回答by dpetek
double myAtof ( string &num){
double tmp;
sscanf ( num.c_str(), "%lf" , &tmp);
return tmp;
}
回答by BSalita
The C++ 11 way is to use std::stod and std::to_string. Both work in Visual Studio 11.
C++ 11 的方式是使用 std::stod 和 std::to_string。两者都适用于 Visual Studio 11。