std::wcstof, std::wcstod, std::wcstold
來自 cppreference.com
在標頭檔案 <cwchar> 中定義 |
||
float wcstof( const wchar_t* str, wchar_t** str_end ); |
(C++11 起) | |
double wcstod( const wchar_t* str, wchar_t** str_end ); |
||
long double wcstold( const wchar_t* str, wchar_t** str_end ); |
(C++11 起) | |
解釋 str 指向的寬字串中的浮點值。
函式會忽略任何空白字元(由 std::iswspace 確定),直到找到第一個非空白字元。然後,它儘可能多地獲取字元以形成有效的浮點表示,並將其轉換為浮點值。有效的浮點值可以是以下之一:
- 十進位制浮點表示式。它由以下部分組成:
- (可選) 加號或減號
- 非空十進位制數字序列,可選地包含小數點字元(由當前的 C 區域設定 確定)(定義有效數)
- (可選)
e
或E
,後跟可選的減號或加號以及非空十進位制數字序列(定義以 10 為底的指數)
|
(C++11 起) |
- 當前已安裝的 C 區域設定 可能接受的任何其他表示式。
函式將 str_end 指向的指標設定為指向已解釋的最後一個字元之後的寬字元。如果 str_end 是空指標,則忽略它。
目錄 |
[edit] 引數
str | - | 指向要解釋的以 null 結尾的寬字串的指標 |
str_end | - | 指向寬字元指標的指標 |
[edit] 返回值
成功時返回與 str 內容對應的浮點值。如果轉換後的值超出相應返回型別的範圍,則發生範圍錯誤並返回 HUGE_VAL、HUGE_VALF 或 HUGE_VALL。如果無法執行轉換,則返回 0。
[edit] 示例
執行此程式碼
#include <cerrno> #include <clocale> #include <cwchar> #include <iostream> #include <string> int main() { const wchar_t* p = L"111.11 -2.22 0X1.BC70A3D70A3D7P+6 -Inf 1.18973e+4932zzz"; wchar_t* end; std::wcout << "Parsing L\"" << p << "\":\n"; for (double f = std::wcstod(p, &end); p != end; f = std::wcstod(p, &end)) { std::wcout << " '" << std::wstring(p, end-p) << "' -> "; p = end; if (errno == ERANGE) { std::wcout << "range error, got "; errno = 0; } std::wcout << f << '\n'; } if (std::setlocale(LC_NUMERIC, "de_DE.utf8")) { std::wcout << L"With de_DE.utf8 locale:\n"; std::wcout << L" '123.45' -> " << std::wcstod(L"123.45", 0) << L'\n'; std::wcout << L" '123,45' -> " << std::wcstod(L"123,45", 0) << L'\n'; } }
輸出
Parsing L"111.11 -2.22 0X1.BC70A3D70A3D7P+6 -Inf 1.18973e+4932zzz": '111.11' -> 111.11 ' -2.22' -> -2.22 ' 0X1.BC70A3D70A3D7P+6' -> 111.11 ' -Inf' -> -inf ' 1.18973e+4932' -> range error, got inf With de_DE.utf8 locale: '123.45' -> 123 '123,45' -> 123.45
[edit] 參閱
將位元組字串轉換為浮點值 (函式) | |
C 文件 對應 wcstof
|