Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I convert a double into a string in C++?
A double datatype is used to represent a decimal or exponential value and string represents a sequence of characters.
In this article, we are having a double value and our task is to convert the given double into string. Here is a list of approaches to convert double to string in C++ that we will be discussing in this article with stepwise explanation and complete example codes.
- Using to_string() Function
- Using ostringstream
- Using stringstream
- Using sprintf() Function
- Using to_chars() Function
Using to_string() Function
In this approach, we have used the std::to_string() function for converting double into a string. The to_string() function converts the given double value into a string value.
Example
Here is an example where we have converted a double into a string using to_string():
#include <bits/stdc++.h>
using namespace std;
int main() {
double num = 3.141592;
string str = to_string(num);
cout << "String value: " << str << endl;
return 0;
}
Using ostringstream
You can use the ostringstream of <sstream> header for converting the double into a string.
Example
Here is an example of using 'ostringstream' for the conversion of double into a string:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
double num = 3.141592;
ostringstream oss;
oss << num; //stores the double value in the oss stream
string str = oss.str(); //converts the stream to string
cout << "String value: " << str << endl;
return 0;
}
Using stringstream
For converting a double another approach is using stringstream of <sstream> header. It is similar to the ostringstream used in the previous approach.
Example
The following example uses the stringstream and converts the given double value into a string:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
double num = 3.141592;;
stringstream ss;
ss << num;
string str = ss.str();
cout << "String value:: " << str << endl;
return 0;
}
Using sprintf() Function
In this approach of conversion of a double to string, we have used the sprintf() function of <cstdio> header. It stores the double value into a character buffer in decimal form using "%f". Then we convert the character buffer to a string using the string constructor.
Example
In this example, we have used the sprintf() function to convert the given double value into a string.
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main() {
double num = 3.141592;;
char buffer[50];
sprintf(buffer, "%f", num); // Stores num in buffer in decimal form
string str(buffer); // Converts buffer to string
cout << "String value: " << str << endl;
return 0;
}
Using to_chars() Function
To convert a double into a string, we have used the to_chars() function of the <charconv> header. The to_chars() function converts the given double value to a character array.
- Using the to_chars() function, we have converted the given double value to the character and stored them in the buffer.
- The above step returns a pointer and an error code (ec).
- Then using the string constructor, we convert the character in the range specified by pointer buffer to pointer.
Example
Here is an example of implementing the above-mentioned steps for converting a double value to a string:
#include <iostream>
#include <charconv>
#include <string>
using namespace std;
int main() {
double num = 3.141592;;
char buffer[32];
//Converts num to character and stores them in buffer
auto [pointer, ec] = to_chars(buffer, buffer + sizeof(buffer), num);
//Converts the character array to string
string str(buffer, pointer);
cout << "String value: " << str << endl;
return 0;
}