atof

 

Description

Converts string to a double.

Syntax

double atof( LPCSTR lpcsz, BOOL bAllowUnits = false )

Parameters

lpcsz
[input] the string to convert
bAllowUnits
[input] true will allow k, m, u etc engineering units, false will treat them as non-numeric and return NANUM

Return

a double value. If conversion can not be made,NANUM is returned.

Examples

EX1

// This is a self contained sample program for the function atof, 
// To run the program, enter the following command in the Script window:
//   atof_ex1
// When it runs, the following meassage will be printed:
//   Converted "-123.456" to -123.456000
//   Converted "1.23e5" to 123000.000000
//   Converted "abc" to 0.000000
void    atof_ex1()
{
    double    rr;
    
    char    sz1[] = "-123.456";
    rr = atof(sz1);
    printf("Converted \"%s\" to %f\n", sz1,rr);
    
    char    sz2[] = "1.23e5";
    rr = atof(sz2);
    printf("Converted \"%s\" to %f\n", sz2,rr);
    
    char    sz3[] = "abc";
    rr = atof(sz3);
    printf("Converted \"%s\" to %f\n", sz3,rr);
    
}

EX2

// try the followings
// cs2num 123
// cs2num 23.4
// cs2num 1.4e5
// cs2num -23.4E-05
// cs2num 1k
// cs2num 2.3M
// cs2num 45K
// cs2num 56m
void cs2num(string str)
{
    double a = atof(str, true);
    if(a == NANUM)
        printf("%s is not a numeric string\n", str);
    else
        printf("%s is numeric, value = %g\n", str, a);
}

Remark

Converts string to a double.

See Also

ftoa

header to Include

origin.h

Reference