[英]Converting char* to float or double
I have a value I read in from a file and is stored as a char*. 我有一个从文件读入的值,并存储为char *。 The value is a monetary number, #.##, ##.##, or ###.##. 值是货币编号,#。##,##。##或###。##。 I want to convert the char* to a number I can use in calculations, I've tried atof and strtod and they just give me garbage numbers. 我想将char *转换为我可以在计算中使用的数字,我尝试了atof和strtod,他们只是给我垃圾数字。 What is the correct way to do this, and why is the way I am doing it wrong? 这样做的正确方法是什么,为什么我做错的方式呢?
This is essentially what I am doing, just the char* value is read in from a file. 这基本上就是我正在做的事情,只是从文件读入char *值。 When I print out the temp and ftemp variables they are just garbage, gigantic negative numbers. 当我打印出temp和ftemp变量时,它们只是垃圾,巨大的负数。
Another Edit: 另一个编辑:
I am running exactly this in gcc 我在gcc中正是这样运行的
#include <stdio.h>
int main()
{
char *test = "12.11";
double temp = strtod(test,NULL);
float ftemp = atof(test);
printf("price: %f, %f",temp,ftemp);
return 0;
} }
and my output is price: 3344336.000000, 3344336.000000 我的输出是价格:3344336.000000,33444336.000000
Edit: Here is my code 编辑:这是我的代码
if(file != NULL)
{
char curLine [128];
while(fgets(curLine, sizeof curLine, file) != NULL)
{
tempVal = strtok(curLine,"|");
pairs[i].name= strdup(tempVal);
tempVal = strtok(NULL,"|");
pairs[i].value= strdup(tempVal);
++i;
}
fclose(file);
}
double temp = strtod(pairs[0].value,NULL);
float ftemp = atof(pairs[0].value);
printf("price: %d, %f",temp,ftemp);
my input file is very simple name, value pairs like this: 我的输入文件是非常简单的名称,值对如下:
NAME|VALUE
NAME|VALUE
NAME|VALUE
with the value being dollar amounts 价值是美元金额
SOLVED: Thank you all, I was using %d instead of %f and didn't have the right headers included. 解决:谢谢大家,我使用的是%d而不是%f,并且没有包含正确的标题。
You are missing an include : #include <stdlib.h>
, so GCC creates an implicit declaration of atof
and atod
, leading to garbage values. 你缺少一个include: #include <stdlib.h>
,所以GCC创建一个atof
和atod
的隐式声明,导致垃圾值。
And the format specifier for double is %f
, not %d
(that is for integers). double的格式说明符是%f
,而不是%d
(即整数)。
#include <stdlib.h>
#include <stdio.h>
int main()
{
char *test = "12.11";
double temp = strtod(test,NULL);
float ftemp = atof(test);
printf("price: %f, %f",temp,ftemp);
return 0;
}
/* Output */
price: 12.110000, 12.110000
Code posted by you is correct and should have worked. 您发布的代码是正确的,应该有效。 But check exactly what you have in the char*
. 但要确切查看char*
。 If the correct value is to big to be represented, functions will return a positive or negative HUGE_VAL
. 如果要表示的值正确,则函数将返回正或负HUGE_VAL
。 Check what you have in the char*
against maximum values that float
and double
can represent on your computer. 检查char*
中的内容,以及float
和double
在计算机上可以表示的最大值。
Check this page for strtod
reference and this page for atof
reference . 检查此页strtod
参考和此页面atof
参考 。
I have tried the example you provided in both Windows and Linux and it worked fine. 我已经尝试过您在Windows和Linux中提供的示例,但它运行良好。
printf("price: %d, %f",temp,ftemp);
^^^
This is your problem. 这是你的问题。 Since the arguments are type double
and float
, you should be using %f
for both (since printf
is a variadic function, ftemp
will be promoted to double
). 由于参数是double
和float
类型,因此你应该使用%f
(因为printf
是一个可变参数函数, ftemp
将被提升为double
)。
%d
expects the corresponding argument to be type int
, not double
. %d
期望相应的参数是int
类型,而不是double
。
Variadic functions like printf
don't really know the types of the arguments in the variable argument list; 像printf
这样的变量printf
并不真正知道变量参数列表中参数的类型。 you have to tell it with the conversion specifier. 你必须用转换说明符告诉它。 Since you told printf
that the first argument is supposed to be an int
, printf will take the next sizeof (int)
bytes from the argument list and interpret it as an integer value; 既然你告诉printf
第一个参数应该是一个int
,printf将从参数列表中获取下一个sizeof (int)
字节并将其解释为整数值; hence the first garbage number. 因此第一个垃圾号码。
Now, it's almost guaranteed that sizeof (int)
< sizeof (double)
, so when printf
takes the next sizeof (double)
bytes from the argument list, it's probably starting with the middle byte of temp
, rather than the first byte of ftemp
; 现在,几乎可以保证sizeof (int)
< sizeof (double)
,所以当printf
从参数列表中获取下一个sizeof (double)
字节时,它可能从temp
的中间字节开始,而不是ftemp
的第一个字节; hence the second garbage number. 因此第二个垃圾号码。
Use %f
for both. 两者都使用%f
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.