std::to_wstring in c++

This function is used to convert the numerical value to the wide string i.e. it parses a numerical value of datatypes (int, long long, float, double ) to a wide string. It returns a wide string of data type wstring representing the numerical value passed in the function. In this function data type is being internally typecasted to the wstring data type and bypassing the numerical value as its parameter we can obtain a wstring type of string in return where the numerical value has been typecasted to the desired data type.

Syntax :

wstring to_wstring (int val);
wstring to_wstring (long long val);
wstring to_wstring (float val);
wstring to_wstring (double val);
Parameters :
val :This is the numerical value that is to be converted to the wide string.

Return Value :
It returns the passed numerical value into the wide string of data type wstring.

// C++ code to convert numerical value
// to wide string data tpye
// using the to_wstring function

// These header files contains wcout, 
// wstring and to_wstring
#include <iostream> 
#include <string>     
using namespace std;

//Driver code
int main ()
{

// Data types to be typecasted
float x = 3.1415926;
int a = 5 , b = 9;
double y = 6.29;

// numercial values being typecasted into wstring 
wstring pi = L"Pi is " + to_wstring(x);
wstring perfect = to_wstring(a+b) +
                    L" is a number";
wstring num = to_wstring(y/x) + 
                    L"is division of two numbers";

// Printing the typecasted wstring 
wcout << pi << L'\n';
wcout << perfect << L'\n';
wcout << num <<L'\n';
return 0;
}

Output:

Pi is 3.141593
14 is a number
2.002169is division of two numbers

Applications :
It can be used for calculation to be used in report statement such as average value so we need not write any statement for this purpose and we can directly use this function to convert numerical values.

Example : Suppose the HOD of a department is filing a report for number of sections in his department and he has to avoid data type casting so he can use to_wstring function to do this particular task.
Below is the implementation :

// These header files contains wcout and wstring
#include <iostream> 
#include <string>     

using namespace std;

// Driver code
int main ()
{

// Data types to be typecasted
int a = 600 , b = 150;

// numercial values being typecasted into wstring 
wstring rep = L"Number of section = " + to_wstring(a/b);
wstring sec_rep = to_wstring(b) + 
            L" is the number of students in each section";

// Printing the typecasted wstring 
wcout << rep << L'\n';
wcout << sec_rep << L'\n';
return 0;
}

Output:

Number of section =  4
150 is the number of students in each section

In the above example we need not typecast the data type every time but with the help of this function external typecasting is avoided which is beneficial for the system as external typecasting takes more time then the stl fucntions.


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.