重複の可能性:
c++ で setprecision は丸められますか? もしそうなら、なぜ私はこれを見ているのですか?
ここに私が使用している関数があります
char* round(double value, int decimal_places)
{
decimal_places = decimal_places+1;
std::stringstream s2;
s2 << std::setprecision(decimal_places) << std::setiosflags(std::ios_base::fixed) << value;
std::string st = s2.str();
return st;
}
私の入力値は 0.89425 で、小数宮殿の数は 4 です
私の出力は 0.8942 ですが、0.8943 が必要です。つまり、必要な小数点以下の桁数が 5 以上の場合、出力は次の値に丸められる必要があります。