atof, _wtof

From RAD Studio
Jump to: navigation, search

Go Up to math.h Index


Header File

stdlib.h, math.h

Category

Conversion Routines, Math Routines

Prototype

double atof(const char *s);
double _wtof(const wchar_t *s);

Description

Converts a string to a floating-point number.

  • atof converts a string pointed to by s to double; this function recognizes the character representation of a floating-point number, made up of the following:
  • An optional string of tabs and spaces
  • An optional sign
  • A string of digits and an optional decimal point (the digits can be on both sides of the decimal point)
  • An optional e or E followed by an optional signed integer

The characters must match this generic format:

[whitespace] [sign] [ddd] [.] [ddd] [e|E[sign]ddd]

atof also recognizes +INF and -INF for plus and minus infinity, and +NAN and -NAN for not-a-number.

In this function, the first unrecognized character ends the conversion.

The functions strtod and _strtold are similar to atof and provide better error detection, and hence are preferred in some applications.

Return Value

Returns the converted value of the input string.

If there is an overflow, atof returns plus or minus HUGE_VAL (or _LHUGE_VAL), errno is set to ERANGE (Result out of range), and _matherr (or _matherrl) is not called.

Example

#include <stdlib.h>
#include <math.h>
double add_inputs(wchar_t* left, wchar_t* right)
{
  /* Transform the input strings into floats */
  double d_left = _wtof(left);
  double d_right = _wtof(right);

  /* Add the transformed floats */
  return d_left + d_right;
}