atof
來自 cppreference.com
在標頭檔案 <stdlib.h> 中定義 |
||
double atof( const char* str ); |
||
解釋指向 str 的位元組字串中的浮點值。
函式會丟棄任何空白字元(由 isspace 確定),直到找到第一個非空白字元。然後,它會盡可能多地獲取字元以形成有效的浮點表示,並將其轉換為浮點值。有效的浮點值可以是以下之一:
- 十進位制浮點表示式。它由以下部分組成:
- (可選) 加號或減號
- 非空十進位制數字序列,可選地包含小數點字元(由當前的 C locale 確定)(定義有效數字)
- (可選)
e
或E
,後跟可選的減號或加號以及非空十進位制數字序列(定義以 10 為底的指數)
|
(C99 起) |
- 當前安裝的 C locale 可能接受的任何其他表示式。
目錄 |
[編輯] 引數
str | - | 指向要解釋的空終止位元組字串的指標 |
[編輯] 返回值
成功時,返回對應於 str 內容的 double 值。如果轉換後的值超出返回型別的範圍,則返回值為未定義。如果無法執行轉換,則返回 0.0。
[編輯] 注意
該名稱代表“ASCII 到浮點數”。
[編輯] 示例
執行此程式碼
#include <stdio.h> #include <stdlib.h> int main(void) { printf("%g\n", atof(" -0.0000000123junk")); printf("%g\n", atof("0.012")); printf("%g\n", atof("15e16")); printf("%g\n", atof("-0x1afp-2")); printf("%g\n", atof("inF")); printf("%g\n", atof("Nan")); printf("%g\n", atof("1.0e+309")); // UB: out of range of double printf("%g\n", atof("0.0")); printf("%g\n", atof("junk")); // no conversion can be performed }
可能的輸出
-1.23e-08 0.012 1.5e+17 -107.75 inf nan inf 0 0
[編輯] 參考資料
- C23 標準 (ISO/IEC 9899:2024)
- 7.22.1.1 atof 函式 (p: TBD)
- C17 標準 (ISO/IEC 9899:2018)
- 7.22.1.1 atof 函式 (p: TBD)
- C11 標準 (ISO/IEC 9899:2011)
- 7.22.1.1 atof 函式 (p: 341)
- C99 標準 (ISO/IEC 9899:1999)
- 7.20.1.1 atof 函式 (p: 307)
- C89/C90 標準 (ISO/IEC 9899:1990)
- 4.10.1.1 atof 函式
[編輯] 另請參閱
(C99)(C99) |
將位元組字串轉換為浮點值 (函式) |
C++ 文件 for atof
|