📅  最后修改于: 2020-09-25 10:05:37             🧑  作者: Mango
此函数还会设置一个指针,以指向宽字符串的最后一个有效字符 (如果有)之后的第一个宽字符 ,否则,该指针将设置为null。
它在
float wcstof( const wchar_t* str, wchar_t** str_end );
wcstof() 函数将宽字符串和指向宽字符的指针作为其参数,将宽字符串的内容解释为浮点数并返回浮点值。
wcstof() 函数返回:
如果转换后的值超出范围,则发生范围错误,并返回正或负的HUGE_VAL 。
#include
#include
#include
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
#include
#include
#include
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() 函数的有效浮点值包含一个可选的+或-号,后跟以下一组值:
#include
#include
#include
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
#include
#include
#include
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指向的对象中。
#include
#include
#include
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