如何在 C++ 中将双精度数转换为字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/332111/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:42:14  来源:igfitidea点击:

How do I convert a double into a string in C++?

c++stringdouble

提问by Bill the Lizard

I need to store a double as a string. I know I can use printfif I wanted to display it, but I just want to store it in a string variable so that I can store it in a map later (as the value, not the key).

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

回答by Adam Rosenfield

// 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);

回答by Johannes Schaub - litb

The boost (tm)way:

升压(TM)方式:

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

The Standard C++way:

标准C ++方式:

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

Note: Don't forget #include <sstream>

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

回答by kennytm

The Standard C++11way (if you don't care about the output format):

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

#include <string>

auto str = std::to_string(42.5); 

to_stringis a new library function introduced in N1803(r0), N1982(r1) and N2408(r2) "Simple Numeric Access". There are also the stodfunction to perform the reverse operation.

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

If you do want to have a different output format than "%f", use the snprintfor ostringstreammethods as illustrated in other answers.

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

回答by Yochai Timmer

You can use std::to_stringin C++11

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

double d = 3.0;
std::string str = std::to_string(d);

回答by Konrad Rudolph

If you use C++, avoid sprintf. It's un-C++y and has several problems. Stringstreams are the method of choice, preferably encapsulated as in Boost.LexicalCastwhich can be done quite easily:

如果您使用 C++,请避免sprintf. 它是非 C++y 并且有几个问题。Stringstreams 是首选的方法,最好像Boost.LexicalCast一样封装,这可以很容易地完成:

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

Usage:

用法:

string s = to_string(42.5);

回答by coppro

sprintfis okay, but in C++, the better, safer, and also slightly slower way of doing the conversion is with stringstream:

sprintf没问题,但在 C++ 中,更好、更安全且速度稍慢的转换方式是stringstream

#include <sstream>
#include <string>

// In some function:
double d = 453.23;
std::ostringstream os;
os << d;
std::string str = os.str();

You can also use Boost.LexicalCast:

您还可以使用Boost.LexicalCast

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

// In some function:
double d = 453.23;
std::string str = boost::lexical_cast<string>(d);

In both instances, strshould be "453.23"afterward. LexicalCast has some advantages in that it ensures the transformation is complete. It uses stringstreams internally.

在这两种情况下,str都应该在"453.23"之后。LexicalCast 有一些优点,因为它确保转换是完整的。它stringstream在内部使用s。

回答by DannyK

I would look at the C++ String Toolkit Libary. Just posted a similar answer elsewhere. I have found it very fast and reliable.

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

#include <strtk.hpp>

double pi = M_PI;
std::string pi_as_string  = strtk::type_to_string<double>( pi );

回答by Fred Larson

Herb Sutter has an excellent article on string formatting. I recommend reading it. I've linked it beforeon SO.

Herb Sutter 有一篇关于字符串格式的优秀文章。我建议阅读它。我之前在 SO 上链接过它

回答by Fred Larson

The problem with lexical_cast is the inability to define precision. Normally if you are converting a double to a string, it is because you want to print it out. If the precision is too much or too little, it would affect your output.

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

回答by Firas Assaad

You could also use stringstream.

您也可以使用stringstream