C++ wcstof()

The wcstof() function in C++ interprets the contents of a wide string as a floating point number and return its value as a float.

This function also sets a pointer to point to the first wide character after the last valid character of the wide string if there is any, otherwise the pointer is set to null.

It is defined in <cwchar> header file.

wcstof() prototype

float wcstof( const wchar_t* str, wchar_t** str_end );

The wcstof() function takes wide string and a pointer to wide character as its parameter, interprets the content of the wide string as a floating point number and returns a float value.


wcstof() Parameters

  • str: A wide string having the representation of a floating point number.
  • str_end: Pointer to a pointer to a wide character. The value of str_end is set by the function to the next character in str after the last valid character. This parameter can also be a null pointer.

wcstof() Return value

The wcstof() function returns:

  • a float value (which is converted from the wide string).
  • 0.0 if no valid conversion could be performed.

If the converted value is out of the range, a range error occurs and a positive or negative HUGE_VAL is returned.


Example 1: How wcstof() function works?

#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;
}

When you run the program, the output will be:

Wide String = 40.001∏c12
Float value = 40.001
End String = ∏c12

Example 2: wcstof() function without trailing characters

#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;
}

When you run the program, the output will be:

Wide String = 791.513
Float value = 791.513
End String =

A valid floating-point value for wcstof() function consists of an optional + or - sign followed by one of the following sets:

  • For decimal floating-point value:
    • A group of decimal digits (0-9), optionally containing a decimal point (.). For example: 13.170, -5.63, etc .
    • An optional exponent part (e or E) followed by an optional + or - sign and non-empty sequence of decimal digits. For example: 3.46101e+007, 13.19e-013, etc.
  • For hexadecimal floating-point value:
    • A string starting with 0x or 0X, followed by a non-empty sequence of hexadecimal digits, optionally containing a decimal point (.). For Example: 0xfa5, -0xb1f.24, etc.
    • An optional exponent part (p or P) followed by an optional + or - sign and non-empty sequence of hexadecimal digits. For Example: 0x51c.23p5, -0x2a.3p-3, etc.
  • Infinity:
    • INF or INFINITY (ignoring case). For Example: -Inf, InfiNiTy, etc.
  • NaN (Not a Number):
    • NAN or NANsequence (ignoring case) where sequence is a sequence of characters consisting only of alphanumeric characters or the underscore (_).The result is a quiet NaN. For Example: Nan, NaNab1, etc.

Example 3: How wcstof() works with exponents and hexadecimals?

#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;
}

When you run the program, the output will be:

Wide String = 39.119e+2xxઓ
Float value = 3911.9
End String = xxઓ
Wide String = 0Xf1.23cØa1
Float value = 241.14
End String = Øa1

Example 4: wcstof cases for INFINITY and NaN

#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;
}

When you run the program, the output will be:

Wide String = -inFinityxΣy
Float value = -inf
End String = xΣy
Wide String = NaN11Щ
Float value = nan
End String = 11Щ

In general, a valid floating point argument for wcstof() function has the following form:

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

The wcstof() function ignores all the leading whitespace characters until the primary non-whitespace character is found.

Then, beginning from this character, it takes as many characters as possible that forms a valid floating-point representation and converts them to a floating point value. Whatever is left of the string after the last valid character is stored in the object pointed by str_end.


Example 5: wcstof() function with leading whitespace

#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;
}

When you run the program, the output will be:

Wide String = 21.69Ӹaa
Float value = 21.69
End String = Ӹaa
Did you find this article helpful?