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_wtof에 대한 str 인수의 형식은 다음과 같습니다.

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

공백 whitespace 또는 탭 문자로 구성되며 무시 sign 됩니다. 더하기(+) 또는 빼기(-)이며 digits 하나 이상의 소수 자릿수입니다. 소수점 앞에 숫자가 없는 경우 소수점 뒤에는 하나 이상 있어야 합니다. 10진수 뒤에 지수가 올 수 있습니다. 지수는 소개 문자(e 또는 E) 및 선택적으로 부호 있는 10진수 정수로 구성됩니다.

이러한 함수의 UCRT 버전은 포트란 스타일(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

참고 항목

데이터 변환
수학 및 부동 소수점 지원
Locale
_ecvt
_fcvt
_gcvt
setlocale, _wsetlocale
_atodbl, _atodbl_l, _atoldbl, _atoldbl_l, _atoflt, _atoflt_l