C++ 中的wcstod() 函數將寬字符串的內容解釋為浮點數,並將其值作為雙精度值返回。
wcstod() 函數還設置一個指針,指向寬字符串的最後一個有效字符之後的第一個寬字符(如果有),否則指針設置為空。
它在<cwchar> 頭文件中定義。
wcstod()原型
double wcstod( const wchar_t* str, wchar_t** str_end );
wcstod() 函數將寬字符串和指向寬字符的指針作為其參數,將寬字符串的內容解釋為浮點數並返回雙精度值。
參數:
str
:具有浮點數表示的寬字符串。str_end
:指向寬字符指針的指針。 str_end 的值由函數設置為 str 中最後一個有效字符之後的下一個字符。該參數也可以是空指針。
返回:
wcstod() 函數返回:
- 一個雙精度值(從寬字符串轉換而來)。
- 如果無法執行有效轉換,則為 0.0。
如果轉換後的值超出範圍,則會發生範圍錯誤並返回正數或負數HUGE_VAL。
示例 1:wcstod() 函數如何工作?
#include <cwchar>
#include <clocale>
#include <iostream>
using namespace std;
int main()
{
setlocale(LC_ALL, "en_US.UTF-8");
wchar_t str[] = L"83.201xz\u0496\u0687";
wchar_t *end;
double value;
value = wcstod(str,&end);
wcout << L"Wide String = " << str << endl;
wcout << L"Double value = " << value << endl;
wcout << L"End String = " << end << endl;
return 0;
}
運行程序時,輸出將是:
Wide String = 83.201xzҖڇ Double value = 83.201 End String = xzҖڇ
示例 2:wcstod() 函數沒有尾隨字符
#include <cwchar>
#include <clocale>
#include <iostream>
using namespace std;
int main()
{
setlocale(LC_ALL, "en_US.UTF-8");
wchar_t str[] = L"83.201";
wchar_t *end;
double value;
value = wcstod(str,&end);
wcout << L"Wide String = " << str << endl;
wcout << L"Double value = " << value << endl;
wcout << L"End String = " << end << endl;
return 0;
}
運行程序時,輸出將是:
Wide String = 83.201 Double value = 83.201 End String =
wcstod() 函數的有效浮點值包含可選的 + 或 - 符號,後跟以下集合之一:
- 對於十進製浮點值:
- 一組十進製數字 (0-9),可選擇包含小數點 (.)。例如:13.170、-5.63 等。
- 可選的 index 部分(e 或 E),後跟可選的 + 或 - 符號和非空的十進製數字序列。例如:3.46101e+007、13.19e-013等。
- 對於十六進製浮點值:
- 以 0x 或 0X 開頭的字符串,後跟一個非空的十六進製數字序列,可以選擇包含小數點 (.)。例如:0xfa5、-0xb1f.24 等。
- 可選的 index 部分(p 或 P),後跟可選的 + 或 - 符號和非空的十六進製數字序列。例如:0x51c.23p5、-0x2a.3p-3 等。
- Infinity:
- INF 或 INFINITY(忽略大小寫)。例如:-Inf、InfiNiTy 等。
- NaN(不是數字):
- 南或南序列(忽略大小寫)其中序列是僅由字母數字字符或下劃線 (_) 組成的字符序列。結果是安靜的 NaN。例如:Nan、NaNab1 等。
示例 3:wcstod() 如何與 index 和十六進製一起使用?
#include <cwchar>
#include <clocale>
#include <iostream>
using namespace std;
int main()
{
setlocale(LC_ALL, "en_US.UTF-8");
wchar_t str1[] = L"-136.31e-2End\u03c8";
wchar_t str2[] = L"0x11a.2c\u05ea";
wchar_t *end;
double value;
value = wcstod(str1,&end);
wcout << L"Wide String = " << str1 << endl;
wcout << L"Double value = " << value << endl;
wcout << L"End String = " << end << endl;
value = wcstod(str2,&end);
wcout << L"Wide String = " << str2 << endl;
wcout << L"Double value = " << value << endl;
wcout << L"End String = " << end << endl;
return 0;
}
運行程序時,輸出將是:
Wide String = -136.31e-2Endψ Double value = -1.3631 End String = Endψ Wide String = 0x11a.2cת Double value = 282.172 End String = ת
示例 4:INFINITY 和 NaN 的 wcstod 案例
#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"NaN11\u0429";
wchar_t *end;
double value;
value = wcstod(str1,&end);
wcout << L"Wide String = " << str1 << endl;
wcout << L"Double value = " << value << endl;
wcout << L"End String = " << end << endl;
value = wcstod(str2,&end);
wcout << L"Wide String = " << str2 << endl;
wcout << L"Double value = " << value << endl;
wcout << L"End String = " << end << endl;
return 0;
}
運行程序時,輸出將是:
Wide String = -inFinityxΣy Double value = -inf End String = xΣy Wide String = NaN11Щ Double value = nan End String = 11Щ
通常,wcstod() 函數的有效浮點參數具有以下形式:
[whitespace] [- | +] [digits] [.digits] [ {e | E }[- | +]digits]
wcstod() 函數會忽略所有前導空白字符,直到找到主要的非空白字符。
然後,從這個字符開始,盡可能多的字符組成一個有效的浮點表示並將它們轉換為浮點值。最後一個有效字符之後字符串的剩餘部分存儲在str_end 指向的對象中。
示例 5:wcstod() 帶有前導空格的函數
#include <cwchar>
#include <clocale>
#include <iostream>
using namespace std;
int main()
{
setlocale(LC_ALL, "en_US.UTF-8");
wchar_t str[] = L" 21.69\u04f8aa";
wchar_t *end;
double value;
value = wcstod(str,&end);
wcout << L"Wide String = " << str << endl;
wcout << L"Double value = " << value << endl;
wcout << L"End String = " << end << endl;
return 0;
}
運行程序時,輸出將是:
Wide String = 21.69Ӹaa Double value = 21.69 End String = Ӹaa
相關用法
- C++ wcstod()用法及代碼示例
- C++ wcstold()用法及代碼示例
- C++ wcstok()用法及代碼示例
- C++ wcstof()用法及代碼示例
- C++ wcstol()用法及代碼示例
- C++ wcstombs()用法及代碼示例
- C++ wcstoull()用法及代碼示例
- C++ wcstoll()用法及代碼示例
- C++ wcstoul()用法及代碼示例
- C++ wcsftime()用法及代碼示例
- C++ wcscspn()用法及代碼示例
- C++ wcsncmp()用法及代碼示例
- C++ wcsstr()用法及代碼示例
- C++ wcsrchr()用法及代碼示例
- C++ wcsncpy()用法及代碼示例
- C++ wcslen()用法及代碼示例
- C++ wcsspn()用法及代碼示例
- C++ wcschr()用法及代碼示例
- C++ wcspbrk()用法及代碼示例
- C++ wcscat()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ wcstod()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。