函式
<cwchar>

wcstod

double wcstod (const wchar_t* str, wchar_t** endptr);
將寬字串轉換為double
解析 C 寬字串 str,將其內容解釋為浮點數,並以double型別返回其值。如果 endptr 不是空指標,則該函式還會將 endptr 的值設定為指向數字後面的第一個字元。

這是 strtod<cstdlib>)的寬字元等效函式,其解釋 str 的方式與 strtod 相同。

引數

str
以浮點數表示形式開頭的 C 寬字串。
endptr
對一個已分配的wchar_t*型別物件的引用,函式會將其值設定為 str 中數值部分之後的下一個字元。
此引數也可以是空指標,在這種情況下,函式不會使用它。

返回值

成功時,函式返回轉換後的浮點數,型別為double.
如果無法執行有效的轉換,則函式返回零(0.0).
如果正確值超出該型別可表示值的範圍,則返回正的或負的 HUGE_VAL,並將 errno 設定為 ERANGE
如果正確值會導致下溢,則函式返回一個其絕對值不大於最小的歸一化正數的值(某些庫實現也可能在此情況下將 errno 設定為 ERANGE)。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
/* wcstod example */
#include <wchar.h>

int main ()
{
  wchar_t szOrbits[] = L"365.24 29.53";
  wchar_t * pEnd;
  double d1, d2;
  d1 = wcstod (szOrbits,&pEnd);
  d2 = wcstod (pEnd,NULL);
  wprintf (L"The moon completes %.2f orbits per Earth year.\n", d1/d2);
  return 0;
}

輸出

The moon completes 12.37 orbits per Earth year.


另見