atof, _atof_l, _wtof, _wtof_l

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

構文

double atof(
   const char *str
);
double _atof_l(
   const char *str,
   _locale_t locale
);
double _wtof(
   const wchar_t *str
);
double _wtof_l(
   const wchar_t *str,
   _locale_t locale
);

パラメーター

str
変換対象の文字列。

locale
使用するロケール。

戻り値

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

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

解説

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

入力文字列は、指定された型の数値として解釈できる文字シーケンスです。 この関数は、数値の一部として認識できない最初の文字で入力文字列の読み取りを停止します。 この文字は、文字列を終了する null 文字 ('\0' または L'\0') である場合があります。

atof および _wtofstr 引数には、次の形式があります。

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

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

これらの関数の UCRT バージョンでは、Fortran スタイル (d または D) 指数の文字の変換をサポートしていません。 この非標準の拡張機能は、CRT の以前のバージョンでサポートされており、コードの互換性に影響する変更点がある可能性があります。

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

既定では、この関数のグローバル状態の適用対象は、アプリケーションになります。 この動作を変更するには、「CRT のグローバル状態」を参照してください

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

TCHAR.H ルーチン _UNICODE_MBCS が定義されていない _MBCS が定義されている _UNICODE が定義されている
_tstof atof atof _wtof
_ttof atof atof _wtof

必要条件

ルーチン 必須ヘッダー
atof, _atof_l C: <math.h> または <stdlib.h> C++: <cstdlib><stdlib.h><cmath>、または <math.h>
_wtof, _wtof_l C: <stdlib.h> または <wchar.h> C++: <cstdlib><stdlib.h>、または <wchar.h>

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

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

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

int main(void)
{
    char    *str = NULL;
    double value = 0;
    _locale_t fr = _create_locale(LC_NUMERIC, "fr-FR");

    // 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 'E' exponential formatting keyword.
    str = "3.1412764583E210";
    value = atof(str);
    printf("Function: atof(\"%s\") = %e\n", str, value);

    // An example of the atof and _atof_l functions
    // using the 'e' exponential formatting keyword
    // and showing different decimal point interpretations.
    str = "  -2,309e-25";
    value = atof(str);
    printf("Function: atof(\"%s\") = %e\n", str, value);
    value = _atof_l(str, fr);
    printf("Function: _atof_l(\"%s\", fr)) = %e\n", str, value);
}
Function: atof("  3336402735171707160320 ") = 3.336403e+21
Function: atof("3.1412764583E210") = 3.141276e+210
Function: atof("  -2,309e-25") = -2.000000e+00
Function: _atof_l("  -2,309e-25", fr)) = -2.309000e-25

関連項目

データ変換
数学と浮動小数点のサポート
ロケール
_ecvt
_fcvt
_gcvt
setlocale, _wsetlocale
_atodbl, _atodbl_l, _atoldbl, _atoldbl_l, _atoflt, _atoflt_l