将双精度转换为字符串 C++?

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

Convert double to string C++?

c++stringdoubletostring

提问by Mentalikryst

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

可能的重复:
如何在 C++ 中将双精度值转换为字符串?

I want to combine a string and a double and g++ is throwing this error:

我想将一个字符串和一个双精度组合起来,g++ 抛出这个错误:

main.cpp: In function ‘int main()':
main.cpp:40: error: invalid operands of types ‘const char [2]' and ‘double' to binary ‘operator+'

main.cpp:在函数“int main()”中:
main.cpp:40:错误:“const char [2]”和“double”类型的无效操作数转换为二进制“operator+”

Here is the line of code which it is throwing the error on:

这是它抛出错误的代码行:

storedCorrect[count] = "("+c1+","+c2+")";

storedCorrect[] is a string array, and c1 and c2 are both doubles. Is there a way to convert c1 and c2 to strings to allow my program to compile correctly?

storageCorrect[] 是一个字符串数组,c1 和 c2 都是双精度型。有没有办法将 c1 和 c2 转换为字符串以允许我的程序正确编译?

回答by Adam Rosenfield

You can't do it directly. There are a number of ways to do it:

你不能直接做。有多种方法可以做到:

  1. Use a std::stringstream:

    std::ostringstream s;
    s << "(" << c1 << ", " << c2 << ")";
    storedCorrect[count] = s.str()
    
  2. Use boost::lexical_cast:

    storedCorrect[count] = "(" + boost::lexical_cast<std::string>(c1) + ", " + boost::lexical_cast<std::string>(c2) + ")";
    
  3. Use std::snprintf:

    char buffer[256];  // make sure this is big enough!!!
    snprintf(buffer, sizeof(buffer), "(%g, %g)", c1, c2);
    storedCorrect[count] = buffer;
    
  1. 使用std::stringstream

    std::ostringstream s;
    s << "(" << c1 << ", " << c2 << ")";
    storedCorrect[count] = s.str()
    
  2. 使用boost::lexical_cast

    storedCorrect[count] = "(" + boost::lexical_cast<std::string>(c1) + ", " + boost::lexical_cast<std::string>(c2) + ")";
    
  3. 使用std::snprintf

    char buffer[256];  // make sure this is big enough!!!
    snprintf(buffer, sizeof(buffer), "(%g, %g)", c1, c2);
    storedCorrect[count] = buffer;
    

There are a number of other ways, using various double-to-string conversion functions, but these are the main ways you'll see it done.

还有许多其他方法,使用各种双字符到字符串转换函数,但这些是您将看到它完成的主要方法。

回答by kennytm

In C++11, use std::to_stringif you can accept the default format (%f).

在C ++ 11,使用std::to_string,如果你能接受默认的格式(%f)。

storedCorrect[count]= "(" + std::to_string(c1) + ", " + std::to_string(c2) + ")";

回答by Michael Kristofik

Use std::stringstream. Its operator <<is overloaded for all built-in types.

使用std::stringstream. 它operator <<为所有内置类型重载。

#include <sstream>    

std::stringstream s;
s << "(" << c1 << "," << c2 << ")";
storedCorrect[count] = s.str();

This works like you'd expect - the same way you print to the screen with std::cout. You're simply "printing" to a string instead. The internals of operator <<take care of making sure there's enough space and doing any necessary conversions (e.g., doubleto string).

这就像您期望的那样工作 - 与您使用std::cout. 您只是简单地“打印”到一个字符串。的内部负责operator <<确保有足够的空间并进行任何必要的转换(例如,doubleto string)。

Also, if you have the Boost library available, you might consider looking into lexical_cast. The syntax looks much like the normal C++-style casts:

此外,如果您有可用的 Boost 库,您可以考虑查看lexical_cast. 语法看起来很像普通的 C++ 风格的类型转换:

#include <string>
#include <boost/lexical_cast.hpp>
using namespace boost;

storedCorrect[count] = "(" + lexical_cast<std::string>(c1) +
                       "," + lexical_cast<std::string>(c2) + ")";

Under the hood, boost::lexical_castis basically doing the same thing we did with std::stringstream. A key advantage to using the Boost library is you can go the other way (e.g., stringto double) just as easily. No more messing with atof()or strtod()and raw C-style strings.

在幕后,boost::lexical_cast基本上是在做我们对std::stringstream. 使用 Boost 库的一个关键优势是您可以轻松地采用另一种方式(例如,stringto double)。不再弄乱atof()strtod()和原始 C 样式字符串。

回答by aJ.

std::string stringify(double x)
 {
   std::ostringstream o;
   if (!(o << x))
     throw BadConversion("stringify(double)");
   return o.str();
 }

C++ FAQ: http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.1

C++ 常见问题:http: //www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.1

回答by mp.

I believe the sprintf is the right function for you. I's in the standard library, like printf. Follow the link below for more information:

我相信 sprintf 是适合您的功能。我在标准库中,比如 printf。请点击以下链接了解更多信息:

http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/

http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/