当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ wcstod()用法及代码示例


wcstod()函数将宽字符串转换为双精度。此函数将宽字符串的内容解释为浮点数。如果endString不是空指针,则该函数还会将endString的值设置为指向数字后的第一个字符。

用法:

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

参数:该函数接受两个强制性参数,如下所述:


  • string: 指定以浮点数表示形式开头的字符串
  • endString: 指定指向宽字​​符的指针

返回值:该函数返回两个值,如下所示:

  • 成功后,函数将转换后的浮点数作为double类型的值返回。
  • 如果无法执行有效的转换,则返回0.0。
  • 如果正确值超出该类型可表示值的范围,则返回正数或负数HUGE_VAL,并将errno设置为ERANGE。
  • 如果正确的值将导致下溢,则该函数将返回一个其大小不大于最小归一化正数的值(在这种情况下,某些库实现也可能会将errno设置为ERANGE)。

以下示例程序旨在说明上述函数:
示例1:

// 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; 
}
输出:
String -> 95.6Geek
Double value -> 95.6
End String is : Geek

示例2:

// 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; 
}
输出:
String -> 10.6464
Double value -> 10.6464
End String is :

示例3:

// 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; 
}
输出:
String ->             99.999Geek
Double value -> 99.999
End String is : Geek


相关用法


注:本文由纯净天空筛选整理自AmanSrivastava1大神的英文原创作品 wcstod() function in C/C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。