atof 函式詳解

2022-06-11 14:54:09 字數 1076 閱讀 2857

atof()函式

atof():double atof(const char *str );

功 能: 把字串轉換成浮點數

str:要轉換的字串。

返回值:每個函式返回 double 值,此值由將輸入字元作為數字解析而生成。 如果該輸入無法轉換為該型別的值,則返回值為 0.0。

函式說明 :atof()會掃瞄引數nptr字串,跳過前面的空格字元,直到遇上數字或正負符號才開始做轉換,而再遇到非數字或字串結束時('\0')才結束轉換,並將結果返回,str字串可包含正負號、小數點或e(e)來表示指數部分。

1  #include 2  #include 3

intmain(){

4char *a = "

-100.23",

5 *b = "

200e-2",

6 *c = "

341"

,7 *d = "

100.34cyuyan",

8 *e = "

cyuyan";

9 printf("

a = %.2f\n

", atof(a));

10 printf("

b = %.2f\n

", atof(b));

11 printf("

c = %.2f\n

", atof(c));

12 printf("

d = %.2f\n

", atof(d));

13 printf("

e = %.2f\n

", atof(e));

14 system("

pause");

15return0;

16執行結果:

a = -100.23

b = 2.00

c = 341.00

d = 100.34

e = 0.00

atoi函式和atof函式實現

本文對於最基本的功能,實現atoi和atof函式,對於含有e的次冪的字串,只需要新增一部分單獨處理即可。atoi函式 include include include stdlib.h using namespace std int myatoi char s else if s while s 0 ...

c 語言實現 atof 函式

2.實現 atof 函式功能 c 庫函式 double atof const char str 屬於庫函式stdlib.h,把引數 str 所指向的字串轉換為乙個浮點數。具體操作為 跳過字串開始的空白符,識別數字,點好,或正負符號,開始掃瞄合法浮點表達形式,直到遇到非合法字元結束掃瞄。如果空白符後是...

從0開始《十》 atof 函式的簡單實現

程式一 atof函式的簡單實現 解法思路很簡單,就不多說 double atof char s return sign val power 程式二 對atof函式進行擴充,使它可以處理形如123.45e 6的科學表示法,其中,浮點數後面可能會緊跟乙個 e或e一級乙個指數 可能有正負號 解法一 順著上...