📜  C++ wcstold()

📅  最后修改于: 2020-09-25 10:08:35             🧑  作者: Mango

C++中的wcstold() 函数将宽字符串的内容解释为浮点数,并将其值返回为长双精度数。

该wcstold() 函数还设置一个指针指向第一个宽字符 ,如果有任何的宽字符串的最后一个有效字符之后,否则指针设置为null。

它在头文件中定义。

wcstold()原型

long double wcstold( const wchar_t* str, wchar_t** str_end );

wcstold() 函数将宽字符串和指向宽字符的指针作为其参数,将宽字符串的内容解释为浮点数,并返回一个长双精度值。

wcstold()参数

wcstold()返回值

wcstold() 函数返回:

如果转换后的值超出范围,则发生范围错误,并返回正或负的HUGE_VAL

示例1:wcstold() 函数如何工作?

#include 
#include 
#include 
using namespace std;

int main()
{
    setlocale(LC_ALL, "en_US.UTF-8");
    
    wchar_t str[] = L"93.410\u03b7\u05ea";
    wchar_t *end;
    long double value;
    
    value = wcstold(str,&end);
    wcout << L"Wide String = " << str << endl;
    wcout << L"Long Double value = " << value << endl;
    wcout << L"End String = " << end << endl;
    
    return 0;
}

运行该程序时,输出为:

Wide String = 93.410ηת
Long Double value = 93.41
End String = ηת

示例2:wcstold() 函数不带结尾字符

#include 
#include 
#include 
using namespace std;

int main()
{
    setlocale(LC_ALL, "en_US.UTF-8");
    
    wchar_t str[] = L"93.410";
    wchar_t *end;
    long double value;
    
    value = wcstold(str,&end);
    wcout << L"Wide String = " << str << endl;
    wcout << L"Long Double value = " << value << endl;
    wcout << L"End String = " << end << endl;
    
    return 0;
}

运行该程序时,输出为:

Wide String = 93.410
Long Double value = 93.41
End String =

wcstold() 函数的有效浮点值包含一个可选的+或-号,后跟以下一组值:

示例3:wcstold()如何使用指数和十六进制?

#include 
#include 
#include 
using namespace std;

int main()
{
    setlocale(LC_ALL, "en_US.UTF-8");
    
    wchar_t str1[] = L"34.901\u00c6\u00f1";
    wchar_t str2[] = L"0Xfc1.a12\u03c7r12";
    wchar_t *end;
    long double value;
    
    value = wcstold(str1,&end);
    wcout << L"Wide String = " << str1 << endl;
    wcout << L"Long Double value = " << value << endl;
    wcout << L"End String = " << end << endl;
    
    value = wcstold(str2,&end);
    wcout << L"Wide String = " << str2 << endl;
    wcout << L"Long Double value = " << value << endl;
    wcout << L"End String = " << end << endl;
    
    return 0;
}

运行该程序时,输出为:

Wide String = 34.901Æñ
Long Double value = 34.901
End String = Æñ
Wide String = 0Xfc1.a12χr12
Long Double value = 4033.63
End String = χr12

示例4:INFINITY和NaN的wcstold案例

#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"NaN22\u0429";
    wchar_t *end;
    long double value;
    
    value = wcstold(str1,&end);
    wcout << L"Wide String = " << str1 << endl;
    wcout << L"Long Double value = " << value << endl;
    wcout << L"End String = " << end << endl;
    
    value = wcstold(str2,&end);
    wcout << L"Wide String = " << str2 << endl;
    wcout << L"Long Double value = " << value << endl;
    wcout << L"End String = " << end << endl;
    
    return 0;
}

运行该程序时,输出为:

Wide String = inFinityxΣy
Long Double value = inf
End String = xΣy
Wide String = NaN22Щ
Long Double value = nan
End String = 22Щ

通常,wcstold() 函数的有效浮点参数具有以下形式:

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

直到主非空白字符被找到wcstold() 函数将忽略所有的前导空白字符 。

然后,从该字符开始,它需要尽可能多的字符 ,以形成有效的浮点表示形式并将其转换为浮点值。最后一个有效字符之后, 字符串剩余的内容将存储在str_end指向的对象中。

示例5:wcstold() 函数 ,前导空格

#include 
#include 
#include 
using namespace std;

int main()
{
    setlocale(LC_ALL, "en_US.UTF-8");
    
    wchar_t str[] = L" 59.013\u0915\u0975";
    wchar_t *end;
    long double value;
    
    value = wcstold(str,&end);
    wcout << L"Wide String = " << str << endl;
    wcout << L"Long Double value = " << value << endl;
    wcout << L"End String = " << end << endl;
    
    return 0;
}

运行该程序时,输出为:

Wide String = 59.013कॵ
Long Double value = 59.013
End String = कॵ