Share via


_atodbl, _atodbl_l, _atoldbl, _atoldbl_l, _atoflt, _atoflt_l

將字串轉換成雙精度浮點數 (_atodbl)、長雙精度浮點數 (_atoldbl) 或浮點數 (_atoflt)。

語法

int _atodbl( _CRT_DOUBLE * value, char * str );
int _atodbl_l ( _CRT_DOUBLE * value, char * str, _locale_t locale );
int _atoldbl( _LDOUBLE * value, char * str );
int _atoldbl_l ( _LDOUBLE * value, char * str, _locale_t locale );
int _atoflt( _CRT_FLOAT * value, const char * str );
int _atoflt_l( _CRT_FLOAT * value, const char * str, _locale_t locale );

參數

value
將字串轉換成浮點值所產生的雙精度浮點數、長雙精度浮點數或浮點值。 這些值包裝在結構中。

str
要剖析以轉換成浮點值的字串。

locale
要使用的地區設定。

傳回值

如果成功,會傳回 0。 可能的錯誤碼為 _UNDERFLOW_OVERFLOW ,其定義于標頭檔 < math.h > 中。

備註

這些函式會將字串轉換成浮點值。 這些函式與 atof 函式系列之間的差異在於這些函式不會產生浮點程式碼,也不會造成硬體例外狀況。 相反地,錯誤狀況會回報為錯誤碼。

如果字串沒有有效的解譯做為浮點值, value 則會設定為零,且傳回值為零。

具有 _l 尾碼的這些函式版本與沒有尾碼的版本相同,不同之處在于它們會使用 locale 傳入的參數,而不是目前的執行緒地區設定。

根據預設,此函式的全域狀態會限定于應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態。

需求

常式 必要的標頭
_atodbl, _atoldbl, _atoflt

_atodbl_l, _atoldbl_l, _atoflt_l
<stdlib.h>

範例

// crt_atodbl.c
// Uses _atodbl to convert a string to a double precision
// floating point value.

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

int main()
{
   char str1[256] = "3.141592654";
   char abc[256] = "abc";
   char oflow[256] = "1.0E+5000";
   _CRT_DOUBLE dblval;
   _CRT_FLOAT fltval;
   int retval;

   retval = _atodbl(&dblval, str1);

   printf("Double value: %lf\n", dblval.x);
   printf("Return value: %d\n\n", retval);

   retval = _atoflt(&fltval, str1);
   printf("Float value: %f\n", fltval.f);
   printf("Return value: %d\n\n", retval);

   // A non-floating point value: returns 0.
   retval = _atoflt(&fltval, abc);
   printf("Float value: %f\n", fltval.f);
   printf("Return value: %d\n\n", retval);

   // Overflow.
   retval = _atoflt(&fltval, oflow);
   printf("Float value: %f\n", fltval.f);
   printf("Return value: %d\n\n", retval);

   return 0;
}
Double value: 3.141593
Return value: 0

Float value: 3.141593
Return value: 0

Float value: 0.000000
Return value: 0

Float value: inf
Return value: 3

另請參閱

資料轉換
數學和浮點支援
地區設定
atof, _atof_l, _wtof, _wtof_l