将字符串转换为C中的双变量

我编写了以下代码….它应该将像“88”这样的字符串转换为double值88并打印出来

void convertType(char* value) { int i = 0; char ch; double ret = 0; while((ch = value[i] )!= '\0') { ret = ret*10 +(ch - '0'); ++i; } printf("%d",ret);//or %f..what is the control string for double? } //input string :88 

但它总是打印0 …但是当我将ret的类型更改为int …它工作正常…当类型为float或double时,它打印为零…所以为什么我得到这个模棱两可的结果?

使用sscanf (C ++中的头文件stdio.hcstdio ):

 char str[] = "12345.56"; double d; sscanf(str, "%lf", &d); printf("%lf", d); 

但它总是打印0 …但是当我将ret的类型更改为int …它工作正常…当类型为float或double时,它打印为零。

逻辑很好。 只是你的格式说明符是错误的。 把它改成%f ,一切都很好!

你可能能够使用atof()它返回一个double。

资源

如果要将char *解析为double,则应使用函数“atof”。

您还应该使用分隔符“%f”来打印双精度:

可以在此处找到更多信息和使用示例。

使用示例:

 #include  #include  #include  int main() { float val; char str[20]; strcpy(str, "98993489"); val = atof(str); printf("String value = %s, Float value = %f\n", str, val); strcpy(str, "tutorialspoint.com"); val = atof(str); printf("String value = %s, Float value = %f\n", str, val); return(0); } 

要打印它,您必须将其打印为浮动:

 printf("This is the value in float: %f\n", yourFloatValue); 

将字符串转换为C中的双变量

如果溢出不是问题,但代码想要在数字文本后检测额外的非空白文本:

 // return 1 on success int convertType(const char* value, double *destination) { char sentinel; return sscanf(value,"%f %c", destination, &sentinel) == 1; } 

如果sscanf()找不到double ,则sscanf()的返回值将为EOF或0。

如果sscanf()在数字文本后找到非空白文本,它将返回2。

如果只扫描一个double ,没有额外的, sscanf()返回1.前导和尾随空格都可以。

例:

 double x; if (convertType(some_string, &x)) { printf("%.17e\n", x); // or whatever FP format you like } else { puts("Failed"); } 
 #define ZERO 48 #define NINE 57 #define MINUS 45 #define DECPNT 46 long strtolng_n(char* str, int n) { int sign = 1; int place = 1; long ret = 0; int i; for (i = n-1; i >= 0; i--, place *= 10) { int c = str[i]; switch (c) { case MINUS: if (i == 0) sign = -1; else return -1; break; default: if (c >= ZERO && c <= NINE) ret += (c - ZERO) * place; else return -1; } } return sign * ret; } double _double_fraction(char* str, int n) { double place = 0.1; double ret = 0.0; int i; for (i = 0; i < n; i++, place /= 10) { int c = str[i]; ret += (c - ZERO) * place; } return ret; } double strtodbl(char* str) { int n = 0; int sign = 1; int d = -1; long ret = 0; char* temp = str; while (*temp != '\0') { switch (*temp) { case MINUS: if (n == 0) sign = -1; else return -1; break; case DECPNT: if (d == -1) d = n; else return -1; break; default: if (*temp < ZERO && *temp > NINE) return -1; } n++; temp++; } if (d == -1) { return (double)(strtolng_n(str, n)); } else if (d == 0) { return _double_fraction((str+d+1), (nd-1)); } else if (sign == -1 && d == 1) { return (-1)*_double_fraction((str+d+1), (nd-1)); } else if (sign == -1) { ret = strtolng_n(str+1, d-1); return (-1) * (ret + _double_fraction((str+d+1), (nd-1))); } else { ret = strtolng_n(str, d); return ret + _double_fraction((str+d+1), (nd-1)); } } 

以下代码适用于我。

 #include  void convertType(char* value); int main(int argc, char *argv[]) { char *str="0929"; convertType(str); return 0; } void convertType(char* value) { double ret = 0; while(*value != '\0') { ret = ret*10 +(*value - '0'); value++; } fprintf(stdout, "value: %f\n", ret); }