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