wcstod() function in C/C++

The wcstod() function converts the wide string as a double. This function interprets the contents of a wide string as a floating point number. If endString is not a null pointer, the function also sets the value of endString to point to the first character after the number.

Syntax:

double wcstod( const wchar_t* str, wchar_t** str_end )

Parameters: The function accepts two mandatory parameters which are described below:

Return value: The function returns two value as below:

Below programs illustrate the above function:
Program 1 :



filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// wcstod() function
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // initialize the wide string
    // beginning with the floating point number
    wchar_t string[] = L"95.6Geek";
  
    // Pointer to a pointer to a wide character
    wchar_t* endString;
  
    // Convert wide string to double
    double value = wcstod(string, &endString);
  
    // print the string, starting double value
    // and its endstring
    wcout << L"String -> " << string << "\n";
    wcout << L"Double value -> " << value << "\n";
    wcout << L"End String is : " << endString << "\n";
  
    return 0;
}
chevron_right

Output:
String -> 95.6Geek
Double value -> 95.6
End String is : Geek

Program 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// wcstod() function
// with no endString characters
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // initialize the wide string
    // beginning with the floating point number
    wchar_t string[] = L"10.6464";
  
    // Pointer to a pointer to a wide character
    wchar_t* endString;
  
    // Convert wide string to double
    double value = wcstod(string, &endString);
  
    // print the string, starting double value
    // and its endstring
    wcout << L"String -> " << string << "\n";
    wcout << L"Double value -> " << value << "\n";
    wcout << L"End String is : " << endString << "\n";
  
    return 0;
}
chevron_right

Output:
String -> 10.6464
Double value -> 10.6464
End String is :

Program 3 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// wcstod() function
// with whitespace present at the beginning
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // initialize the wide string
    // beginning with the floating point number
    wchar_t string[] = L"            99.999Geek";
  
    // Pointer to a pointer to a wide character
    wchar_t* endString;
  
    // Convert wide string to double
    double value = wcstod(string, &endString);
  
    // print the string, starting double value
    // and its endstring
    wcout << L"String -> " << string << "\n";
    wcout << L"Double value -> " << value << "\n";
    wcout << L"End String is : " << endString << "\n";
  
    return 0;
}
chevron_right

Output:
String ->             99.999Geek
Double value -> 99.999
End String is : Geek

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.





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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
C
Practice Tags :