wcstod() - wcstof() - wcstold() — Convert Wide-Character String to Double, Float, and Long Double

Format

#include <wchar.h>
double wcstod(const wchar_t *nptr, wchar_t **endptr);
float wcstof(const wchar_t *nptr, wchar_t **endptr);
long double wcstold(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(), wcstof(), wcstold() functions convert the initial portion of the wide-character string pointed to by nptr to a double, float or long double value. The nptr parameter points to a sequence of characters that can be interpreted as a numeric binary floating-point value. These functions stop 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(), wcstof(), wcstold() functions expect nptr to point to a string with the following form:
Read syntax diagramSkip visual syntax diagramwhitespace +  – digits.digits.digitseE +  – digits0xXhexdigits.hexdigits.hexdigitspP +  – digits

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

If an exponent is specified with the hexadecimal digit form, the exponent is interpreted as a binary (base 2) exponent. If an exponent is specified with the decimal digit form, the exponent is interpreted as a decimal (base 10) exponent.

Return Value

The wcstod(), wcstof(), wcstold() functions return the converted double, float or long double value. If no conversion could be performed, these functions return 0. If the correct value is outside the range of representable values, these functions return +HUGE_VAL or -HUGE_VAL (according to the sign of the value), and set errno to ERANGE. If the correct value would cause underflow, these functions return 0 and set 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(), wcstof(), wcstold() functions do 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

This example uses the wcstod(), wcstof(), wcstold() functions to convert the string wcs to double, float and long double values.
#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);

   printf("   wcstof = %f\n", wcstof(wcs, &stopwcs));
   printf("   Stop scanning at \"%ls\"\n", stopwcs);
   printf("   wcstold = %lf\n", wcstold(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"
         wcstof = 3.141593                  
         Stop scanning at "This stopped it" 
         wcstold = 3.141593                 
         Stop scanning at "This stopped it" 
   **************************************************/
}

Related Information