当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


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()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。