Hàm atof convert string thành số thập phân

double atof(const char *str);

Parameter:

str: chuỗi kí tự cần convert

Remark:

Hàm atof( ) convert chuỗi kí tự (kiểu char*) thành kiểu double.

  • Hàm atof( ) trả về 0.0 nếu convert không thành công
  • Trong trường hợp overflow, errno được set hằng số ERANGE

Ví dụ:

#include "stdio.h"
#include "conio.h"
#include "stdlib.h"

void main( void )
{
    char *str = NULL;
    double value = 0;

    // An example of the atof function
    // using leading and training spaces.
    str = " 3336402735171707160320 ";
    value = atof( str );
    printf( "Function: atof( \"%s\" ) = %e\n", str, value );

    // Another example of the atof function
    // using the 'd' exponential formatting keyword.
    str = "3.1412764583d210";
    value = atof( str );
    printf( "Function: atof( \"%s\" ) = %e\n", str, value );

    // An example of the atof function
    // using the 'e' exponential formatting keyword.
    str = " -2309.12E-15";
    value = atof( str );
    printf( "Function: atof( \"%s\" ) = %e\n", str, value );

    getch();
}

Kết quả:

Hàm atof convert string thành dấu phẩy động
Hàm atof convert string thành dấu phẩy động

Be the first to comment

Leave a Reply