當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。