C 教程
C 標(biāo)準(zhǔn)庫(kù) - <stdlib.h>
C 庫(kù)函數(shù) double strtod(const char *str, char **endptr) 把參數(shù) str 所指向的字符串轉(zhuǎn)換為一個(gè)浮點(diǎn)數(shù)(類型為 double 型)。如果 endptr 不為空,則指向轉(zhuǎn)換中最后一個(gè)字符后的字符的指針會(huì)存儲(chǔ)在 endptr 引用的位置。
下面是 strtod() 函數(shù)的聲明。
double strtod(const char *str, char **endptr)
該函數(shù)返回轉(zhuǎn)換后的雙精度浮點(diǎn)數(shù),如果沒有執(zhí)行有效的轉(zhuǎn)換,則返回零(0.0)。
下面的實(shí)例演示了 strtod() 函數(shù)的用法。
#include <stdio.h> #include <stdlib.h> int main() { char str[30] = "20.30300 This is test"; char *ptr; double ret; ret = strtod(str, &ptr); printf("數(shù)字(double)是 %lfn", ret); printf("字符串部分是 |%s|", ptr); return(0); }
讓我們編譯并運(yùn)行上面的程序,這將產(chǎn)生以下結(jié)果:
數(shù)字(double)是 20.303000 字符串部分是 | This is test|其他擴(kuò)展