wcstod() — Convert wide-character string to a double floating-point

Standards

Standards / Extensions C or C++ Dependencies

ISO C Amendment
XPG4
XPG4.2
C99
Single UNIX Specification, Version 3

both  

Format

#include <wchar.h>

double wcstod(const wchar_t * __restrict__nptr, wchar_t ** __restrict__endptr);

General description

The wcstod() function converts a wchar_t * type floating-point number input string to a double value.

See the “fscanf Family of Formatted Input Functions” on fscanf(), scanf(), sscanf() — Read and format data for a description of special infinity and NaN sequences recognized by z/OS® formatted input functions, including wcstod() in IEEE Binary Floating-Point mode.

Converts the initial portion of the wide-character string pointed to by nptr to double representation. First it decomposes the input string into three parts:
  1. An initial, possibly empty, sequence of white space characters (as specified by the iswspace() function)
  2. A subject sequence interpreted as a floating-point constant or representing infinity or a NAN.
  3. A final string of one or more unrecognized characters, including the terminating NULL character of the input string.

Then it attempts to convert the subject sequence to a floating-point number, and returns the result.

The expected form of the subject sequence is an optional plus or minus sign, then one of the following:

  • A non-empty sequence of decimal digits optionally containing a radix character, then an optional exponent part. Where radix character is the character that separates the integer part of a number from the fractional part.
  • A 0x or 0X, then a non-empty sequence of hexadecimal digits optionally containing a radix character, then an optional binary exponent part. Where radix character is the character that separates the integer part of a number from the fractional part.
  • One of INF or INFINITY, ignoring case.
  • One of NANQ or NANQ(n-char-sequence), ignoring case.
  • One of NANS or NANS(n-char-sequence), ignoring case.
  • One of NAN or NAN(n-char-sequence), ignoring case.

The subject sequence is defined as the longest initial subsequence of the input wide-character string, starting with the first non-white space wide character, that is of the expected form. The subject sequence contains no wide characters if the input wide-character string is empty or consists entirely of white space wide characters, or if the first non-white space wide character is other than a sign, a digit, or a decimal-point wide character.

If the subject sequence has the expected form, the sequence of wide characters starting with the first digit or the decimal-point wide character (whichever occurs first) is interpreted as a floating constant according to the rules of ISO/IEC 9899: subclause 6.1.3.1, except the decimal-point wide character is used in place of a period, and if neither an exponent part nor a decimal-point wide character appears, a decimal-point is assumed to follow the last digit in the wide-character string. If the subject sequence begins with a minus sign, the value resulting from the conversion is negated. A pointer to the final wide-character string is stored in the object pointed to by endptr, provided that endptr is not a NULL pointer.

In a locale other than the C locale, additional implementation-defined subject sequence forms may be accepted.

If the subject sequence is empty or does not have the expected form, no conversion is performed; the value of nptr is stored in the object pointed to by endptr, provided that endptr is not a NULL pointer.

The behavior of this wide-character function is affected by the LC_CTYPE category of the current locale. If you change the category, undefined results can occur.

Returned value

If successful, wcstod() returns the converted value, if any.

If no conversion could be performed, wcstod() returns 0.

The double value is hexadecimal floating-point or IEEE Binary Floating-Point format depending on the floating-point mode of the thread invoking wcstod(). The wcstod() function uses __isBFP() to determine the floating-point format (hexadecimal floating-point or IEEE Binary Floating-Point) of the invoking thread.

If the correct value is outside the range of representable values, wcstod() returns ±HUGE_VAL—according to the sign of the value—and the value of the macro ERANGE is stored in errno.

Example

CELEBW21
⁄* CELEBW21 *⁄                                   
#include <stdio.h>                                                              
#include <wchar.h>                                                              
                                                                                
int main(void)                                                                  
{                                                                               
   wchar_t *wcs;                                                                
   wchar_t *stopwcs;                                                            
   double   d;                                                                  
                                                                                
   wcs = L"3.1415926This stopped it";                                           
   d = wcstod(wcs, &stopwcs);                                                   
   printf("wcs = `%ls`\n", wcs);                                                
   printf("   wcstod = %f\n", d);                                               
   printf("   Stopped scan at `%ls`\n", stopwcs);                               
}                                                                               

Related information