5
私はあなたの助けが必要な変換問題に直面しています。私はgcc4コンパイラを使用していますが、gcc4の使用はかなり制限されています。語彙キャスト文字列を2倍にする
std :: stringをdoubleに変換したいと考えています。ところで
std::string aQuantity = aRate.getQuantity();
std::string aAmount = aRate.getAmount();
// aAmount = "22.05"
double dQuantity = boost::lexical_cast<double>(aQuantity);
double dAmount = boost::lexical_cast<double> (aAmount);
// dAmount = 22.050000000000001
、私はまた、atof
を試してみましたが、私はまだ同じ問題を持っています。 istringstream
とsetprecission(2)
を使用してaAmount
で示される正しい値を得る方法はありますか?
精度が低いだけで印刷してください。常に同じ精度が格納されます。 – chris
'boost :: lexical_cast'関数は実際に' std :: istringstream'を内部的に使用して、値の解析/抽出を行います。 –
もし私が次のことをしたら、私は代わりに22を得ています... 'std :: stringstream precisionValue; precisionValue.precision(2); precisionValue << boost :: lexical_cast(aAmount)<< std :: endl; double dAmount; precisionValue >> dAmount; //今すぐ取得します。 ' –
Nostradamus