に変換しようとしstd::stringていfloat/doubleます。私は試した:
std::string num = "0.6";
double temp = (double)atof(num.c_str());
ただし、常にゼロを返します。他の方法は?
に変換しようとしstd::stringていfloat/doubleます。私は試した:
std::string num = "0.6";
double temp = (double)atof(num.c_str());
ただし、常にゼロを返します。他の方法は?
回答:
std::string num = "0.6";
double temp = ::atof(num.c_str());
私のためにそれをします、それは文字列をdoubleに変換する有効なC ++構文です。
stringstreamまたはboost :: lexical_castでそれを行うことができますが、これらにはパフォーマンスのペナルティが伴います。
Ahha Qtプロジェクトがあります...
QString winOpacity("0.6");
double temp = winOpacity.toDouble();
エクストラノート:
入力データがある場合はconst char*、QByteArray::toDoubleより高速になります。
標準ライブラリ(C ++ 11)は、以下の機能を備えていますstd::stod。
std::string s = "0.6"
std::wstring ws = "0.7"
double d = std::stod(s);
double dw = std::stod(ws);
一般に、他のほとんどの基本的なタイプについては、を参照してください<string>。C文字列にもいくつかの新機能があります。見る<stdlib.h>
ostringstreamそれ自体が一人で利用してみましょう、あまりにも長いアウト型にした...
語彙のキャストはとてもいいです。
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <string>
using std::endl;
using std::cout;
using std::string;
using boost::lexical_cast;
int main() {
string str = "0.6";
double dub = lexical_cast<double>(str);
cout << dub << endl;
}
try { ... boost::lexical_cast ... } catch (std::exception const& err) { //handle excpetion }
catch ( boost::bad_lexical_cast const& err )して例外をキャッチします。
std :: stringstream:を使用できます。
#include <sstream>
#include <string>
template<typename T>
T StringToNumber(const std::string& numberAsString)
{
T valor;
std::stringstream stream(numberAsString);
stream >> valor;
if (stream.fail()) {
std::runtime_error e(numberAsString);
throw e;
}
return valor;
}
使用法:
double number= StringToNumber<double>("0.6");
すべてのブーストをドラッグしたくない場合は、strtod(3)fromを使用し<cstdlib>ます。すでにdoubleが返されます。
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
using namespace std;
int main() {
std::string num = "0.6";
double temp = ::strtod(num.c_str(), 0);
cout << num << " " << temp << endl;
return 0;
}
出力:
$ g++ -o s s.cc
$ ./s
0.6 0.6
$
atof()が機能しないのはなぜですか?どのプラットフォーム/コンパイラを使用していますか?
この答えはあなたのコメントでlitbをバックアップしています。結果が適切に表示されていないという深刻な疑いがあります。
まったく同じことが一度は起こりました。私は1日を費やして、なぜ64ビットのintに不正な値が入っているのかを理解するために費やしましたが、printfが2番目のバイトを無視していることを発見しただけです。64ビット値をそのintのようにprintfに渡すことはできません。
なぜatof()元の質問で機能しないのかについて:それが2倍にキャストされるという事実は私を疑わしくします。コードはなし#include <stdlib.h>でコンパイルすべきではありませんが、キャストがコンパイル警告を解決するために追加された場合、atof()正しく宣言されません。コンパイラがatof()intを返すと想定している場合、キャストすると変換警告が解決されますが、戻り値がdoubleとして認識されません。
#include <stdlib.h>
#include <string>
...
std::string num = "0.6";
double temp = atof(num.c_str());
警告なしで動作するはずです。
Boostを方程式にドラッグするのではなく、文字列を(一時的に)として保持してchar[]を使用できますsprintf()。
しかしもちろん、とにかくBoostを使用している場合は、それほど問題にはなりません。
とにかく、文字列<->浮動小数点のBoost lexical_castは必要ありません。このユースケースのサブセットは、以前の関数よりもブーストが一貫して悪い唯一のセットであり、基本的にはすべての障害をそこに集中させています。これは、独自のパフォーマンス結果が、そのような変換にsscanfやprintfを使用するよりも20-25倍遅いパフォーマンスを示しているためです。
自分でグーグル。boost :: lexical_castは50の変換のようなものを処理でき、浮動小数点数#を含む変換を除外すると、明らかな代替案と同じかそれ以上に優れています(これらすべての操作に対して単一のAPIを持つという追加の利点があります)。しかし、フロートを持ち込むと、パフォーマンスの点でタイタニック号が氷山を打つようになります。
古い専用のstr-> double関数は、すべて30ミリ秒(またはそれ以上)のような処理で10000パースできます。lexical_castが同じ処理を実行するには、650ミリ秒程度かかります。
私の問題:
私の解決策(Windows関数_wcstod_lを使用):
// string to convert. Note: decimal seperator is ',' here
std::wstring str = L"1,101";
// Use this for error detection
wchar_t* stopString;
// Create a locale for "C". Thus a '.' is expected as decimal separator
double dbl = _wcstod_l(str.c_str(), &stopString, _create_locale(LC_ALL, "C"));
if (wcslen(stopString) != 0)
{
// ... error handling ... we'll run into this because of the separator
}
HTH ...この解決策を得るのにかなり時間がかかりました。それでも、文字列のローカライズなどについては十分に理解していないと感じています...