atof、_atof_l、_wtof、_wtof_l

文字列を double 型に変換します。

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

パラメーター

  • str
    変換される文字列。

  • locale
    使用するロケール。

戻り値

各関数は、入力文字列を数値として解釈して作成した double 型の値を返します。 入力値をその型の値に変換できない場合、戻り値は 0.0 です。

すべての-の-範囲外の場合、errno がERANGE。 渡されたパラメーターが NULL の場合、「パラメーターの検証」に説明されているように、無効なパラメーター ハンドラーが呼び出されます。 実行の継続が許可された場合、これらの関数は errno を EINVAL に設定し、0 を返します。

解説

これらの関数は、文字列を倍精度浮動小数点値に変換します。

入力文字列は、指定された型の数値として解釈できる文字の並びにします。 関数は、入力文字列の読み込み時に、数値の一部として認識できない文字が現れた時点で停止します。 この文字は、文字列の終わりを示す NULL ('\0' または L'\0') の場合もあります。

atof 関数と _wtof 関数の str 引数の形式は次のとおりです。

[whitespace] [sign] [digits] [.digits] [ {d | D | e |E }[sign]digits]

whitespace はスペースやタブで構成され、無視されます。sign は正符号 (+) または負符号 (–) のいずれかで、digits は 1 つ以上の 10 進数字です。 小数点の前に数字がない場合は、小数点の後ろに少なくとも 1 つの数字が必要です。 10 進数の後には指数部を指定できます。指数部は、指数部の開始文字 (d、D、e、または E) および必要に応じて符号付き整数で構成されます。

_l サフィックスが付いているこれらの関数の各バージョンは、現在のロケールの代わりに渡されたロケール パラメーターを使用する点を除いて同じです。

汎用テキスト ルーチンのマップ

TCHAR.H のルーチン

_UNICODE および _MBCS が未定義の場合

_MBCS が定義されている場合

_UNICODE が定義されている場合

_tstof

atof

atof

_wtof

_ttof

atof

atof

_wtof

必要条件

ルーチン

必須ヘッダー

atof

<math.h> および <stdlib.h>

_atof_l

<math.h> および <stdlib.h>

_wtof, _wtof_l

<stdlib.h> または <wchar.h>

使用例

このプログラムは、atof 関数を使用して、文字列として格納されている数字を数値に変換する方法を示します。

// crt_atof.c
//
// This program shows how numbers stored as 
// strings can be converted to numeric
// values using the atof function.

#include <stdlib.h>
#include <stdio.h>

int 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 );

}
  

同等の .NET Framework 関数

参照

参照

データ変換

浮動小数点サポート

ロケール

_ecvt

_fcvt

_gcvt

setlocale、_wsetlocale

_atodbl、_atodbl_l、_atoldbl、_atoldbl_l、_atoflt _atoflt_l