在C++中将float转换为std :: string

ada*_*m_0 62 c++ type-conversion

我有一个需要放入的浮点值std::string.如何从float转换为字符串?

float val = 2.5;
std::string my_val = val; // error here
Run Code Online (Sandbox Code Playgroud)

dmc*_*kee 120

从C++ 11开始,标准C++库为函数提供了std::to_string(arg)各种支持的类型arg.

  • 使用浮点数时,请注意std :: to_string()可能出现的意外行为.(请参阅此链接上的示例输出http://en.cppreference.com/w/cpp/string/basic_string/to_string). (7认同)

Geo*_*che 56

除非您担心性能,否则请使用字符串流:

std::ostringstream ss;
ss << myFloat;
std::string s(ss.str());
Run Code Online (Sandbox Code Playgroud)

如果你对Boost没问题,那么lexical_cast <>是一个方便的选择:

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

有效的替代方案是例如FastFormat或简单的C风格的功能.

  • 对于第一种方法,不要忘记`#include <sstream>`. (4认同)

dcp*_*dcp 15

您可以定义一个模板,该模板不仅可以用于双打,还可以用于其他类型.

template <typename T> string tostr(const T& t) { 
   ostringstream os; 
   os<<t; 
   return os.str(); 
} 
Run Code Online (Sandbox Code Playgroud)

然后你可以将它用于其他类型.

double x = 14.4;
int y = 21;

string sx = tostr(x);
string sy = tostr(y);
Run Code Online (Sandbox Code Playgroud)


Rik*_*ika 15

重要提示:请
阅读最后的说明.

快速回答:
使用to_string().(从c ++ 11开始提供)
示例:

#include <iostream>   
#include <string>  

using namespace std;
int main ()
{
    string pi = "pi is " + to_string(3.1415926);
    cout<< "pi = "<< pi << endl;

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

自己运行: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);
Run Code Online (Sandbox Code Playgroud)

重要提示:
正如@MichaelKonečný正确指出的那样,使用to_string()风险充其量只会导致意外结果.
来自http://en.cppreference.com/w/cpp/string/basic_string/to_string:

浮点类型std::to_string可能会产生意外结果,因为返回字符串中的有效位数可以为零,请参阅示例.
返回值可能与std::cout默认打印的显着不同,请参阅示例. std::to_string依赖于当前语言环境进行格式化,因此std::to_string来自多个线程的并发调用可能导致调用的部分序列化.C++17 提供std::to_chars了一种更高性能的与语言环境无关的替代方案.

最好的方法是stringstream其答案中使用@dcp等其他人.

以下示例演示了此问题:
自己运行示例:https://www.jdoodle.com/embed/v0/T4k

#include <iostream>
#include <sstream>
#include <string>

template < typename Type > std::string to_str (const Type & t)
{
  std::ostringstream os;
  os << t;
  return os.str ();
}

int main ()
{

  // more info : https://en.cppreference.com/w/cpp/string/basic_string/to_string
  double    f = 23.43;
  double    f2 = 1e-9;
  double    f3 = 1e40;
  double    f4 = 1e-40;
  double    f5 = 123456789;
  std::string f_str = std::to_string (f);
  std::string f_str2 = std::to_string (f2); // Note: returns "0.000000"
  std::string f_str3 = std::to_string (f3); // Note: Does not return "1e+40".
  std::string f_str4 = std::to_string (f4); // Note: returns "0.000000"
  std::string f_str5 = std::to_string (f5);

  std::cout << "std::cout: " << f << '\n'
    << "to_string: " << f_str << '\n'
    << "ostringstream: " << to_str (f) << "\n\n"
    << "std::cout: " << f2 << '\n'
    << "to_string: " << f_str2 << '\n'
    << "ostringstream: " << to_str (f2) << "\n\n"
    << "std::cout: " << f3 << '\n'
    << "to_string: " << f_str3 << '\n'
    << "ostringstream: " << to_str (f3) << "\n\n"
    << "std::cout: " << f4 << '\n'
    << "to_string: " << f_str4 << '\n'
    << "ostringstream: " << to_str (f4) << "\n\n"
    << "std::cout: " << f5 << '\n'
    << "to_string: " << f_str5 << '\n'
    << "ostringstream: " << to_str (f5) << '\n';

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

std::cout: 23.43
to_string: 23.430000
ostringstream: 23.43

std::cout: 1e-09
to_string: 0.000000
ostringstream: 1e-09

std::cout: 1e+40
to_string: 10000000000000000303786028427003666890752.000000
ostringstream: 1e+40

std::cout: 1e-40
to_string: 0.000000
ostringstream: 1e-40

std::cout: 1.23457e+08
to_string: 123456789.000000
ostringstream: 1.23457e+08 
Run Code Online (Sandbox Code Playgroud)

  • 使用浮点数时,请注意std :: to_string()可能出现的意外行为.(请参阅此链接上的示例输出http://en.cppreference.com/w/cpp/string/basic_string/to_string). (3认同)

vit*_*aut 5

std::to_chars一旦您的标准库提供它,请使用:

std::array<char, 32> buf;
auto result = std::to_chars(buf.data(), buf.data() + buf.size(), val);
if (result.ec == std::errc()) {
  auto str = std::string(buf.data(), result.ptr - buf.data());
  // use the string
} else {
  // handle the error
}
Run Code Online (Sandbox Code Playgroud)

这种方法的优点是:

  • 它与语言环境无关,可以防止在将数据写入需要 '.' 的 JSON 等格式时出现错误。作为小数点
  • 它提供最短的十进制表示和往返保证
  • 它可能比其他标准方法更有效,因为它不使用语言环境并且不需要分配

不幸的std::to_string是,浮点数的实用性有限,因为它使用固定表示,将小值舍入为零并为大值生成长字符串,例如

auto s1 = std::to_string(1e+40);
// s1 == 10000000000000000303786028427003666890752.000000

auto s2 = std::to_string(1e-40);
// s2 == 0.000000
Run Code Online (Sandbox Code Playgroud)

C++20 可能会获得更方便的std::formatAPI,其好处std::to_charsP0645标准提案获得批准相同。