doubleをC ++の文字列に変換するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/332111

  •  22-07-2019
  •  | 
  •  

質問

doubleを文字列として保存する必要があります。表示したい場合はprintfを使用できますが、後でマップに保存できるように文字列変数に保存するだけです( value ではなく、 キー)。

役に立ちましたか?

解決

ブースト(tm)の方法:

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

標準C ++ の方法:

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

#include <sstream>

を忘れないでください

他のヒント

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

標準C ++ 11 の方法(出力形式を気にしない場合):

#include <string>

auto str = std::to_string(42.5); 

to_string は、で導入された新しいライブラリ関数です。 href = "http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1803.html" rel = "noreferrer"> N1803 (r0)、 N1982 (r1)および N2408 (r2)<!> quot; シンプル数値アクセス <!> quot;。逆操作を実行するstod関数もあります。

"%f"とは異なる出力形式を使用する場合は、他の回答に示されているsnprintfまたはostringstreamメソッドを使用します。

C ++を使用する場合は、sprintfを避けてください。非C ++ yであり、いくつかの問題があります。文字列ストリームは、 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);

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には、変換の完了を保証するという利点があります。内部で<=> sを使用します。

C ++ String Toolkit Libary を参照します。他の場所に同様の回答を投稿したところです。非常に高速で信頼性が高いことがわかりました。

#include <strtk.hpp>

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

Herb Sutterには、優れた文字列のフォーマットに関する記事があります。読むことをお勧めします。 SOで前にリンクしました。

lexical_castの問題は、精度を定義できないことです。通常、doubleを文字列に変換しているのは、それを出力したいためです。精度が大きすぎるか小さすぎる場合、出力に影響します。

stringstream も使用できます。

はい、これを書いたばかりです(この質問とは無関係です):

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

/* rest of the code */

SOに関する以前の投稿を読むことをお勧めします。 (一時的なostringstreamオブジェクトを使用したマクロ版)。

記録用:私自身のコードでは、snprintf()を好みます。ローカルスタック上のchar配列では、それほど効率的ではありません。 (まあ、配列サイズを超えてループを2回実行すると...)

(vsnprintf()を介してラップしました。しかし、そのために型チェックが必要になります。コードが必要な場合はYelp ...)

sprintf()と家族を見てください。

通常、この操作では、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);
}

よりコンパクトなスタイルを試すことができます:

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

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

この関数を使用して、あらゆるものをあらゆるものに変換できます。

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");
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top