将字符串转换成翻番 [英] Converting a string to double
问题描述
我有以下的code不正确运行。
I have the following code which doesn't run properly.
char dec_number[300];
dec_number[0]='\0';
//some code that reads a decimal number and stores it in dec_number
//I get in dec_number 0.19
当我打印的价值,我得到 0.19
。
When I printed the value, I get 0.19
.
之后,我想尝试繁殖,所以我需要将其存储在双。
After that I want to multiply it with something so I need to store it in double.
我把它转换使用双K = atod(dec_number)增加一倍;
和 K =的strtod(dec_number,NULL);
。但我得到 9716
或一些大的,但没有接近 0.19
。
I convert it to double using double k=atod(dec_number);
and k=strtod(dec_number, NULL);
. But I get 9716
or something large but nothing near 0.19
.
我做了什么错?有什么建议么?谢谢你。
What have I done wrong? Any suggestions? Thank you.
推荐答案
您错误地将其转换,显然。没有人在这里有心灵感应能力(我presume),所以没有办法说你在做什么错了,直到你告诉我们,你的code,你的字符串转换为双击
。
You are converting it incorrectly, apparently. Nobody here is has telepathic abilities (I presume), so there's no way to say what you are doing wrong until you show us your code where you convert your string to double
.
总之,一个字符串转换双
的正确方法是使用的strtod
功能。
Anyway, the proper way to convert a string to double
is to use strtod
function.
char *end;
double d = strtod(dec_number, &end);
/* Perform error handling be examining `errno` and, if necessary, `end` */
您使用的strtod
?
这篇关于将字符串转换成翻番的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!