mystring.c 添加字符串转浮点函数
This commit is contained in:
@@ -132,7 +132,7 @@ static int str_ainttoi(const char *s)
|
||||
ret*=10;
|
||||
ret+=*s-'0';
|
||||
}
|
||||
else return ret;
|
||||
else return ret*sig;
|
||||
s++;
|
||||
}
|
||||
return ret*sig;
|
||||
@@ -226,6 +226,52 @@ list_def *str_atod_list(const char *s, char c)
|
||||
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
* 计算整数乘方
|
||||
*
|
||||
*/
|
||||
float str_exp(int a,int b)
|
||||
{
|
||||
int n=1;
|
||||
for(int i=0;i<b;i++){
|
||||
n*=a;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
* 把带一个小数点的数字字符串转化为浮点数
|
||||
*
|
||||
*/
|
||||
float str_atof(const char *s)
|
||||
{
|
||||
list_def *nums=str_split(s,'.');
|
||||
int a,b,len;
|
||||
char *sa,*sb;
|
||||
float exp;
|
||||
len=list_length(nums);
|
||||
if(len==2){
|
||||
sa=list_get_str(nums,0);
|
||||
sb=list_get_str(nums,1);
|
||||
a=str_atoi(sa);
|
||||
b=str_atoi(sb);
|
||||
if(a<0) b=-b;
|
||||
return a+(b/str_exp(10,str_len(sb)));
|
||||
}else if(len==1){
|
||||
sa=list_get_str(nums,0);
|
||||
a=str_atoi(sa);
|
||||
return a;
|
||||
}else{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
|
@@ -14,6 +14,7 @@ const char *str_find_char_right_p(const char *s,const char *const p, char c);
|
||||
const char *str_find_char_left(const char *const p,const char *s, char c);
|
||||
int str_ahextoi(const char *s);
|
||||
int str_atoi(const char *s);
|
||||
float str_atof(const char *s);
|
||||
int str_len(const char *s);
|
||||
int str_cpystr(char *s1, const char *s2, char c);
|
||||
list_def *str_split(const char *s,char c);/*temp_ptr*/
|
||||
|
Reference in New Issue
Block a user