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


C++ wcstof()用法及代碼示例

C++ 中的wcstof() 函數將寬字符串的內容解釋為浮點數,並將其值作為浮點數返回。

此函數還設置一個指針,指向寬字符串的最後一個有效字符(如果有)之後的第一個寬字符,否則將指針設置為空。

它在<cwchar> 頭文件中定義。

wcstof()原型

float wcstof( const wchar_t* str, wchar_t** str_end );

wcstof() 函數將寬字符串和指向寬字符的指針作為其參數,將寬字符串的內容解釋為浮點數並返回浮點值。

參數:

  • str :具有浮點數表示的寬字符串。
  • str_end:指向寬字符指針的指針。 str_end 的值由函數設置為 str 中最後一個有效字符之後的下一個字符。該參數也可以是空指針。

返回:

wcstof() 函數返回:

  • 一個浮點值(從寬字符串轉換而來)。
  • 如果無法執行有效轉換,則為 0.0。

如果轉換後的值超出範圍,則會發生範圍錯誤並返回正數或負數HUGE_VAL。

示例 1:wcstof() 函數如何工作?

#include <cwchar>
#include <clocale>
#include <iostream>
using namespace std;

int main()
{
	setlocale(LC_ALL, "en_US.UTF-8");

	wchar_t str[] = L"40.001\u220fc12";
	wchar_t *end;
	float value;
	value = wcstof(str,&end);

	wcout << L"Wide String = " << str << endl;
	wcout << L"Float value = " << value << endl;
	wcout << L"End String = " << end << endl;

	return 0;
}

運行程序時,輸出將是:

Wide String = 40.001∏c12
Float value = 40.001
End String = ∏c12

示例 2:wcstof() 函數沒有尾隨字符

#include <cwchar>
#include <clocale>
#include <iostream>
using namespace std;

int main()
{
	setlocale(LC_ALL, "en_US.UTF-8");

	wchar_t str[] = L"791.513";
	wchar_t *end;
	float value;
	value = wcstof(str,&end);

	wcout << L"Wide String = " << str << endl;
	wcout << L"Float value = " << value << endl;
	wcout << L"End String = " << end << endl;
	
	return 0;
}

運行程序時,輸出將是:

Wide String = 791.513
Float value = 791.513
End String =

wcstof() 函數的有效浮點值包含可選的 + 或 - 符號,後跟以下集合之一:

  • 對於十進製浮點值
    • 一組十進製數字 (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:wcstof() 如何與 index 和十六進製一起使用?

#include <cwchar>
#include <clocale>
#include <iostream>
using namespace std;

int main()
{
	setlocale(LC_ALL, "en_US.UTF-8");

	wchar_t str1[] = L"39.119e+2xx\u0a93";
	wchar_t str2[] = L"0Xf1.23c\u00d8a1";
	wchar_t *end;
	float value;

	value = wcstof(str1,&end);
	wcout << L"Wide String = " << str1 << endl;
	wcout << L"Float value = " << value << endl;
	wcout << L"End String = " << end << endl;
	
	value = wcstof(str2,&end);
	wcout << L"Wide String = " << str2 << endl;
	wcout << L"Float value = " << value << endl;
	wcout << L"End String = " << end << endl;
	
	return 0;
}

運行程序時,輸出將是:

Wide String = 39.119e+2xxઓ
Float value = 3911.9
End String = xxઓ
Wide String = 0Xf1.23cØa1
Float value = 241.14
End String = Øa1

示例 4:INFINITY 和 NaN 的 wcstof 案例

#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;
	float value;
	
	value = wcstof(str1,&end);
	wcout << L"Wide String = " << str1 << endl;
	wcout << L"Float value = " << value << endl;
	wcout << L"End String = " << end << endl;
	
	value = wcstof(str2,&end);
	wcout << L"Wide String = " << str2 << endl;
	wcout << L"Float value = " << value << endl;
	wcout << L"End String = " << end << endl;
	
	return 0;
}

運行程序時,輸出將是:

Wide String = -inFinityxΣy
Float value = -inf
End String = xΣy
Wide String = NaN11Щ
Float value = nan
End String = 11Щ

通常,wcstof() 函數的有效浮點參數具有以下形式:

[whitespace] [- | +] [digits] [.digits] [ {e | E }[- | +]digits]

wcstof() 函數會忽略所有前導空白字符,直到找到主要的非空白字符。

然後,從這個字符開始,盡可能多的字符組成一個有效的浮點表示並將它們轉換為浮點值。最後一個有效字符之後字符串的剩餘部分存儲在str_end 指向的對象中。

示例 5:wcstof() 帶有前導空格的函數

#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;
	float value;
	
	value = wcstof(str,&end);
	wcout << L"Wide String = " << str << endl;
	wcout << L"Float value = " << value << endl;
	wcout << L"End String = " << end << endl;
	
	return 0;
}

運行程序時,輸出將是:

Wide String = 21.69Ӹaa
Float value = 21.69
End String = Ӹaa

相關用法


注:本文由純淨天空篩選整理自 C++ wcstof()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。