strtod32() — strtod64() — strtod128() — Convert Character String to Decimal Floating-Point

Format

#include <stdlib.h>
_Decimal32 strtod32(const char *nptr, char **endptr);
_Decimal64 strtod64(const char *nptr, char **endptr);
_Decimal128 strtod132(const char *nptr, char **endptr);

Language Level: ANSI

Threadsafe: Yes.

Locale Sensitive: The behavior of these functions might be affected by the LC_CTYPE and LC_NUMERIC categories of the current locale. For more information, see Understanding CCSIDs and Locales.

Description

The strtod32(), strtod64(), and strtod128() functions convert a character string to a single-precision, double-precision, or quad-precision decimal floating-point value. The parameter nptr points to a sequence of characters that can be interpreted as a numeric decimal floating-point value. These functions stop reading the string at the first character that is not recognized as part of a number. This character can be the null character at the end of the string. The endptr parameter is updated to point to this character, provided that endptr is not a NULL pointer.

The strtod32(), strtod64(), and strtod128() functions expect 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 strtod32(), strtod64(), and strtod128() functions return the value of the floating-point number, except when the representation causes an underflow or overflow. For an overflow, strtod32() returns HUGE_VAL_D32 or -HUGE_VAL_D32; strtod64() returns HUGE_VAL_D64 or -HUGE_VAL_D64; strtod128() returns HUGE_VAL_D128 or -HUGE_VAL_D128. For an underflow, all functions return +0.E0.

In both the overflow and underflow cases, errno is set to ERANGE. If the string pointed to by nptr does not have the expected form, a value of +0.E0 is returned and the value of nptr is stored in the object pointed to by endptr, provided that endptr is not a NULL pointer.

The strtod32(), strtod64(), and strtod128() 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.

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.

If necessary, the return value is rounded using the rounding mode Round to Nearest, Ties to Even.

Example that uses strtod32(), strtod64(), and strtod128()

This example converts the strings to single-precision, double-precision, and quad-precision decimal floating-point values. It prints the converted values and the substring that stopped the conversion.

#include <stdlib.h>
#include <stdio.h>
 
int main(void)
{
   	char *string, *stopstring;
   	_Decimal32 d32;
   	_Decimal64 d64;
   	_Decimal128 d128;
 
		string = "3.1415926This stopped it";
 		d32 = strtod32(string, &stopstring); 
  	printf("string = %s\n", string);
  	printf(" strtod32 = %Hf\n", d32);
  	printf(" Stopped scan at %s\n\n", stopstring); 
  	string = "100ergs"; 
  	d32 = strtod32(string, &stopstring); 
  	printf("string = \"%s\"\n", string);
  	printf(" strtof = %Hf\n", d32); 
  	printf(" Stopped scan at \"%s\"\n\n", stopstring); 
  
		string = "3.1415926This stopped it";
  	d64 = strtod64(string, &stopstring); 
  	printf("string = %s\n", string); 
  	printf(" strtod = %Df\n", d64); 
  	printf(" Stopped scan at %s\n\n", stopstring); 
  	string = "100ergs"; 
  	d64 = strtod64(string, &stopstring); 
  	printf("string = \"%s\"\n", string); 
  	printf(" strtod = %Df\n", d64); 
  	printf(" Stopped scan at \"%s\"\n\n", stopstring);

		string = "3.1415926This stopped it"; 
  	d128 = strtod128(string, &stopstring); 
  	printf("string = %s\n", string); 
  	printf(" strtold = %DDf\n", d128); 
  	printf(" Stopped scan at %s\n\n", stopstring); 
  	string = "100ergs"; 
  	d128 = strtod128(string, &stopstring); 
  	printf("string = \"%s\"\n", string); 
  	printf(" strtold = %DDf\n", d128); 
  	printf(" Stopped scan at \"%s\"\n\n", stopstring);
}

/***************** Output should be similar to: ***************** 

string = 3.1415926This stopped it 
 strtof = 3.141593 
 Stopped scan at This stopped it 

string = "100ergs" 
 strtof = 100.000000 
 Stopped scan at "ergs"

string = 3.1415926This stopped it 
 strtod= 3.141593 
 Stopped scan at This stopped it 

string = "100ergs" 
 strtod = 100.000000 
 Stopped scan at "ergs"
string = 3.1415926This stopped it 
 strtold = 3.141593 
 Stopped scan at This stopped it 

string = "100ergs" 
 strtold = 100.000000 
 Stopped scan at "ergs"

*/

Related Information



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