190

我需要将双精度存储为字符串。我知道printf如果我想显示它可以使用,但我只想将它存储在一个字符串变量中,以便以后可以将它存储在地图中(作为value,而不是key)。

4

17 回答 17

213
// 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);
于 2008-12-01T20:39:48.850 回答
193

升压(tm)方式:

std::string str = boost::lexical_cast<std::string>(dbl);

标准 C++方式:

std::ostringstream strs;
strs << dbl;
std::string str = strs.str();

注意:不要忘记#include <sstream>

于 2008-12-01T20:42:11.263 回答
137

标准 C++11方式(如果您不关心输出格式):

#include <string>

auto str = std::to_string(42.5); 

to_stringN1803 (r0)、N1982 (r1) 和N2408 (r2)“简单数值存取”中引入的新库函数。还有stod执行反向操作的功能。

如果您确实希望具有与 不同的输出格式"%f",请使用snprintforostringstream其他答案中说明的方法。

于 2011-11-04T19:09:27.623 回答
30

您可以在 C++11中使用std::to_string

double d = 3.0;
std::string str = std::to_string(d);
于 2017-03-27T12:35:09.267 回答
24

如果您使用 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);
于 2008-12-01T20:41:01.703 回答
11

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 有一些优点,它可以确保转换是完整的。它在内部使用stringstreams。

于 2008-12-01T20:41:52.993 回答
10

我会看看C++ String Toolkit Libary。刚刚在其他地方发布了类似的答案。我发现它非常快速和可靠。

#include <strtk.hpp>

double pi = M_PI;
std::string pi_as_string  = strtk::type_to_string<double>( pi );
于 2015-06-08T15:48:50.393 回答
6

lexical_cast 的问题是无法定义精度。通常,如果您要将双精度数转换为字符串,那是因为您想将其打印出来。如果精度太大或太小,都会影响您的输出。

于 2009-09-22T15:29:59.147 回答
4

您也可以使用stringstream

于 2008-12-01T20:37:46.280 回答
4

嘿,我刚刚写了这个(与这个问题无关):

string temp = "";
stringstream outStream;
double ratio = (currentImage->width*1.0f)/currentImage->height;
outStream << " R: " << ratio;
temp = outStream.str();

/* rest of the code */
于 2008-12-01T20:41:21.170 回答
2

看看sprintf()和家人。

于 2008-12-01T20:37:06.343 回答
2

您可能想阅读我之前在 SO 上的帖子。(带有临时 ostringstream 对象的宏版本。)

作为记录:在我自己的代码中,我喜欢 snprintf()。使用本地堆栈上的 char 数组,它并不是那么低效。(好吧,也许如果您超出了数组大小并循环执行两次...)

(我还通过 vsnprintf() 包装了它。但这需要我进行一些类型检查。如果你想要代码,请叫喊......)

于 2008-12-02T06:48:50.883 回答
2

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);
}
于 2015-11-21T14:42:15.867 回答
2

请注意,字符串只是双精度的表示,将其转换回双精度可能不会产生相同的值。另请注意,默认字符串转换可能会将转换修剪到一定的精度。在标准 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  
于 2021-05-08T15:02:47.563 回答
1

使用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);
于 2016-02-11T16:55:15.217 回答
0

您可以尝试更紧凑的样式:

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(); 
于 2014-04-10T23:36:13.863 回答
0

您可以使用此功能将任何事物转换为任何事物:

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");
于 2016-12-05T06:11:29.623 回答