wcstod()函数将宽字符串转换为double 。此函数将宽字符串的内容解释为浮点数。如果endString不是空指针,则该函数还会将endString的值设置为指向数字后的第一个字符。
句法:
double wcstod( const wchar_t* str, wchar_t** str_end )
参数:该函数接受两个强制性参数,如下所述:
- 字符串:指定以浮点数表示形式开头的字符串
- endString:指定指向宽字符的指针
返回值:该函数返回两个值,如下所示:
- 成功后,函数将转换后的浮点数作为double类型的值返回。
- 如果无法执行有效的转换,则返回0.0。
- 如果正确的值超出该类型可表示的值的范围,则返回正数或负数HUGE_VAL,并将errno设置为ERANGE。
- 如果正确的值将导致下溢,则该函数将返回一个其大小不大于最小归一化正数的值(在这种情况下,某些库实现也可能会将errno设置为ERANGE)。
下面的程序说明了上述函数:
程序1:
// C++ program to illustrate
// wcstod() function
#include
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;
}
输出:
String -> 95.6Geek
Double value -> 95.6
End String is : Geek
程序2:
// C++ program to illustrate
// wcstod() function
// with no endString characters
#include
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;
}
输出:
String -> 10.6464
Double value -> 10.6464
End String is :
程序3:
// C++ program to illustrate
// wcstod() function
// with whitespace present at the beginning
#include
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;
}
输出:
String -> 99.999Geek
Double value -> 99.999
End String is : Geek
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。