INF: Converting a String to a Floating Point

ID Number: Q11558

3.00 4.00 5.00 5.10 6.00 6.00a 6.00ax 7.00 | 5.10 6.00 6.00a

MS-DOS | OS/2

Summary:

In Microsoft C versions 5.0, 5.1, 6.0, 6.0a, 6.0ax, and C/C++ version

7.0, when converting a string into a floating-point number, the sscanf()

function is a good alternative to the atof() function.

More Information:

The atof() function will return a zero if the string cannot be

converted to a number, while sscanf() will return better error

information. This additional information can be used in conjunction

with matherr() to better handle errors.

The atof() function will not call matherr unless an actual math

exception occurred. The atof() function lacks a useful error return.

We suggest one of the following two methods for converting a string

to a floating-point number:

1. Validate the string to be converted prior to calling atof(). This

procedure includes checking the string for non-numeric characters

and making sure that the decimal point and signs are in the

correct places.

2. Use the sscanf() function. It is slower than atof, but it provides

better error return information.

Additional reference words: 5.00 5.10 6.00 6.00a 6.00ax 7.00