wcstod() — Convert Wide-Character String to Double

Format

#include <wchar.h>
double wcstod(const wchar_t *nptr, wchar_t **endptr);

Language Level: XPG4

Threadsafe: Yes.

Locale Sensitive: The behavior of this function might be affected by the LC_CTYPE and LC_NUMERIC categories of the current locale if LOCALETYPE(*LOCALE) is specified on the compilation command. The behavior of this function might also be affected by the LC_UNI_CTYPE and LC_UNI_NUMERIC categories of the current locale if LOCALETYPE(*LOCALEUCS2) or LOCALETYPE(*LOCALEUTF) is specified on the compilation command. This function is not available when LOCALETYPE(*CLD) is specified on the compilation command. For more information, see Understanding CCSIDs and Locales.

Wide Character Function: See Wide Characters for more information.

Description

The wcstod() function converts the initial portion of the wide-character string pointed to by nptr to a double value. The nptr parameter points to a sequence of characters that can be interpreted as a numeric binary floating-point value. The wcstod() function stops reading the string at the first character that it cannot recognize as part of a number. This character can be the wchar_t null character at the end of the string.

The wcstod() function expects nptr to point to a string with the following form:

Read syntax diagramSkip visual syntax diagram>>-+------------+--+-----+--+-digits--+---+--+--------+-+------->
   '-whitespace-'  +- + -+  |         '-.-'  '-digits-' |
                   '- – -'  '-.--digits-----------------'
 
>--+------------------------+----------------------------------><
   '-+-e-+--+-----+--digits-'
     '-E-'  +- + -+
            '- – -'
 

The first character that does not fit this form stops the scan. In addition, a sequence of INFINITY or NAN (ignoring case) is allowed.

Return Value

The wcstod() function returns the converted double value. If no conversion could be performed, the wcstod() function returns 0. If the correct value is outside the range of representable values, the wcstod() function returns +HUGE_VAL or -HUGE_VAL (according to the sign of the value), and sets errno to ERANGE. If the correct value would cause underflow, the wcstod() function returns 0 and sets errno to ERANGE. If the string nptr points to is empty or does not have the expected form, no conversion is performed, and the value of nptr is stored in the object pointed to by endptr, provided that endptr is not a null pointer.

The wcstod() function does not fail if a character other than a digit follows an E or e that is read as an exponent. For example, 100elf is converted to the floating-point value 100.0.

The value of errno may be set to ERANGE, range error.

A character sequence of INFINITY (ignoring case) yields a value of INFINITY. A character value of NAN yields a Quiet Not-A-Number (NAN) value.

Example that uses wcstod()

This example uses the wcstod() function to convert the string wcs to a binary floating-point value.

#include <stdio.h>
#include <wchar.h>
 
int main(void)
{
   wchar_t *wcs = L"3.1415926This stopped it";
   wchar_t *stopwcs;
 
   printf("wcs = \"%ls\"\n", wcs);
   printf("   wcstod = %f\n", wcstod(wcs, &stopwcs));
   printf("   Stop scanning at \"%ls\"\n", stopwcs);
   return 0;
 
   /**************************************************
      The output should be similar to:
 
      wcs = "3.1415926This stopped it"
         wcstod = 3.141593
         Stop scanning at "This stopped it"
   **************************************************/
}

Related Information



[ Top of Page | Previous Page | Next Page | Contents | Index ]