The wcstold() function also sets a pointer to point to the first wide character after the last valid character of the wide string if there is any, otherwise the pointer is set to null.
It is defined in <cwchar> header file.
wcstold() prototype
long double wcstold( const wchar_t* str, wchar_t** str_end );
The wcstold() function takes wide string and a pointer to wide character as its parameter, interprets the content of the wide string as a floating point number and returns a long double value.
wcstold() Parameters
- str: A wide string having the representation of a floating point number.
- str_end: Pointer to a pointer to a wide character. The value of str_end is set by the function to the next character in str after the last valid character. This parameter can also be a null pointer.
wcstold() Return value
The wcstold() function returns:
- a long double value (which is converted from the wide string).
- 0.0 if no valid conversion could be performed.
If the converted value is out of the range, a range error occurs and a positive or negative HUGE_VAL is returned.
Example 1: How wcstold() function works?
#include <cwchar>
#include <clocale>
#include <iostream>
using namespace std;
int main()
{
setlocale(LC_ALL, "en_US.UTF-8");
wchar_t str[] = L"93.410\u03b7\u05ea";
wchar_t *end;
long double value;
value = wcstold(str,&end);
wcout << L"Wide String = " << str << endl;
wcout << L"Long Double value = " << value << endl;
wcout << L"End String = " << end << endl;
return 0;
}
When you run the program, the output will be:
Wide String = 93.410ηת Long Double value = 93.41 End String = ηת
Example 2: wcstold() function without trailing characters
#include <cwchar>
#include <clocale>
#include <iostream>
using namespace std;
int main()
{
setlocale(LC_ALL, "en_US.UTF-8");
wchar_t str[] = L"93.410";
wchar_t *end;
long double value;
value = wcstold(str,&end);
wcout << L"Wide String = " << str << endl;
wcout << L"Long Double value = " << value << endl;
wcout << L"End String = " << end << endl;
return 0;
}
When you run the program, the output will be:
Wide String = 93.410 Long Double value = 93.41 End String =
A valid floating-point value for wcstold() function consists of an optional + or - sign followed by one of the following sets:
- For decimal floating-point value:
- A group of decimal digits (0-9), optionally containing a decimal point (.). For example: 13.170, -5.63, etc .
- An optional exponent part (e or E) followed by an optional + or - sign and non-empty sequence of decimal digits. For example: 3.46101e+007, 13.19e-013, etc.
- For hexadecimal floating-point value:
- A string starting with 0x or 0X, followed by a non-empty sequence of hexadecimal digits, optionally containing a decimal point (.). For Example: 0xfa5, -0xb1f.24, etc.
- An optional exponent part (p or P) followed by an optional + or - sign and non-empty sequence of hexadecimal digits. For Example: 0x51c.23p5, -0x2a.3p-3, etc.
- Infinity:
- INF or INFINITY (ignoring case). For Example: -Inf, InfiNiTy, etc.
- NaN (Not a Number):
- NAN or NANsequence (ignoring case) where sequence is a sequence of characters consisting only of alphanumeric characters or the underscore (_).The result is a quiet NaN. For Example: Nan, NaNab1, etc.
Example 3: How wcstold() works with exponents and hexadecimals?
#include <cwchar>
#include <clocale>
#include <iostream>
using namespace std;
int main()
{
setlocale(LC_ALL, "en_US.UTF-8");
wchar_t str1[] = L"34.901\u00c6\u00f1";
wchar_t str2[] = L"0Xfc1.a12\u03c7r12";
wchar_t *end;
long double value;
value = wcstold(str1,&end);
wcout << L"Wide String = " << str1 << endl;
wcout << L"Long Double value = " << value << endl;
wcout << L"End String = " << end << endl;
value = wcstold(str2,&end);
wcout << L"Wide String = " << str2 << endl;
wcout << L"Long Double value = " << value << endl;
wcout << L"End String = " << end << endl;
return 0;
}
When you run the program, the output will be:
Wide String = 34.901Æñ Long Double value = 34.901 End String = Æñ Wide String = 0Xfc1.a12χr12 Long Double value = 4033.63 End String = χr12
Example 4: wcstold cases for INFINITY and NaN
#include <cwchar>
#include <clocale>
#include <iostream>
using namespace std;
int main()
{
setlocale(LC_ALL, "en_US.UTF-8");
wchar_t str1[] = L"inFinityx\u03a3y";
wchar_t str2[] = L"NaN22\u0429";
wchar_t *end;
long double value;
value = wcstold(str1,&end);
wcout << L"Wide String = " << str1 << endl;
wcout << L"Long Double value = " << value << endl;
wcout << L"End String = " << end << endl;
value = wcstold(str2,&end);
wcout << L"Wide String = " << str2 << endl;
wcout << L"Long Double value = " << value << endl;
wcout << L"End String = " << end << endl;
return 0;
}
When you run the program, the output will be:
Wide String = inFinityxΣy Long Double value = inf End String = xΣy Wide String = NaN22Щ Long Double value = nan End String = 22Щ
In general, a valid floating point argument for wcstold() function has the following form:
[whitespace] [- | +] [digits] [.digits] [ {e | E }[- | +]digits]
The wcstold() function ignores all the leading whitespace characters until the primary non-whitespace character is found.
Then, beginning from this character, it takes as many characters as possible that forms a valid floating-point representation and converts them to a floating point value. Whatever is left of the string after the last valid character is stored in the object pointed by str_end.
Example 5: wcstold() function with leading whitespace
#include <cwchar>
#include <clocale>
#include <iostream>
using namespace std;
int main()
{
setlocale(LC_ALL, "en_US.UTF-8");
wchar_t str[] = L" 59.013\u0915\u0975";
wchar_t *end;
long double value;
value = wcstold(str,&end);
wcout << L"Wide String = " << str << endl;
wcout << L"Long Double value = " << value << endl;
wcout << L"End String = " << end << endl;
return 0;
}
When you run the program, the output will be:
Wide String = 59.013कॵ Long Double value = 59.013 End String = कॵ