如何在Python中将double转换为字符串?

Bil*_*ard 165 c++ string double

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

Ada*_*eld 185

// 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);
Run Code Online (Sandbox Code Playgroud)

  • 为C++ 11方式投票.我是C++的新手,并没有意识到有一个`to_string`函数.我正在使用Microsoft Visual C++ 2013. (11认同)
  • 重要提示:“std::to_string”的精度为 6 位。所以生成的字符串基本上是浮点型而不是双精度型。 (3认同)
  • 包括C方式的upvote.对我来说似乎更清洁. (2认同)

Joh*_*itb 179

升压(TM)方式:

std::string str = boost::lexical_cast<std::string>(dbl);
Run Code Online (Sandbox Code Playgroud)

标准C++方式:

std::ostringstream strs;
strs << dbl;
std::string str = strs.str();
Run Code Online (Sandbox Code Playgroud)

注意:别忘了#include <sstream>

  • 不要忘记`#include <sstream>`for c ++ way :) (26认同)
  • 谢谢.我正在使用标准C++方式. (15认同)
  • 我认为boost :: lexical_cast几乎是标准的C++方式,为您精心打包.顺便说一下,你有一个小错字 - "boot:lexical_cast". (3认同)
  • boost :: lexical_cast不仅仅是stringstream代码的简单包装器.许多转换例程都是内联实现的.根据本页底部的性能测量结果(http://www.boost.org/doc/libs/1_47_0/libs/conversion/lexical_cast.htm),boost :: lexical_cast比使用stringstreams更快,并且在大多数情况下案例,比scanf/printf更快 (2认同)
  • 为了文档,如果你没有`#include <sstream>`,你会收到一个错误"不允许不完整的类型." (2认同)
  • std :: to_string(dbl)在C++ 11中工作 (2认同)

ken*_*ytm 118

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

#include <string>

auto str = std::to_string(42.5); 
Run Code Online (Sandbox Code Playgroud)

to_stringN1803(r0),N1982(r1)和N2408(r2)" 简单数字访问 "中引入的新库函数.还有stod执行反向操作的功能.

如果您确实希望使用不同的输出格式"%f",请使用其他答案中所示的snprintfostringstream方法.

  • 哦耶宝宝.to_string和stod都可以在Visual Studio 11中使用. (3认同)
  • 这对小数字不起作用......例如:`1e-9`产生`0.000000` (3认同)

Kon*_*lph 24

如果您使用C++,请避免使用sprintf.这是un-C++ y并且有几个问题.Stringstreams是首选方法,最好封装在Boost.LexicalCast中,可以很容易地完成:

template <typename T>
std::string to_string(T const& value) {
    stringstream sstr;
    sstr << value;
    return sstr.str();
}
Run Code Online (Sandbox Code Playgroud)

用法:

string s = to_string(42.5);
Run Code Online (Sandbox Code Playgroud)


Yoc*_*mer 23

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

double d = 3.0;
std::string str = std::to_string(d);
Run Code Online (Sandbox Code Playgroud)

  • 我的数字非常小,“std::to_string()”只是返回 0.000000,而不是科学形式。 (2认同)

cop*_*pro 10

sprintf没关系,但在C++中,更好,更安全,也更慢的转换方式是stringstream:

#include <sstream>
#include <string>

// In some function:
double d = 453.23;
std::ostringstream os;
os << d;
std::string str = os.str();
Run Code Online (Sandbox Code Playgroud)

您还可以使用Boost.LexicalCast:

#include <boost/lexical_cast.hpp>
#include <string>

// In some function:
double d = 453.23;
std::string str = boost::lexical_cast<string>(d);
Run Code Online (Sandbox Code Playgroud)

在这两种情况下,str都应该在"453.23"之后.LexicalCast具有一些优势,可确保转换完成.它stringstream在内部使用s.


Dan*_*nyK 10

我会看一下C++ String Toolkit Libary.刚刚在别处发布了类似的答案.我发现它非常快速可靠.

#include <strtk.hpp>

double pi = M_PI;
std::string pi_as_string  = strtk::type_to_string<double>( pi );
Run Code Online (Sandbox Code Playgroud)


Fre*_*son 7

Herb Sutter有一篇关于字符串格式的优秀文章.我建议阅读它.我以前把它联系起来了.

  • 这不是答案,但会对该问题做出非常有用的评论。 (2认同)

小智 6

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