将字符串转换在C双变量 [英] converting string to a double variable in C
本文介绍了将字符串转换在C双变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写了下面的code ....应该转换为字符串,如88为双精度值88和打印
I have written the following code....It should convert a string like "88" to double value 88 and print it
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,它打印零...所以为什么我收到这个暧昧结果如何?
But it always prints 0...But when i change type of ret to int ...it works fine...when the type is float or double,it prints zero...so why am i getting this ambiguous results?
推荐答案
使用的sscanf
:
char str[] = "12345.56";
double d;
sscanf(str, "%lf", &d);
printf("%lf", d);
这篇关于将字符串转换在C双变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文