How to use to_string In C++
In C++, use std::to string to convert a double to a string. The required parameter is a double value, and a string object containing the double value as a sequence of characters is returned.
C++
// C++ Program to demonstrate Double to // String Conversion using to_string #include <iostream> #include <string.h> using namespace std; int main() { double n = 456321.7651234; string str = to_string(n); cout << "String is: " << str; return 0; } |
Output
String is: 456321.765123
C++ Program For Double to String Conversion
Here, we will build a C++ program for double to string conversion using various methods i.e.
- Using to_string
- Using stringstream
- Using sprintf
- Using lexical_cast
We will keep the same input in all the mentioned approaches and get an output accordingly.
Input:
n = 456321.7651234
Output:
string: 456321.7651234