我如何在 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;

更新日:14-Sep-2023

29K+ ビュー

開啟你的 職業生涯

完成課程認證

開始
廣告
© . All rights reserved.