C++ Program For Double to String Conversion Last Updated : 26 Jul, 2022 Comments Improve Suggest changes Like Article Like Report Here, we will build a C++ program for double to string conversion using various methods i.e. Using to_stringUsing stringstreamUsing sprintfUsing 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 1. Using to_string 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; } OutputString is: 456321.7651232. Using stringstream A double can also be converted into a string in C++ in different ways depending on our requirements using ostringstream. C++ // C++ Program to demonstrate Double to // String Conversion using string stream #include <iostream> #include <sstream> #include <string> using namespace std; int main() { ostringstream s; double n = 2332.43; s << n; string str = s.str(); cout << "String is:" << str; return 0; } OutputString is:2332.433. Using sprintf By specifying the precision in sprintf, we can convert double to string or character array with custom precision. We can use sprintf to add extra text (as required) to the string at the same time. C++ // C++ Program to demonstrate Double to // String Conversion using sprintf #include <cstring> #include <iostream> #include <string> #define Max_Digits 10 using namespace std; int main() { double N = 1243.3456; char str[Max_Digits + sizeof(char)]; std::sprintf(str, "%f", N); std::printf("string is: %s \n", str); return 0; } Outputstring is: 1243.345600 4. Using lexical_cast The lexical cast is one of the best ways to convert double to string. C++ // C++ Program to demonstrate Double to // String Conversion using lexical_cast #include <boost/lexical_cast.hpp> #include <iostream> #include <string> using namespace std; int main() { double n = 432.12; string str = boost::lexical_cast<string>(n); cout << "string is:" << str; return 0; } Outputstring is:432.12 Comment More infoAdvertise with us Next Article C++ Program For Double to String Conversion K ksrikanth0498 Follow Improve Article Tags : C++ Programs C++ C++ Conversion Programs Practice Tags : CPP Similar Reads C++ Program For String to Double Conversion There are situations, where we need to convert textual data into numerical values for various calculations. In this article, we will learn how to convert strings to double in C++. Methods to Convert String to Double We can convert String to Double in C++ using the following methods: Using stod() Fun 3 min read C++ Program For String to Long Conversion In this article, we will learn how to convert strings to long in C++. For this conversion, there are 3 ways as follows: Using stol()Using stoul()Using atol() Let's start by discussing each of these methods in detail. Example: Input: s1 = "20" s2 = "30" Output: s1 + s2 long: 50 1. Using stol() In C++ 3 min read C++ Program For Binary To Decimal Conversion The binary number system uses only two digits 0 and 1 to represent an integer and the Decimal number system uses ten digits 0 to 9 to represent a number. In this article, we will discuss the program for Binary to Decimal conversion in C++. Algorithm to Convert Binary Numbers to DecimalInitialize a v 4 min read C++ Program For char to int Conversion In C++, we cannot directly perform numeric operations on characters that represent numeric values. If we attempt to do so, the program will interpret the character's ASCII value instead of the numeric value it represents. We need to convert the character into an integer.Example:Input: '9'Output: 9Ex 2 min read C++ Program For int to char Conversion In this article, we will learn how to convert int to char in C++. For this conversion, there are 5 ways as follows: Using typecasting.Using static_cast.Using sprintf().Using to_string() and c_str().Using stringstream. Let's start by discussing each of these methods in detail. Examples: Input: N = 65 6 min read Like