【轉貼:式語言教學誌】
stdlib.h 的函數 strtod() 接受字串當作參數,
將字串中的數字轉換為 double 型態的浮點數,
其餘非數字部份以另一指標儲存位址。
/* 《程式語言:教學誌》的範例程式
http://pydoing.blogspot.com/]
功能:示範 stdlib.h 中函數 strtod() 的使用
作者:張凱慶
時間:西元2010年6月 */
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char *test = "the answer is 33.23mm + 25mm";
char *endPtr = test;
double sum = 0.0;
while (*test) {
sum += strtod(test, &endPtr);
test = endPtr;
while (!isdigit(*test) && *test) {
test++;
}
}
printf("%.2f\n", sum);
return 0;
}
結果:
58.23
類似:
strtoul
ref:
程式語言教學誌
沒有留言:
張貼留言