我如何在 C++ 中将双精度类型转换成字符串?
可以使用 std::to_string 将双精度类型转换成 C++ 中的字符串。が必要なパラメータは double 型の値であり、double 型の値が文字シーケンスとして含まれている文字列オブジェクトが返されます。
以下に C++ でこの内容を示したプログラムを示します。
例
#include <iostream> #include <string.h> using namespace std; int main() { double d = 238649.21316934; string s = to_string(d); cout << "Conversion of double to string: " << s; return 0; }
输出
上述プログラムの出力は次のようになります。
Conversion of double to string: 238649.213169
それでは、上記のプログラムについて理解していきましょう。
double 型の変数 d は 238649.21316934 に初期化されます。この double 型の値は to_string() を使用して文字列に変換されます。最後に、これが表示されます。この内容を示すコード スニペットを次に示します。
double d = 238649.21316934; string s = to_string(d); cout << "Conversion of double to string: " << s;
广告