C语言 将字符转换为双精度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13424265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert a char to double
提问by Frank
Ok, I have a charthat is a number. How could I convert it into a double?
好的,我有char一个数字。我怎么能把它转换成一个double?
char c;
I've tried (double)c, but it converts to zero.
我试过(double)c,但它转换为零。
Any ideas?
有任何想法吗?
回答by iabdalkader
if you have a null terminatedstring that you wish to convert to double use atof:
如果您有一个空终止字符串,您希望将其转换为双重使用atof:
const char *str = "3.14";
double x = atof(str);
printf("%f\n", x); //prints 3.140000
If you have a single character, casting should work:
如果您只有一个角色,则转换应该有效:
char c = 'a'; //97 in ASCII
double x = (double)c;
printf("%f\n", x); //prints 97.000000
If the character is zero then it print zeros of course:
如果字符为零,那么它当然打印零:
char c = 'const char *str = "3.14";
double x = strtod(str, NULL);
';
double x = (double)c;
printf("%f\n", x); //prints 0.000000
Note: atofand similar functions don't detect overflows and return zero on error, so there's no way to know if it failed (not sure if it sets errno), see also Keith's comments about undefined behaviourfor certain values, so the point is you should use strtolfor converting from strings to intand strtodfor converting to doublethose have much better error handling:
注意:atof和类似的函数不检测溢出并在错误时返回零,因此无法知道它是否失败(不确定它是否设置errno),另请参阅 Keith对某些值的未定义行为的评论,所以重点是你应该使用strtol从字符串转换为int和strtod转换到double那些有更好的错误处理:
#include <stdio.h>
int main(void) {
char c = 42;
// double d = (double)c; The cast is not needed here, because ...
double d = c; // ... the conversion is done implicitly.
printf("c = %d\n", c);
printf("d = %f\n", d);
return 0;
}
回答by Keith Thompson
To answer the question you asked:
要回答您提出的问题:
##代码##charis an integer type; its range is typically either -128to +127or 0to +255. It's most commonly used to store character values like 'x', but it can also be used to store small integers.
char是整数类型;它的范围通常是-128to+127或0to +255。它最常用于存储像 那样的字符值'x',但它也可以用于存储小整数。
But I suspect you really want to know how to convert a character string, like "1234.5", to type doublewith the numeric value 1234.5. There are several ways to do this.
但我怀疑你真的想知道如何将一个字符转换字符串,如"1234.5",要键入double与数值1234.5。有几种方法可以做到这一点。
The atof()function takes a char*that points to a string, and returns a doublevalue; atof("1234.5")returns 1234.5. But it does no real error handing; if the argument is too big, or isn't a number, it can behave badly. (I'm not sure of the details of that, but I believe its behavior is undefined in some cases.)
该atof()函数采用char*指向字符串的 a ,并返回一个double值; atof("1234.5")返回1234.5。但它没有真正的错误处理;如果参数太大,或者不是一个数字,它就会表现得很糟糕。(我不确定它的细节,但我相信在某些情况下它的行为是未定义的。)
The strtod()function does the same thing and is much more robust, but it's more complex to use. Consult your system's documentation (man strtodif you're on a Unix-like system).
该strtod()函数执行相同的操作并且更加健壮,但使用起来更加复杂。请查阅您的系统文档(man strtod如果您使用的是类 Unix 系统)。
And as Coodey said in a comment, you need to make your question more precise. An example with actual code would have made it easier to figure out just what you're asking.
正如库迪在评论中所说,你需要让你的问题更准确。一个带有实际代码的示例可以更容易地弄清楚你在问什么。

