我需要将双精度存储为字符串。我知道printf
如果我想显示它可以使用,但我只想将它存储在一个字符串变量中,以便以后可以将它存储在地图中(作为value,而不是key)。
17 回答
// The C way:
char buffer[32];
snprintf(buffer, sizeof(buffer), "%g", myDoubleVar);
// The C++03 way:
std::ostringstream sstream;
sstream << myDoubleVar;
std::string varAsString = sstream.str();
// The C++11 way:
std::string varAsString = std::to_string(myDoubleVar);
// The boost way:
std::string varAsString = boost::lexical_cast<std::string>(myDoubleVar);
升压(tm)方式:
std::string str = boost::lexical_cast<std::string>(dbl);
标准 C++方式:
std::ostringstream strs;
strs << dbl;
std::string str = strs.str();
注意:不要忘记#include <sstream>
您可以在 C++11中使用std::to_string
double d = 3.0;
std::string str = std::to_string(d);
如果您使用 C++,请避免使用sprintf
. 它不是 C++y 并且有几个问题。Stringstreams 是选择的方法,最好像Boost.LexicalCast一样封装,这可以很容易地完成:
template <typename T>
std::string to_string(T const& value) {
stringstream sstr;
sstr << value;
return sstr.str();
}
用法:
string s = to_string(42.5);
sprintf
没关系,但在 C++ 中,更好、更安全且速度稍慢的转换方法是stringstream
:
#include <sstream>
#include <string>
// In some function:
double d = 453.23;
std::ostringstream os;
os << d;
std::string str = os.str();
您还可以使用Boost.LexicalCast:
#include <boost/lexical_cast.hpp>
#include <string>
// In some function:
double d = 453.23;
std::string str = boost::lexical_cast<string>(d);
在这两种情况下,str
都应该在"453.23"
之后。LexicalCast 有一些优点,它可以确保转换是完整的。它在内部使用stringstream
s。
我会看看C++ String Toolkit Libary。刚刚在其他地方发布了类似的答案。我发现它非常快速和可靠。
#include <strtk.hpp>
double pi = M_PI;
std::string pi_as_string = strtk::type_to_string<double>( pi );
lexical_cast 的问题是无法定义精度。通常,如果您要将双精度数转换为字符串,那是因为您想将其打印出来。如果精度太大或太小,都会影响您的输出。
您也可以使用stringstream。
嘿,我刚刚写了这个(与这个问题无关):
string temp = "";
stringstream outStream;
double ratio = (currentImage->width*1.0f)/currentImage->height;
outStream << " R: " << ratio;
temp = outStream.str();
/* rest of the code */
看看sprintf()
和家人。
您可能想阅读我之前在 SO 上的帖子。(带有临时 ostringstream 对象的宏版本。)
作为记录:在我自己的代码中,我喜欢 snprintf()。使用本地堆栈上的 char 数组,它并不是那么低效。(好吧,也许如果您超出了数组大小并循环执行两次...)
(我还通过 vsnprintf() 包装了它。但这需要我进行一些类型检查。如果你想要代码,请叫喊......)
Normaly 对于此操作,您必须使用 ecvt、fcvt 或 gcvt 函数:
/* gcvt example */
#include <stdio.h>
#include <stdlib.h>
main ()
{
char buffer [20];
gcvt (1365.249,6,buffer);
puts (buffer);
gcvt (1365.249,3,buffer);
puts (buffer);
return 0;
}
Output:
1365.25
1.37e+003
作为一个函数:
void double_to_char(double f,char * buffer){
gcvt(f,10,buffer);
}
请注意,字符串只是双精度的表示,将其转换回双精度可能不会产生相同的值。另请注意,默认字符串转换可能会将转换修剪到一定的精度。在标准 C++ 方式中,您可以按如下方式控制精度:
#include <sstream>
#include <math.h>
#include <iostream>
#include <iomanip>
int main()
{
std::ostringstream sout;
sout << M_PI << '\n';
sout << std::setprecision(99) << M_PI << '\n';
sout << std::setprecision(3) << M_PI << '\n';
sout << std::fixed; //now the setprecision() value will look at the decimal part only.
sout << std::setprecision(3) << M_PI << '\n';
std::cout << sout.str();
}
这会给你输出
3.14159
3.141592653589793115997963468544185161590576171875
3.14
3.142
使用to_string()
.
例子 :
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string pi = "pi is " + to_string(3.1415926);
cout<< "pi = "<< pi << endl;
return 0;
}
自己运行:http: //ideone.com/7ejfaU
这些也可用:
string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);
您可以尝试更紧凑的样式:
std::string number_in_string;
double number_in_double;
std::ostringstream output;
number_in_string = (dynamic_cast< std::ostringstream*>(&(output << number_in_double <<
std::endl)))->str();
您可以使用此功能将任何事物转换为任何事物:
template<class T = std::string, class U>
T to(U a) {
std::stringstream ss;
T ret;
ss << a;
ss >> ret;
return ret;
};
用法 :
std::string str = to(2.5);
double d = to<double>("2.5");