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 Octal To Decimal Conversion Given an octal number as input, we need to write a program to convert the given octal number into an equivalent decimal number. Examples: Input : 67Output: 55 Input : 512Output: 330 Input : 123Output: 83 1. Simple ApproachThe idea is to extract the digits of a given octal number starting from the ri 2 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 C++ Program to Perform Calculations in Pure Strings Given a string of operations containing three operands for each operation "type of command", "first operand", and "second operand". Calculate all the commands given in this string format. In other words, you will be given a pure string that will ask you to perform an operation and you have to perfor 5 min read How to Convert wstring to double in C++ In C++, std::wstring is a type of string where each character is of a wide character type. Converting wstring to a double in C++ can be a common requirement especially when dealing with the inputs that are stored as unicode characters. In this article, we will learn how to convert a wstring to a dou 2 min read C++ Program For Binary To Octal Conversion The problem is to convert the given binary number (represented as a string) to its equivalent octal number. The input could be very large and may not fit even into an unsigned long long int. Examples: Input: 110001110Output: 616 Input: 1111001010010100001.010110110011011Output: 1712241.26633 Simple 5 min read C Program to Extract Characters From a String Character extraction can be done by iterating through the string in the form of a character array. It basically means plucking out a certain amount of characters from an array or a string. Now, to take input in C we do it by using the following methods: scanf("%c",&str[i]); - Using a loopscanf(" 2 min read Like