📜  C库中的wcstof函数

📅  最后修改于: 2021-05-25 20:35:39             🧑  作者: Mango

wcstof()函数将str指向的宽字符字符串的初始部分转换为浮点值。 str参数指向可以解释为数字浮点值的字符序列。这些函数停止读取无法识别为数字一部分的第一个字符处的字符串,即,如果第一个字符是除数字以外的任何类型的函数,则该函数仅在该处终止。该字符可以是字符串末尾的wchar_t空字符。
在C中的wchar.h库中声明了以’wcs’开头的标准库函数。

句法 :

float wcstof (const wchar_t* str, wchar_t** endptr);
Parameters :
str : C wide string beginning with the 
representation of a floating-point number.
endptr : Reference to an already allocated object 
of type wchar_t*, whose value is set by the function 
to the next character in str after the numerical value.
This parameter can also be a null pointer.

Return value : Floating point value corresponding 
to the contents of str on success. If the converted value 
falls out of range of corresponding return type, 
range error occurs and is returned.

// C code to convert string having
// floating point as its content
// using wcstof function
  
#include 
  
// header file containing wcstof function
#include  
  
int main()
{
    // wide Character array to be parsed
    wchar_t str[] = L"58.152 9.26";
  
    // Wide Character array to be parsed
    wchar_t* pEnd;
  
    // float variable to store floating point values
    float f1, f2;
  
    // Parsing the float values to f1 and f2
    f1 = wcstof(str, &pEnd);
    f2 = wcstof(pEnd, NULL);
    f2 = 2 * f2;
  
    // Printing parsed float values of f1 and f2
    wprintf(L"%.2f\n%.2f\n", f1, f2);
  
    // operation being performed on the values parsed
    wprintf(L"Value of Pie is %.2f .\n", f1 / f2);
  
    return 0;
}

输出:

58.15
18.52
Value of Pie is 3.14 .
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。