C/C++ 中的 strtod() 函式
函式 strtod() 用於將字串轉換成浮點數。字串將轉換成 double 型別的數字。如果成功,它將返回轉換後的數字;否則返回零。這在“stdlib.h”標頭檔案中宣告。
以下是 strtod() 在 C 語言中的語法:
double strtod(const char *string, char **endpointer);
此處:
字串 − 要轉換的字串。
endpointer − 已分配物件的指標,其值在函式執行後,被設定為數字值之後的下一個字元。
以下是 strtod() 在 C 語言中的一個示例:
示例
#include <stdio.h>
#include <stdlib.h>
int main () {
char s[20] = "8.28 is a number";
char *p;
double result;
result = strtod(s, &p);
printf("The number after conversion of string : %lf", result);
return(0);
}輸出
The number after conversion of string : 8.280000
在上面的程式中,聲明瞭一個 char 型別的陣列 s[20],它已初始化為字母數字字元。使用函式 strtod() 將該字串轉換成一個 double 型別的數字。
char s[20] = "8.28 is a number"; char *p; double result; result = strtod(s, &p);
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP