C++ Program For String to Double Conversion Last Updated : 04 Aug, 2023 Comments Improve Suggest changes Like Article Like Report 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() FunctionUsing stold() FunctionUsing atof() Function Let's discuss each of these methods in detail. 1. Using stod() Function In C++, the stod() function performs a string to double conversion. It is a library function defined inside <string.h> header file. Syntaxdouble stod(const string& str, size_t* pos = 0); Parameters str: the string to convert.pos: address of an integer to store the number of characters processed. This parameter can also be a null pointer, in which case it is not used. Return Value Converted double value.C++ Program to Convert String to Double Using stod() Function C++ // C++ program to convert string // to double using stod() #include <iostream> using namespace std; // Driver code int main() { char s1[] = "14.25"; char s2[] = "34.87"; double n1 = stod(s1); double n2 = stod(s2); double res = n1 + n2; cout << res; return 0; } Output49.12Complexity AnalysisTime complexity: O(1)Auxiliary Space: O(1)2. Using stold() Function In C++, the stold() function performs a string to long double conversion. Like stod() function, this function is also defined inside <string.h> header file. Syntaxlong double stold(const string& str, size_t *pos = 0); Parameters str: the string to convert.pos: address of integer to store the index of the first unconverted character. This parameter can also be a null pointer, in which case it is not used. Return Value Converted long double value.C++ Program to Convert String to Long Double Using stold() Function C++ // C++ program to convert string // to long double using stold() #include <iostream> using namespace std; // Driver code int main() { char s1[] = "14.25"; char s2[] = "34.87"; long double n1 = stold(s1); long double n2 = stold(s2); long double res = n1 + n2; cout << res; return 0; } Output49.12Complexity AnalysisTime complexity: O(1)Auxiliary Space: O(1)3. Using atof() Function In C++, the atof() method translates a string's contents as a floating-point integer and returns its value as a double. It is declared inside <cstdlib> header file. Syntaxdouble atof (const char * str) Parameters The function accepts a single mandatory parameter str which is the string representation of a floating-point number. Return Value Converted long double value.C++ Program to Convert String to Double Using atof() Function C++ // C++ program to convert string // to double using atof() #include <iostream> using namespace std; // Driver code int main() { char s[] = "14.25"; double num = atof(s); cout << num; return 0; } Output14.25Complexity AnalysisTime complexity: O(1)Auxiliary Space: O(1)Related ArticlesConvert String to int in C++ Comment More infoAdvertise with us Next Article C++ Program For String to Double Conversion L laxmigangarajula03 Follow Improve Article Tags : C++ Programs C++ C Conversion Programs Practice Tags : CPP Similar Reads 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_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.76 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 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 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 Convert Float to String In C++ In this article, we learn how we can convert float to string in C++ using different methods: Using the to_string()Using stringstreamUsing MacrosUsing lexical_cast from the boost library1. Using to_string() The to_string() method takes a single integer variable or other data type and converts it into 3 min read C++ Program to compare two string using pointers Given two strings, compare the strings using pointers Examples: Input: str1 = geeks, str2 = geeks Output: Both are equal Input: str1 = hello, str2 = hellu Output: Both are not equal As their length are same but characters are different The idea is to dereference given pointers, compare values and ad 1 min read Like